class Token(Model): token = Field(str, coerce=str) email = Field(str, coerce=str) def __init__(self, email, token): self.email = email self.token = token @property def id(self): return self.token
class Winner(Model): winners = Field(dict, default=dict) contest = Field(str, coerce=str) @property def id(self): return self.contest def __init__(self, contest): self.contest = contest def add_winner(self, category, winner): self.winners[category] = winner def get_points(self, votes): points = 0 categories = CATEGORIES.contest(self.contest) for category, vote in votes.votes.items(): if vote == self.winners.get(category): points += categories.get(category).points return points def get_missed_points(self, votes): points = 0 categories = CATEGORIES.contest(self.contest) for category, vote in votes.votes.items(): if self.winners.get(category) and \ vote != self.winners.get(category): points += categories.get(category).points return points @property def remaining_points(self): points = 0 categories = CATEGORIES.contest(self.contest) for category in categories: if category.key not in self.winners: points += category.points return points @property def progress(self): category_count = 0 categories = CATEGORIES.contest(self.contest) for category in categories: if category.key in self.winners: category_count += 1 return float(category_count) / len(categories)
class User(Model): name = Field(str, coerce=str, serialize=validate_name) email = Field(str, coerce=str, serialize=validate_email) password = Field(str, coerce=str, serialize=hash_password) token = Field(str, coerce=str, default=generate_token) admin = Field(bool, default=False) def __init__(self, name, email, password): self.name = name self.email = email self.password = password @property def id(self): return self.email def generate_reset_password_url(self, base_url, expiration=None): expiration = str(int(expiration or time.time() + _RESET_EXPIRATION)) raw_value = base_url + self.email + self.password + self.token + \ expiration + config.PASSWORD_SALT signature = hashlib.sha256(raw_value.encode("utf8")).hexdigest() # won't be friendly with query parameters. url = "{0}?{1}".format(base_url, urllib.parse.urlencode({ "reset_signature": signature, "reset_expiration": expiration, "reset_email": self.email })) return url def verify_reset_password_url(self, signed_url): base_url, query_arguments = signed_url.split("?") query_parameters = dict([ (k, v) for k, v in urlparse.parse_qsl(query_arguments) ]) expiration = query_parameters["reset_expiration"] if int(expiration) < time.time(): return False generated_url = self.generate_reset_password_url(base_url, expiration) return signed_url == generated_url def authenticate(self, password): return self.password == hash_password(self, password)
class Votes(Model): email = Field(str, coerce=str) votes = Field(dict, default=dict) contest = Field(str, coerce=str) @property def id(self): return "{0}:{1}".format(self.contest, self.email) def __init__(self, email, contest): self.email = email self.contest = contest def add_vote(self, category_key, nominee_key): self.votes[category_key] = nominee_key @property def progress(self): categories = CATEGORIES.contest(self.contest) return float(len(self.votes)) / len(categories) def get_vote(self, key): return self.votes.get(key, None)
def test_field_set_default_without_default(self): model = {} field = Field() result = field.set_default("field", field) self.assertEqual({}, model) self.assertEqual(EMPTY, result)
def test_field_set_default_with_default(self): model = {} field = Field(default="foo") result = field.set_default("field", model) self.assertEqual({"field": "foo"}, model) self.assertEqual("foo", result)
class M(Model): field = Field(default="whatever")
class M(Model): id = Field()
class M(Model): field1 = Field(default="foobar") field2 = Field() field3 = Field(required=True)
class M(Model): field = Field()
class M(Model): field1 = Field(default="foobar") field2 = Field(default=None) field3 = Field()
class M(Model): field = Field(required=True, default="foo")
class M(Model): field = Field(required=True)