Restrict usernames to lowercase, digits, and hyphens.
[zzz-floof.git] / floof / model / users.py
1 #
2 # floof/floof/model/users.py
3 #
4 # Copyright (c) 2009 Scribblr
5 #
6
7 import re
8
9 from elixir import *
10
11 from search import GalleryWidget
12
13 class User(Entity):
14 name = Field(Unicode(20))
15 display_name = Field(Unicode(20))
16 uploads = OneToMany('Art')
17 has_many('identity_urls', of_kind='IdentityURL')
18 searches = OneToMany('SavedSearch')
19 # galleries = OneToMany('GalleryWidget')
20 pages = OneToMany('UserPage', inverse="owner")
21 primary_page = OneToOne('UserPage', inverse="owner")
22 relationships = OneToMany('UserRelationship', inverse='user')
23 target_of_relationships = OneToMany('UserRelationship', inverse='target_user')
24
25 @classmethod
26 def is_valid_name(cls, name):
27 """Returns True iff the name is a valid username.
28
29 Only lowercase letters, numbers, and hyphens are allowed.
30
31 Names must also be at least one character long, but no more than 20,
32 and cannot start or end with a hyphen.
33 """
34 return re.match('^[-a-z0-9]{1,20}$', name) \
35 and name[0] != '-' and name[-1] != '-'
36
37
38 def __unicode__(self):
39 return self.name
40
41 def __str__(self):
42 return self.name
43
44 def __init__(self, **kwargs):
45 super(User, self).__init__(**kwargs)
46
47
48
49 # TODO: have this clone a standard starter page
50 self.primary_page = UserPage(owner=self, title="default", visible=True)
51
52 # a starter gallery, just for fun
53 gallery = GalleryWidget(owner=self, string="awesome")
54 self.primary_page.galleries.append(gallery)
55
56
57 class IdentityURL(Entity):
58 url = Field(Unicode(255))
59 belongs_to('user', of_kind='User')
60
61
62
63 class UserPage(Entity):
64 """A user can have multiple pages, though by default they only have one visible.
65 This is so that they can keep some nice themed pages lying around for special occasions.
66 Page templates that provide familiar interfaces will also be UserPage records. Users will
67 see a panel full of them, and they can choose to clone those template pages to their own page list.
68 If more than one is set to visible, there would be tabs. The primary page is indicated in the user model.
69 """
70
71 owner = ManyToOne('User', inverse="pages")
72 title = Field(String)
73
74 visible = Field(Boolean)
75 galleries = OneToMany('GalleryWidget')
76
77
78 class UserRelationshipTypes(object):
79 IS_WATCHING = 1
80
81 class UserRelationship(Entity):
82 """Represents some sort of connection between users.
83
84 For the moment, this means "watching". Later, it may mean friending or
85 ignoring.
86
87 XXX: Watching should be made more general than this; it should have the
88 power of an arbitrary query per watched artist without being unintelligible
89 to users.
90 """
91
92 user = ManyToOne('User')
93 target_user = ManyToOne('User')
94 type = Field(Integer) # UserRelationshipTypes above
95