coding utf-8 from django db import models from django contrib auth mod

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# -*- coding: utf-8 -*-
from django.db import models
from django.contrib.auth.models import User
from fields import AutoOneToOneField
from blog.models import Post, Comment
GENDER_CHOICES = (
('M', 'Male'),
('F', 'Female'),
)
ACTIONS = (
('cs', 'can_see'),
('cm', 'can_moderate')
#('ce', 'can_edit'),
#('cm', 'can_move'),
#('cd', 'can_delete'),
#('cw', 'can_warnings'),
)
class Permission(models.Model):
post = models.ForeignKey(Post)
action = models.CharField(max_length=2, choices=ACTIONS)
def _get_dict(self):
_dict[self.action] = self.post
return _dict
class Group(models.Model):
name = models.CharField(max_length=30)
img = models.ImageField(upload_to='%d', null=True)
permissions = models.ManyToManyField(Permission, null=True)
def _get_perms(self):
perms = {}
for perm in self.permissions:
perms.update(perm._get_dict())
return perms
def __unicode__(self):
return self.name
class Profile(models.Model):
user = AutoOneToOneField(User, related_name='blog_profile', primary_key=True)
registration_key = models.CharField(max_length=40)
sex = models.CharField(max_length=1, choices=GENDER_CHOICES, null=True)
site = models.URLField(verify_exists=False, null=True)
group = models.ForeignKey(Group, null=True)
permissions = models.ManyToManyField(Permission)
def __unicode__(self):
return self.user.username
def get_absolute_url(self):
return "/profile/%s/" % self.id
def activate_user(self):
self.user.is_active = True
self.user.save()
def has_perm(self, action, post):
group_perms = self.group._get_perms()
user_perms = {}
for perm in permissions:
user_perms.update(perm._get_dict)
user_perms.update(group_perms)
if user_perms.has_key(action):
if user_perms[action] == post:
return True