Start to switch over to a Peewee-based model instead
Getty Ritter
4 years ago
5 | 5 | import pystache |
6 | 6 | import web |
7 | 7 | |
8 | import model | |
8 | 9 | import storage |
9 | 10 | |
10 | 11 | web.template.ALLOWED_AST_NODES.append("Constant") |
67 | 68 | @app.route("/") |
68 | 69 | @main |
69 | 70 | 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() | |
72 | 72 | |
73 | 73 | |
74 | 74 | @app.route("/design/<id>/<_slug>/") |
75 | 75 | @main |
76 | 76 | def get_design(id, _slug=None): |
77 |
design = |
|
77 | design = model.Design.get(visible_id=id) | |
78 | 78 | return "Designs", Templates.design_page(design) |
79 | 79 | |
80 | 80 | |
225 | 225 | # else: |
226 | 226 | # raise web.redirect('/edit/design/{0:05}/'.format(id)) |
227 | 227 | |
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 | ||
250 | 228 | # class modify_photo: |
251 | 229 | |
252 | 230 | |
353 | 331 | # '/edit/photo/([^/]*)/?', admin.modify_photo, |
354 | 332 | # '/edit/view-photo/?', admin.view_all_photos, |
355 | 333 | # '/edit/view-photo/([^/]*)/?', admin.view_photo, |
356 | # '/edit/file-list/?', admin.all_files, | |
357 | # '/edit/file/?', admin.add_file, | |
358 | 334 | # '/edit/pages/?', admin.edit_page_list, |
359 | 335 | # '/edit/pages/([^/]*)/?', admin.edit_page, |
360 | 336 | # '/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] |
111 | 111 | |
112 | 112 | [[package]] |
113 | 113 | 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" | |
114 | 122 | description = "Python Imaging Library (Fork)" |
115 | 123 | name = "pillow" |
116 | 124 | optional = false |
159 | 167 | watchdog = ["watchdog"] |
160 | 168 | |
161 | 169 | [metadata] |
162 |
content-hash = " |
|
170 | content-hash = "5fea0a12a60b3694817cdc385006627049d260549ff078073a48c7aa9d285796" | |
163 | 171 | python-versions = "^3.8" |
164 | 172 | |
165 | 173 | [metadata.files] |
230 | 238 | {file = "more-itertools-8.2.0.tar.gz", hash = "sha256:b1ddb932186d8a6ac451e1d95844b382f55e12686d51ca0c68b6f61f2ab7a507"}, |
231 | 239 | {file = "more_itertools-8.2.0-py3-none-any.whl", hash = "sha256:5dd8bcf33e5f9513ffa06d5ad33d78f31e1931ac9a18f33d37e77a180d393a7c"}, |
232 | 240 | ] |
241 | peewee = [ | |
242 | {file = "peewee-3.13.1.tar.gz", hash = "sha256:9492af4d1f8e18a7fa0e930960315b38931286ea0f1659bbd5503456cffdacde"}, | |
243 | ] | |
233 | 244 | pillow = [ |
234 | 245 | {file = "Pillow-7.0.0-cp35-cp35m-macosx_10_6_intel.whl", hash = "sha256:5f3546ceb08089cedb9e8ff7e3f6a7042bb5b37c2a95d392fb027c3e53a2da00"}, |
235 | 246 | {file = "Pillow-7.0.0-cp35-cp35m-manylinux1_i686.whl", hash = "sha256:9d2ba4ed13af381233e2d810ff3bab84ef9f18430a9b336ab69eaf3cd24299ff"}, |
12 | 12 | markdown = "^3.1.1" |
13 | 13 | web-py = "^0.40" |
14 | 14 | pillow = "^7.0.0" |
15 | peewee = "^3.13.1" | |
15 | 16 | |
16 | 17 | [tool.poetry.dev-dependencies] |
17 | 18 |
67 | 67 | |
68 | 68 | def tags(self): |
69 | 69 | 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 | ] | |
70 | 77 | |
71 | 78 | @classmethod |
72 | 79 | def list(cls, query_results) -> typing.List["Design"]: |
280 | 287 | |
281 | 288 | def delete_design(self, id): |
282 | 289 | 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("")) | |
296 | 290 | |
297 | 291 | def add_photo(self, file, d_id): |
298 | 292 | self.photo_num = self.get_max_photo_num() + 1 |
3 | 3 | <h2>{{title}}</h2> |
4 | 4 | </div> |
5 | 5 | <div class="images"> |
6 |
{{# |
|
6 | {{#photos}} | |
7 | 7 | <div class="image"> |
8 |
<img src="/static/photos/{{ |
|
8 | <img src="/static/photos/{{filename}}" /> | |
9 | 9 | </div> |
10 |
{{/ |
|
10 | {{/photos}} | |
11 | 11 | </div> |
12 | 12 | <div class="description">{{#description}}{{{ rendered }}}{{/description}}</div> |
13 | 13 | <div class="related"> |
14 |
|
|
14 | {{#category}} | |
15 | <a href="/category/{{name}}/">{{nicename}}</a> | |
16 | {{/category}} | |
15 | 17 | </div> |
16 | 18 | </div> |