This repository has been archived by the owner on Sep 9, 2020. It is now read-only.
/
models.py
90 lines (70 loc) · 2.1 KB
/
models.py
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
89
# -*- coding: utf-8 -*-
from google.appengine.ext import ndb
class Link(ndb.Model):
"""Represents link data type."""
title = ndb.StringProperty()
def to_dict(self):
d = super(Link, self).to_dict()
d.update({'url': self.key.id()})
return d
class Skill(ndb.Model):
"""Models an individual Skill entry with title and desc,
approved and link."""
title = ndb.StringProperty()
desc = ndb.StringProperty()
approved = ndb.IntegerProperty(default=0)
order = ndb.IntegerProperty(default=0)
links = ndb.StringProperty(repeated=True)
date = ndb.DateTimeProperty(auto_now_add=True)
enabled = ndb.BooleanProperty(default=True)
def approve(self):
"""
Approves skill
"""
self.approved += 1
def to_dict(self):
d = super(Skill, self).to_dict()
d.update({'_id': self.key.id()})
if d['links']:
links = []
for link in d['links']:
l = Link.get_by_id(link)
if l:
links.append(l.to_dict())
d['links'] = links
return d
@classmethod
def delete(cls, skill_id):
return cls.get(int(skill_id)).delete()
@classmethod
def get(cls, skill_id):
return cls.get_by_id(int(skill_id))
@classmethod
def all(cls):
return cls.query(Skill.enabled == True).order(-cls.approved).fetch()
class Goal(ndb.Model):
"""
Goal Model
"""
title = ndb.StringProperty()
done = ndb.BooleanProperty(default=False)
votes = ndb.IntegerProperty(default=0)
enabled = ndb.BooleanProperty(default=True)
def to_dict(self):
d = super(Goal, self).to_dict()
d.update({'_id': self.key.id()})
return d
def delete(self):
self.enabled = False
self.put()
def restore(self):
self.enabled = True
self.put()
def purge(self):
self.key.delete()
@classmethod
def all(cls):
return cls.query().order(-cls.votes).fetch()
@classmethod
def get(cls, goal_id):
return cls.get_by_id(int(goal_id))