-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
414 lines (354 loc) · 15.5 KB
/
server.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import cherrypy
import inspect, os
from mako.lookup import TemplateLookup
from Core.Database import Database
from models import *
from math import *
from routes import *
try:
_curdir = os.path.join(os.getcwd(), os.path.dirname(os.path.abspath(__file__)))
except NameError:
__file__ = inspect.getfile(inspect.currentframe())
_curdir = os.path.join(os.getcwd(), os.path.dirname(os.path.abspath(__file__)))
print(_curdir)
myTemplatesDir = os.path.join(_curdir, 'View/template')
myModulesDir = os.path.join(_curdir, 'View/tmp/mako_modules')
mylookup = TemplateLookup(directories=[myTemplatesDir], module_directory=myModulesDir,
output_encoding='utf-8',
encoding_errors='replace')
# ------------------------------------------------------------
# Templates HTML
# ------------------------------------------------------------
_index = mylookup.get_template("index.html")
_collection = mylookup.get_template("collection.html")
_shopping_list = mylookup.get_template("shopping_list.html")
_no_category = mylookup.get_template("no_category.html")
_search = mylookup.get_template("search.html")
_manga_list = mylookup.get_template("manga_list.html")
_manga_detail = mylookup.get_template("manga_detail.html")
_manga_new = mylookup.get_template("manga_new.html")
_scenariste_list = mylookup.get_template("scenariste_list.html")
_scenariste_new = mylookup.get_template("scenariste_new.html")
_dessinateur_list = mylookup.get_template("dessinateur_list.html")
_dessinateur_new = mylookup.get_template("dessinateur_new.html")
_editeur_list = mylookup.get_template("editeur_list.html")
_editeur_new = mylookup.get_template("editeur_new.html")
_genre_list = mylookup.get_template("genre_list.html")
_genre_new = mylookup.get_template("genre_new.html")
_tome_new = mylookup.get_template("tome_new.html")
_tome_detail = mylookup.get_template("tome_detail.html")
# ------------------------------------------------------------
# Point d'entrée de l'application "MangaCollection"
# ------------------------------------------------------------
class App:
"""
Application hébergée par CherryPy
"""
# On autorise les sessions
# _cp_config = { 'tools.sessions.on': True }
@cherrypy.expose
def index(self):
db = Database()
genreChart = []
editeurChart = []
genres = db.retrieve(Genre)
editeurs = db.retrieve(Editeur)
mangas = db.retrieve(Manga)
for genre in genres:
compteur = 0
for manga in mangas:
if manga.genre == genre.genre:
for tome in manga.tomes:
if tome.possede == True:
compteur += 1
if compteur > 0:
tuple = (genre.genre, compteur)
genreChart.append(tuple)
for editeur in editeurs:
compteur = 0
for manga in mangas:
if manga.editeur == editeur.editeur:
for tome in manga.tomes:
if tome.possede == True:
compteur += 1
if compteur >0:
tuple = (editeur.editeur, compteur)
editeurChart.append(tuple)
return _index.render_unicode(genres = genreChart, editeurs = editeurChart)
def collection(self):
db = Database()
mangas = db.retrieve(Manga)
total = 0
for manga in mangas:
for tome in manga.tomes:
if tome.possede == True:
total+= tome.prix
total = ceil(total)
return _collection.render_unicode(mangas=mangas, total=total)
def shopping_list(self):
db = Database()
mangas = db.retrieve(Manga)
total = 0
for manga in mangas:
for tome in manga.tomes:
if tome.a_acheter == True:
total+= tome.prix
total = ceil(total)
return _shopping_list.render_unicode(mangas = mangas, total=total)
def shopping_list_comfirm(self):
db = Database()
tomes = db.retrieveTome()
for tome in tomes:
if tome.a_acheter == True:
tome.a_acheter = False
tome.possede = True
db.update()
raise cherrypy.HTTPRedirect('/')
def no_category(self):
db = Database()
mangas = db.retrieve(Manga)
return _no_category.render_unicode(mangas = mangas)
class MangaController:
@cherrypy.expose
def manga_list(self):
db = Database()
mangas = db.retrieve((Manga))
return _manga_list.render_unicode(mangas=mangas)
@cherrypy.expose
def manga_detail(self, id):
db = Database()
manga = db.retrieve(Manga, Manga.id, id)
commentaire = db.retrieve(Commentaire, Commentaire.id, manga.id)
return _manga_detail.render_unicode(manga = manga, commentaire=commentaire)
@cherrypy.expose
def manga_delete(self, id):
db = Database()
manga = db.retrieve(Manga, Manga.id, id)
db.delete(manga, Manga)
raise cherrypy.HTTPRedirect('/')
@cherrypy.expose
def manga_add(self, titre, description, editeur, scenariste, dessinateur, statut, genre):
manga = Manga(titre, description, editeur, scenariste, dessinateur, statut, genre)
db = Database()
db.create(manga, Manga)
raise cherrypy.HTTPRedirect('/manga/')
@cherrypy.expose
def manga_new(self):
db = Database()
return _manga_new.render_unicode(
scenaristes=db.retrieve(Scenariste),
dessinateurs=db.retrieve(Dessinateur),
editeurs=db.retrieve(Editeur),
genres=db.retrieve(Genre)
)
@cherrypy.expose
def manga_statut(self, id, statut):
db = Database()
manga = db.retrieve(Manga, Manga.id, id)
print('######################## Statut : ',statut)
if statut == 'ongoing':
manga.statut = "En cours"
if statut == 'complete':
manga.statut = "Terminé"
if statut == 'pause':
manga.statut = "En pause"
print('######################## Statut : ', manga.statut)
db.update()
raise cherrypy.HTTPRedirect('/manga/'+id+'/')
class ScenaristeController:
@cherrypy.expose
def scenariste_list(self):
db = Database()
scenaristes = db.retrieve(Scenariste)
return _scenariste_list.render_unicode(scenaristes=scenaristes)
@cherrypy.expose
def scenariste_new(self):
return _scenariste_new.render_unicode()
@cherrypy.expose
def scenariste_add(self, scenariste):
db = Database()
s = Scenariste(scenariste)
db.create(s,Scenariste)
raise cherrypy.HTTPRedirect('/scenariste/')
class TomeController:
@cherrypy.expose
def tome_detail(self, id_manga, id_tome):
db = Database()
tome = db.retrieveTome(id_manga,id_tome)
return _tome_detail.render_unicode(tome = tome)
@cherrypy.expose
def tome_new(self):
db = Database()
mangas = db.retrieve(Manga)
return _tome_new.render_unicode(mangas = mangas)
@cherrypy.expose
def tome_delete(self, id_manga, id_tome):
db = Database()
tome = db.retrieveTome(id_manga, id_tome)
db.deleteTome(tome)
raise cherrypy.HTTPRedirect('/')
@cherrypy.expose
def tome_buy(self, id_manga, id_tome):
db = Database()
tome = db.retrieveTome(id_manga, id_tome)
tome.a_acheter = False
tome.possede = True
db.update()
raise cherrypy.HTTPRedirect('/collection/')
@cherrypy.expose
def tome_cart(self, id_manga, id_tome):
db = Database()
tome = db.retrieveTome(id_manga, id_tome)
tome.a_acheter = True
db.update()
raise cherrypy.HTTPRedirect('/shopping_list/')
@cherrypy.expose
def tome_read(self, id_manga, id_tome):
db = Database()
tome = db.retrieveTome(id_manga, id_tome)
tome.lu = True
db.update()
raise cherrypy.HTTPRedirect('/')
@cherrypy.expose
def tome_sell(self, id_manga, id_tome):
db = Database()
tome = db.retrieveTome(id_manga, id_tome)
tome.possede = False
db.update()
raise cherrypy.HTTPRedirect('/collection/')
@cherrypy.expose
def tome_add(self, manga_id, numero, date_parution, prix, couverture, date_achat=None, possede=None, lu=None,
a_acheter=None):
tome = Tome(manga_id, numero, date_parution, possede, lu, a_acheter, float(prix), couverture, date_achat)
db = Database()
db.create(tome,Tome)
raise cherrypy.HTTPRedirect('/')
class DessinateurController:
@cherrypy.expose
def dessinateur_list(self):
db = Database()
dessinateurs = db.retrieve(Dessinateur)
return _dessinateur_list.render_unicode(dessinateurs=dessinateurs)
@cherrypy.expose
def dessinateur_new(self):
return _dessinateur_new.render_unicode()
@cherrypy.expose
def dessinateur_add(self, dessinateur):
db = Database()
d = Dessinateur(dessinateur)
db.create(d, Dessinateur)
raise cherrypy.HTTPRedirect('/dessinateur/')
class EditeurController:
@cherrypy.expose
def editeur_list(self):
db = Database()
editeurs = db.retrieve(Editeur)
return _editeur_list.render_unicode(editeurs=editeurs)
@cherrypy.expose
def editeur_new(self):
return _editeur_new.render_unicode()
@cherrypy.expose
def editeur_add(self, editeur):
db = Database()
e = Editeur(editeur)
db.create(e, Editeur)
raise cherrypy.HTTPRedirect('/editeur/')
class GenreController:
@cherrypy.expose
def genre_list(self):
db = Database()
genres = db.retrieve(Genre)
return _genre_list.render_unicode(genres=genres)
@cherrypy.expose
def genre_new(self):
return _genre_new.render_unicode()
@cherrypy.expose
def genre_add(self, genre):
db = Database()
g = Genre(genre)
db.create(g, Genre)
raise cherrypy.HTTPRedirect('/genre/')
class SearchController:
@cherrypy.expose
def search(self, query):
db = Database()
mangas, editeurs, scenaristes, dessinateurs, genres = db.search(query)
return _search.render_unicode(
mangas=mangas,
editeurs=editeurs,
scenaristes=scenaristes,
dessinateurs=dessinateurs,
genres=genres
)
class CommentaireController:
@cherrypy.expose
def add_commentaire(self,commentaire, titre, id):
db = Database()
commentaire = Commentaire(commentaire, titre, id)
db.create(commentaire, Commentaire)
raise cherrypy.HTTPRedirect('/manga/'+id+'/')
if __name__ == '__main__':
global_conf = {
'global': {'autoreload.on': False,
'server.socket_host': '127.0.0.1',
'server.protocol_version': 'HTTP/1.1',
'server.thread_pool': 5,
'tools.encode.encoding': "Utf-8",
'environment': 'production',
'log.error_file': 'site.log',
'log.screen': True}}
cherrypy.config.update(global_conf)
app = App()
d = cherrypy.dispatch.RoutesDispatcher()
d.connect('default_route', '/', controller=app, action='index')
d.connect('collection', '/collection/', controller=app, action='collection')
d.connect('shopping_list', '/shopping_list/', controller=app, action='shopping_list')
d.connect('no_category', '/no_category/', controller=app, action='no_category')
d.connect('shopping_list_comfirm', '/shopping_list/comfirm/', controller=app, action='shopping_list_comfirm')
d.connect('commentaire_add', '/comment/add/:id/', controller=CommentaireController, action="add_commentaire")
d.connect('manga_list', '/manga/', controller=MangaController, action="manga_list")
d.connect('manga_new', '/manga/new/', controller=MangaController, action="manga_new")
d.connect('manga_add', '/manga/add/', controller=MangaController, action="manga_add")
d.connect('manga_detail', '/manga/:id/', controller=MangaController, action='manga_detail')
d.connect('manga_statut', '/manga/:id/:statut/', controller=MangaController, action='manga_statut')
d.connect('manga_delete', '/manga/delete/:id/', controller=MangaController, action='manga_delete')
d.connect('tome_new', '/tome/new/', controller=TomeController, action="tome_new")
d.connect('tome_add', '/tome/add/', controller=TomeController, action="tome_add")
d.connect('tome_detail', '/manga/:id_manga/tome/:id_tome/', controller=TomeController, action='tome_detail')
d.connect('tome_delete', '/manga/:id_manga/tome/delete/:id_tome', controller=TomeController, action='tome_delete')
d.connect('tome_buy', '/manga/:id_manga/tome/buy/:id_tome', controller=TomeController, action='tome_buy')
d.connect('tome_sell', '/manga/:id_manga/tome/sell/:id_tome', controller=TomeController, action='tome_sell')
d.connect('tome_cart', '/manga/:id_manga/tome/cart/:id_tome', controller=TomeController, action='tome_cart')
d.connect('tome_read', '/manga/:id_manga/tome/read/:id_tome', controller=TomeController, action='tome_read')
d.connect('scenariste_list','/scenariste/', controller=ScenaristeController, action="scenariste_list")
d.connect('scenariste_new','/scenariste/new/', controller=ScenaristeController, action="scenariste_new")
d.connect('scenariste_add','/scenariste/add/', controller=ScenaristeController, action="scenariste_add")
d.connect('dessinateur_list','/dessinateur/', controller=DessinateurController, action="dessinateur_list")
d.connect('dessinateur_new','/dessinateur/new/', controller=DessinateurController, action="dessinateur_new")
d.connect('dessinateur_add','/dessinateur/add/', controller=DessinateurController, action="dessinateur_add")
d.connect('editeur_list','/editeur/', controller=EditeurController, action="editeur_list")
d.connect('editeur_new','/editeur/new/', controller=EditeurController, action="editeur_new")
d.connect('editeur_add','/editeur/add/', controller=EditeurController, action="editeur_add")
d.connect('genre_list','/genre/', controller=GenreController, action="genre_list")
d.connect('genre_new','/genre/new/', controller=GenreController, action="genre_new")
d.connect('genre_add','/genre/add/', controller=GenreController, action="genre_add")
d.connect('search', '/search/', controller=SearchController, action="search")
conf_appli = {
'/': {'request.dispatch': d},
'/View': {'tools.staticdir.on': True, 'tools.staticdir.dir': os.path.join(_curdir, 'View')},
'/View/templates': {'tools.staticdir.on': True, 'tools.staticdir.dir': myTemplatesDir},
'/View/tmp/mako_modules': {'tools.staticdir.on': True, 'tools.staticdir.dir': myModulesDir},
'/View/css': {
'tools.staticdir.on': True, 'tools.staticdir.dir': os.path.join(_curdir, 'View/css'),
'tools.staticdir.content_types': {'css': 'text/css'}
},
'/View/images': {
'tools.staticdir.on': True, 'tools.staticdir.dir': os.path.join(_curdir, 'View/images'),
'tools.staticdir.content_types': {'jpg': 'image/jpg'}
},
'/View/js': {
'tools.staticdir.on': True,
'tools.staticdir.dir': os.path.join(_curdir, 'View/js'),
'tools.staticdir.content_types': {'js': 'application/javascript'}
}}
cherrypy.quickstart(app, config=conf_appli)