Added docs about using MongoEngine with Django

This commit is contained in:
Harry Marr 2010-01-07 22:48:39 +00:00
parent d48296eacc
commit b7e8108edd
3 changed files with 23 additions and 0 deletions

View File

@ -2,6 +2,14 @@
Changelog Changelog
========= =========
Changes is v0.1.3
=================
- Added Django authentication backend
- Added Document.meta support for indexes, which are ensured just before
querying takes place
- A few minor bugfixes
Changes in v0.1.2 Changes in v0.1.2
================= =================
- Query values may be processed before before being used in queries - Query values may be processed before before being used in queries

View File

@ -16,6 +16,7 @@ The source is available on `GitHub <http://github.com/hmarr/mongoengine>`_.
tutorial tutorial
userguide userguide
apireference apireference
django
changelog changelog
Indices and tables Indices and tables

View File

@ -32,6 +32,8 @@ class User(Document):
last_login = DateTimeField(default=datetime.datetime.now) last_login = DateTimeField(default=datetime.datetime.now)
def get_full_name(self): def get_full_name(self):
"""Returns the users first and last names, separated by a space.
"""
full_name = u'%s %s' % (self.first_name or '', self.last_name or '') full_name = u'%s %s' % (self.first_name or '', self.last_name or '')
return full_name.strip() return full_name.strip()
@ -42,6 +44,10 @@ class User(Document):
return True return True
def set_password(self, raw_password): def set_password(self, raw_password):
"""Sets the user's password - always use this rather than directly
assigning to :attr:`~mongoengine.django.auth.User.password` as the
password is hashed before storage.
"""
from random import random from random import random
algo = 'sha1' algo = 'sha1'
salt = get_hexdigest(algo, str(random()), str(random()))[:5] salt = get_hexdigest(algo, str(random()), str(random()))[:5]
@ -49,11 +55,19 @@ class User(Document):
self.password = '%s$%s$%s' % (algo, salt, hash) self.password = '%s$%s$%s' % (algo, salt, hash)
def check_password(self, raw_password): def check_password(self, raw_password):
"""Checks the user's password against a provided password - always use
this rather than directly comparing to
:attr:`~mongoengine.django.auth.User.password` as the password is
hashed before storage.
"""
algo, salt, hash = self.password.split('$') algo, salt, hash = self.password.split('$')
return hash == get_hexdigest(algo, salt, raw_password) return hash == get_hexdigest(algo, salt, raw_password)
@classmethod @classmethod
def create_user(cls, username, password, email=None): def create_user(cls, username, password, email=None):
"""Create (and save) a new user with the given username, password and
email address.
"""
user = User(username=username, email=email) user = User(username=username, email=email)
user.set_password(password) user.set_password(password)
user.save() user.save()