gdritter repos frony-ritter-designs / 20f235a
Start to switch over to a Peewee-based model instead Getty Ritter 4 years ago
6 changed file(s) with 87 addition(s) and 45 deletion(s). Collapse all Expand all
55 import pystache
66 import web
77
8 import model
89 import storage
910
1011 web.template.ALLOWED_AST_NODES.append("Constant")
6768 @app.route("/")
6869 @main
6970 def index():
70 pg = db.get_page("main")
71 return "Frony Ritter Designs", markdown.markdown(pg.text)
71 return "Frony Ritter Designs", model.Page.get(name="main").rendered()
7272
7373
7474 @app.route("/design/<id>/<_slug>/")
7575 @main
7676 def get_design(id, _slug=None):
77 design = db.get_design(int(id))
77 design = model.Design.get(visible_id=id)
7878 return "Designs", Templates.design_page(design)
7979
8080
225225 # else:
226226 # raise web.redirect('/edit/design/{0:05}/'.format(id))
227227
228 # class all_files:
229
230
231 # @main
232 # def GET(self):
233 # files = db.all_files()
234 # return ('All Uploaded Files', render.all_files(files))
235
236 # class add_file:
237
238
239 # @main
240 # def GET(self):
241 # return ('Add File', render.file_upload())
242
243
244 # def POST(self):
245 # input = web.input(file={})
246 # if 'file' in input:
247 # file_id = db.add_photo(input['file'])
248 # raise web.redirect('/edit/file-list/')
249
250228 # class modify_photo:
251229
252230
353331 # '/edit/photo/([^/]*)/?', admin.modify_photo,
354332 # '/edit/view-photo/?', admin.view_all_photos,
355333 # '/edit/view-photo/([^/]*)/?', admin.view_photo,
356 # '/edit/file-list/?', admin.all_files,
357 # '/edit/file/?', admin.add_file,
358334 # '/edit/pages/?', admin.edit_page_list,
359335 # '/edit/pages/([^/]*)/?', admin.edit_page,
360336 # '/edit/about/?', admin.edit_about,
1 import markdown
2 import peewee
3 import typing
4
5 db = peewee.SqliteDatabase("new.db")
6
7
8 class Model(peewee.Model):
9 class Meta:
10 database = db
11
12
13 class Category(Model):
14 name = peewee.TextField()
15 nicename = peewee.TextField()
16
17
18 class Design(Model):
19 visible_id = peewee.IntegerField()
20 title = peewee.TextField()
21 description = peewee.TextField()
22 category = peewee.ForeignKeyField(Category, backref="designs")
23
24 def rendered(self):
25 return markdown.markdown(self.description)
26
27
28 class Photo(Model):
29 filename = peewee.TextField()
30 design = peewee.ForeignKeyField(Design, backref="photos")
31
32
33 class Page(Model):
34 name = peewee.TextField()
35 text = peewee.TextField()
36 title = peewee.TextField()
37
38 def rendered(self):
39 return markdown.markdown(self.text)
40
41
42 class Tag(Model):
43 tag_name = peewee.TextField()
44 design = peewee.ForeignKeyField(Design, backref="tags")
45
46
47 class PageRef(typing.NamedTuple):
48 page: int
49
50 def __iter__(self):
51 return [self]
52
53
54 class Paginated(typing.NamedTuple):
55 next_page: typing.Optional[PageRef]
56 prev_page: typing.Optional[PageRef]
57 last_page: int
58 contents: typing.List[Design]
111111
112112 [[package]]
113113 category = "main"
114 description = "a little orm"
115 name = "peewee"
116 optional = false
117 python-versions = "*"
118 version = "3.13.1"
119
120 [[package]]
121 category = "main"
114122 description = "Python Imaging Library (Fork)"
115123 name = "pillow"
116124 optional = false
159167 watchdog = ["watchdog"]
160168
161169 [metadata]
162 content-hash = "00591a90d667ed96b8da4f5528d7382c0166c1ed3d04678632bea8d55604a843"
170 content-hash = "5fea0a12a60b3694817cdc385006627049d260549ff078073a48c7aa9d285796"
163171 python-versions = "^3.8"
164172
165173 [metadata.files]
230238 {file = "more-itertools-8.2.0.tar.gz", hash = "sha256:b1ddb932186d8a6ac451e1d95844b382f55e12686d51ca0c68b6f61f2ab7a507"},
231239 {file = "more_itertools-8.2.0-py3-none-any.whl", hash = "sha256:5dd8bcf33e5f9513ffa06d5ad33d78f31e1931ac9a18f33d37e77a180d393a7c"},
232240 ]
241 peewee = [
242 {file = "peewee-3.13.1.tar.gz", hash = "sha256:9492af4d1f8e18a7fa0e930960315b38931286ea0f1659bbd5503456cffdacde"},
243 ]
233244 pillow = [
234245 {file = "Pillow-7.0.0-cp35-cp35m-macosx_10_6_intel.whl", hash = "sha256:5f3546ceb08089cedb9e8ff7e3f6a7042bb5b37c2a95d392fb027c3e53a2da00"},
235246 {file = "Pillow-7.0.0-cp35-cp35m-manylinux1_i686.whl", hash = "sha256:9d2ba4ed13af381233e2d810ff3bab84ef9f18430a9b336ab69eaf3cd24299ff"},
1212 markdown = "^3.1.1"
1313 web-py = "^0.40"
1414 pillow = "^7.0.0"
15 peewee = "^3.13.1"
1516
1617 [tool.poetry.dev-dependencies]
1718
6767
6868 def tags(self):
6969 return db.get_tags_for_design(self.id)
70
71 def category_list(self):
72 categories = db.all_categories()
73 return [
74 {"name": c.name, "selected": c.name == self.category}
75 for c in categories
76 ]
7077
7178 @classmethod
7279 def list(cls, query_results) -> typing.List["Design"]:
280287
281288 def delete_design(self, id):
282289 self._db.delete("designs", where="id = $id", vars=dict(id=id))
283
284 def add_file(self, file):
285 new_id = self._db.query("select max(id) as n from files")[0].n + 1
286 extn = file.filename.split()[-1]
287 name = "{0:05}.{1}".format(new_id, extn)
288 path = "/" + os.path.join("srv", "http", "frd", "static", "files", name)
289 self.file_num = self.get_max_file_num() + 1
290 with open(path, "rb") as f:
291 f.write(file.file)
292 return new_id
293
294 def all_files(self):
295 return list(self._db.where(""))
296290
297291 def add_photo(self, file, d_id):
298292 self.photo_num = self.get_max_photo_num() + 1
33 <h2>{{title}}</h2>
44 </div>
55 <div class="images">
6 {{#images}}
6 {{#photos}}
77 <div class="image">
8 <img src="/static/photos/{{image}}" />
8 <img src="/static/photos/{{filename}}" />
99 </div>
10 {{/images}}
10 {{/photos}}
1111 </div>
1212 <div class="description">{{#description}}{{{ rendered }}}{{/description}}</div>
1313 <div class="related">
14 <a href="/category/{{category}}/">{{category}}</a>
14 {{#category}}
15 <a href="/category/{{name}}/">{{nicename}}</a>
16 {{/category}}
1517 </div>
1618 </div>