coding utf-8 import datetime import os import random import re import

 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# -*- coding: utf-8
import datetime
import os
import random
import re
import sha
from django.contrib.auth.models import User, UserManager
from django.core.urlresolvers import reverse
from django.conf import settings
from django.db import models
from geo.models import Country, City
from tagging.models import TagManager
from graphic.models import Picture
from managers import RegistrationManager
IM_CHOICES = ((0, u"ICQ"), (1, u"Jabber"), (2, u"MSN"))
RANK_CHOICES = ((0, u"Newbie"), (1, u"Scholar"))
class Profile(User):
display_name = models.CharField(max_length=30)
gender = models.IntegerField(choices=settings.SEX_CHOICES)
city = models.ForeignKey(City)
country_from = models.ForeignKey(Country, blank=True, null=True)
pictures = models.ForeignKey(Picture, related_name='pictures_set', blank=True, null=True)
friends = models.ManyToManyField('self', blank=True, through="Friend", symmetrical=False)
birthday = models.DateField()
ip = models.CharField(editable=False, max_length=15, blank=True)
im = models.IntegerField(choices=IM_CHOICES, blank=True, null=True)
im_number = models.CharField(max_length=50, blank=True)
rank = models.IntegerField(choices=RANK_CHOICES, default=0, editable=False)
score = models.IntegerField(default=0, editable=False)
tags = TagManager()
#activation and password remind fields
activation_key = models.CharField(u'activation key', max_length=40, blank=True)
remind_key = models.CharField(u'remind key', max_length=40, blank=True)
#statistic counters
comments_counter = models.IntegerField(default=0, editable=False)
friends_counter = models.IntegerField(default=0, editable=False)
pictures_counter = models.IntegerField(default=0, editable=False)
pm_counter = models.IntegerField(default=0, editable=False)
forum_threads_counter = models.IntegerField(default=0, editable=False)
forum_posts_counter = models.IntegerField(default=0, editable=False)
#managers
objects = UserManager()
custom_manager = RegistrationManager()
#urls methods
show_url = lambda self: reverse("show-profile", args=[self.id])
edit_url = lambda self: reverse("edit-profile", args=[self.id])
list_url = classmethod(lambda cls: reverse("list-profile"))
create_url = classmethod(lambda cls: reverse("create-profile"))
#custom methods
def set_activation_key(self):
SHA1_RE = re.compile('^[a-f0-9]{40}$')
salt = sha.new(str(random.random())).hexdigest()[:5]
self.activation_key = sha.new(salt+self.email).hexdigest()
class Meta:
verbose_name = "Profile"
verbose_name_plural = "Profiles"
def __unicode__(self):
return self.display_name
class Friend(models.Model):
friend1 = models.ForeignKey(Profile, related_name="set_friend1")
friend2 = models.ForeignKey(Profile, related_name="set_friend2")
friend_type = models.IntegerField(choices=settings.FRIEND_TYPE_CHOICES)
def __unicode__(self):
return "%s > %s (%s)" % (self.friend1.display_name, self.friend2, self.get_friend_type_display())