-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmodels.py
More file actions
78 lines (59 loc) · 1.98 KB
/
Copy pathmodels.py
File metadata and controls
78 lines (59 loc) · 1.98 KB
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
import os
import re
from google.appengine.ext import db
INVALID_SLUG_CHARS = re.compile(r'[^\w-]')
MULTIDASH_RE = re.compile(r'-+')
SLUG_TOKEN_AMOUNT = 2
class Team(db.Model):
CURRENT_VERSION = 2
primary_slug = db.StringProperty()
title = db.StringProperty(required=True)
description = db.TextProperty(required=True)
total_people_joined = db.IntegerProperty(default=0)
goal_dollars = db.IntegerProperty()
youtube_id = db.StringProperty()
zip_code = db.StringProperty()
# for use with google.appengine.api.images get_serving_url
image = db.BlobProperty()
gravatar = db.StringProperty()
user_token = db.StringProperty()
team_version = db.IntegerProperty(default=1)
creation_time = db.DateTimeProperty(auto_now_add=True)
modification_time = db.DateTimeProperty(auto_now=True)
@classmethod
def create(cls, **kwargs):
kwargs["team_version"] = cls.CURRENT_VERSION
team = cls(**kwargs)
team.put()
return team
class Slug(db.Model):
# the key is the slug name
team = db.ReferenceProperty(Team, required=True)
@staticmethod
@db.transactional
def _make(full_slug, team):
e = Slug.get_by_key_name(full_slug)
if e is not None:
return False
Slug(key_name=full_slug, team=team).put()
return True
@staticmethod
def new(team):
slug_name = MULTIDASH_RE.sub('-', INVALID_SLUG_CHARS.sub('-', team.title))
slug_name = slug_name.rstrip('-')
token_amount = SLUG_TOKEN_AMOUNT
while True:
slug_prefix = os.urandom(token_amount).encode('hex')
token_amount += 1
full_slug = "%s-%s" % (slug_prefix, slug_name)
if Slug._make(full_slug, team):
return full_slug
class AdminToTeam(db.Model):
"""This class represents an admin to team relationship, since it's
many-to-many
"""
user = db.StringProperty(required=True) # from current_user["user_id"]
team = db.ReferenceProperty(Team, required=True)
@staticmethod
def memcacheKey(user_id, team):
return repr((str(user_id), str(team.key())))