src/ldt/ldt/user/models.py
author cavaliet
Thu, 24 May 2012 16:04:07 +0200
branchmaintenance_0.21
changeset 624 2252fb64f74e
parent 276 ee792e43b36a
child 314 1a8620e5ebb0
permissions -rw-r--r--
update ldt for maintenance 00.21.2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
188
74bad93d3cb7 Merge with 50dd5c3e8ab8365982cffc4f411f9f5ba7686176
ymh <ymh.work@gmail.com>
parents: 187 175
diff changeset
     1
from django.conf import settings
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
     2
from django.contrib import admin
228
94fdb72b7d56 Users can add their own groups
verrierj
parents: 188
diff changeset
     3
from django.contrib.auth.models import User, UserManager, Group
169
64f24f8841ec Add profile to user
verrierj
parents: 155
diff changeset
     4
from django.db import models
174
43748b6b0678 Add a default profile instead of get_or_create_profile
verrierj
parents: 172
diff changeset
     5
from django.db.models.signals import post_save
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
     6
import datetime
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
     7
276
ee792e43b36a Replace is_regular field in userprofile by django permission
verrierj
parents: 259
diff changeset
     8
        
155
73ce005c4727 Changed name of class user.models.ldt to user.Ldt
verrierj
parents: 130
diff changeset
     9
class Ldt(User):
129
e9f11d142db5 the code works, but is not totally clean. all the parts meant to be removed are commented.
cavaliet
parents: 111
diff changeset
    10
#    irigroups = models.ManyToManyField(IriGroup, blank=True)
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    11
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    12
    class Meta:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    13
        verbose_name = 'iri user'
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    14
        verbose_name_plural = 'iri users'
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    15
    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    16
    def __unicode__(self):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    17
        return self.username
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    18
        
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    19
155
73ce005c4727 Changed name of class user.models.ldt to user.Ldt
verrierj
parents: 130
diff changeset
    20
class LdtManager(UserManager):
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    21
    def create_user(self, username, email, password=None):
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    22
        "Creates and saves a User with the given username, e-mail and password."
187
a59dd2aed213 change utc to utcnow
ymh <ymh.work@gmail.com>
parents: 130
diff changeset
    23
        now = datetime.datetime.utcnow()
169
64f24f8841ec Add profile to user
verrierj
parents: 155
diff changeset
    24
        new_user = Ldt(None, username, '', '', email.strip().lower(), 'placeholder', False, True, False, now, now)
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    25
        if password:
169
64f24f8841ec Add profile to user
verrierj
parents: 155
diff changeset
    26
            new_user.set_password(password)
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    27
        else:
169
64f24f8841ec Add profile to user
verrierj
parents: 155
diff changeset
    28
            new_user.set_unusable_password()
64f24f8841ec Add profile to user
verrierj
parents: 155
diff changeset
    29
        new_user.save()
247
f98f1a6e15f1 Add a group to contain published projects
verrierj
parents: 245
diff changeset
    30
        new_user.groups.add(Group.objects.get(name=settings.PUBLIC_GROUP_NAME))
f98f1a6e15f1 Add a group to contain published projects
verrierj
parents: 245
diff changeset
    31
        return new_user   
169
64f24f8841ec Add profile to user
verrierj
parents: 155
diff changeset
    32
    
64f24f8841ec Add profile to user
verrierj
parents: 155
diff changeset
    33
    
64f24f8841ec Add profile to user
verrierj
parents: 155
diff changeset
    34
class UserProfile (models.Model): 
64f24f8841ec Add profile to user
verrierj
parents: 155
diff changeset
    35
    user = models.OneToOneField(User)
175
d5d2439e2503 Profile migration + translations
verrierj
parents: 174
diff changeset
    36
    language = models.CharField(max_length=2, default=settings.LANGUAGE_CODE[:2])
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    37
174
43748b6b0678 Add a default profile instead of get_or_create_profile
verrierj
parents: 172
diff changeset
    38
    @staticmethod
43748b6b0678 Add a default profile instead of get_or_create_profile
verrierj
parents: 172
diff changeset
    39
    def create_user_profile(sender, instance, created, **kwargs):
43748b6b0678 Add a default profile instead of get_or_create_profile
verrierj
parents: 172
diff changeset
    40
        if created:
43748b6b0678 Add a default profile instead of get_or_create_profile
verrierj
parents: 172
diff changeset
    41
            UserProfile.objects.create(user=instance)
276
ee792e43b36a Replace is_regular field in userprofile by django permission
verrierj
parents: 259
diff changeset
    42
174
43748b6b0678 Add a default profile instead of get_or_create_profile
verrierj
parents: 172
diff changeset
    43
43748b6b0678 Add a default profile instead of get_or_create_profile
verrierj
parents: 172
diff changeset
    44
post_save.connect(UserProfile.create_user_profile, sender=User)
169
64f24f8841ec Add profile to user
verrierj
parents: 155
diff changeset
    45
   
155
73ce005c4727 Changed name of class user.models.ldt to user.Ldt
verrierj
parents: 130
diff changeset
    46
User.objects = LdtManager()
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    47
User.objects.contribute_to_class(User, "objects")
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    48
155
73ce005c4727 Changed name of class user.models.ldt to user.Ldt
verrierj
parents: 130
diff changeset
    49
admin.site.register(Ldt)