-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
1061 lines (714 loc) · 20.9 KB
/
server.py
File metadata and controls
1061 lines (714 loc) · 20.9 KB
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from bottle import *
from entities import *
from pony.orm.integration.bottle_plugin import PonyPlugin
install(PonyPlugin())
'''
Server module
=============
This module provides all the basic functionalities of the application.
The module is a standard Bottle application and runs on port 8080.
'''
#######################################################
### Begin route declaration
#######################################################
'''
Index page of the application.
'''
@route('/')
@view('index')
def index():
return
#############
### Books ###
#############
'''
Show all the books in the system.
'''
@route('/books')
@view('books/index')
def book_all():
# select all the books from the db
books = select(b for b in Book)
# set the books for the template
return dict(books=books)
'''
Show information for specific book.
'''
@route('/books/<id>/')
@view('books/show')
def book_show(id):
# get the book by id
book = Book[id]
# set the book for the template
return dict(book=book)
'''
Add new books to the system.
'''
@route('/books/add')
@view('books/add')
def book_add():
# retrieve the list of authors
authors = Author.select()
# retrieve the list of publishers
publishers = Publisher.select()
# retrieve the list of genres
genres = Genre.select()
# set the data for the screen
return dict(authors=authors, publishers=publishers, genres=genres)
'''
Take in as input the create form for a book and
add the the book to the database.
'''
@route('/books/add', method='POST')
def book_create():
# obtain the data for the book
title = request.forms.get('title')
author = request.forms.get('author')
publisher = request.forms.get('publisher')
genre = request.forms.get('genre')
year = request.forms.get('year')
price = request.forms.get('price')
# create the new book
Book(title=title, author=author, publisher=publisher, genre=genre, year=year, price=price)
# redirect the user to the books overview page
redirect("/books")
'''
Edit books information.
'''
@route('/books/<id>/edit')
@view('books/edit')
def book_edit(id):
# retrieve the book by id from the database
book = Book[id]
# retrieve the list of authors
authors = Author.select()
# retrieve the list of publishers
publishers = Publisher.select()
# retrieve the list of genres
genres = Genre.select()
# set the data for the edit screen
return dict(book=book, authors=authors, publishers=publishers, genres=genres)
'''
Take in as input the edit form for a book and
update the database accordingly.
'''
@route('/books/<id>/edit', method='POST')
def book_update(id):
# retrieve the book by id from the database
book = Book[id]
# updating the data of the book
book.title = request.forms.get('title')
book.author = Author[request.forms.get('author')]
book.publisher = Publisher[request.forms.get('publisher')]
book.genre = Genre[request.forms.get('genre')]
book.year = request.forms.get('year')
book.price = request.forms.get('price')
# redirect the user to the book info page
redirect("/books/%s/" % id)
'''
Delete book by id.
'''
@route('/books/<id>/delete')
def book_delete(id):
# delete the book with the specified id from the database
Book[id].delete()
# redirect the user to the books overview page
redirect("/books")
'''
Search page for books.
'''
@route('/books/search')
@view('books/search')
def book_search():
# retrieve the list of authors
authors = Author.select()
# retrieve the list of publishers
publishers = Publisher.select()
# set the data for the search screen
return dict(authors=authors, publishers=publishers)
'''
Search books by title.
'''
@route('/books/search/title', method="POST")
@view('books/found')
def book_search_title():
# obtain the title from the form
title = request.forms.get('title')
# filter only the books with that title
books = Book.select(lambda b: b.title == title)
# set the data for the screen
return dict(books=books)
'''
Search books by author.
'''
@route('/books/search/author', method="POST")
@view('books/found')
def book_search_author():
# obtain the author from the form
author = request.forms.get('author')
# filter only the books with that author
books = Book.select(lambda b: b.author.id == author)
# set the data for the screen
return dict(books=books)
'''
Search books by publisher.
'''
@route('/books/search/publisher', method="POST")
@view('books/found')
def book_search_publisher():
# obtain the publisher from the form
publisher = request.forms.get('publisher')
# filter only the books with that publisher
books = Book.select(lambda b: b.publisher.id == publisher)
# set the data for the screen
return dict(books=books)
###############
### Authors ###
###############
'''
Show all the authors in the system.
'''
@route('/authors')
@view('authors/index')
def author_all():
# select all the authors from the db
authors = select(a for a in Author)
# set the authors for the template
return dict(authors=authors)
'''
Show information for specific author.
'''
@route('/authors/<id>/')
@view('authors/show')
def author_show(id):
# get the author by id
author = Author[id]
# set the author for the template
return dict(author=author)
'''
Add new author to the system.
'''
@route('/authors/add')
@view('authors/add')
def author_add():
return
'''
Take in as input the create form for an author and
add the the author to the database.
'''
@route('/authors/add', method='POST')
def author_create():
# obtain the data for the author
first_name = request.forms.get('first_name')
last_name = request.forms.get('last_name')
# create the new author
Author(first_name=first_name, last_name=last_name)
# redirect the user to the author overview page
redirect("/authors")
'''
Edit authors information.
'''
@route('/authors/<id>/edit')
@view('authors/edit')
def author_edit(id):
# retrieve the author by id from the database
author = Author[id]
# set the data for the edit screen
return dict(author=author)
'''
Take in as input the edit form for an author and
update the database accordingly.
'''
@route('/authors/<id>/edit', method='POST')
def author_update(id):
# retrieve the author by id from the database
author = Author[id]
# updating the data of the author
author.first_name = request.forms.get('first_name')
author.last_name = request.forms.get('last_name')
# redirect the user to the author info page
redirect("/authors/%s/" % id)
'''
Delete author by id.
'''
@route('/authors/<id>/delete')
def author_delete(id):
# delete the author with the specified id from the database
Author[id].delete()
# redirect the user to the authors overview page
redirect("/authors")
'''
Search page for authors.
'''
@route('/authors/search')
@view('authors/search')
def author_search():
return
'''
Search authors by first name.
'''
@route('/authors/search/first', method="POST")
@view('authors/found')
def author_search_names():
# obtain the first name from the form
first_name = request.forms.get('first_name')
# filter only the authors with that first name
authors = Author.select(lambda a: a.first_name == first_name)
# set the data for the screen
return dict(authors=authors)
'''
Search authors by last name.
'''
@route('/authors/search/last', method="POST")
@view('authors/found')
def author_search_names():
# obtain the names of the author
last_name = request.forms.get('last_name')
# filter only the authors with that last name
authors = Author.select(lambda a: a.last_name == last_name)
# set the data for the screen
return dict(authors=authors)
##################
### Publishers ###
##################
'''
Show all the publishers in the system.
'''
@route('/publishers')
@view('publishers/index')
def publisher_all():
# select all the publishers from the db
publishers = select(p for p in Publisher)
# set the publishers for the template
return dict(publishers=publishers)
'''
Show information for specific publisher.
'''
@route('/publishers/<id>/')
@view('publishers/show')
def publisher_show(id):
# get the publisher by id
publisher = Publisher[id]
# set the publisher for the template
return dict(publisher=publisher)
'''
Add new publisher to the system.
'''
@route('/publishers/add')
@view('publishers/add')
def publisher_add():
return
'''
Take in as input the create form for a publisher and
add the the publisher to the database.
'''
@route('/publishers/add', method='POST')
def publisher_create():
# obtain the data for the publisher
name = request.forms.get('name')
country = request.forms.get('country')
# create the new publisher
Publisher(name=name, country=country)
# redirect the user to the publisher overview page
redirect("/publishers")
'''
Edit publisher information.
'''
@route('/publishers/<id>/edit')
@view('publishers/edit')
def publisher_edit(id):
# retrieve the publisher by id from the database
publisher = Publisher[id]
# set the data for the edit screen
return dict(publisher=publisher)
'''
Take in as input the edit form for a publisher and
update the database accordingly.
'''
@route('/publishers/<id>/edit', method='POST')
def publisher_update(id):
# retrieve the publisher by id from the database
publisher = Publisher[id]
# updating the data of the publisher
publisher.name = request.forms.get('name')
publisher.country = request.forms.get('country')
# redirect the user to the publisher info page
redirect("/publishers/%s/" % id)
'''
Delete publisher by id.
'''
@route('/publishers/<id>/delete')
def publisher_delete(id):
# delete the publisher with the specified id from the database
Publisher[id].delete()
# redirect the user to the publisher overview page
redirect("/publishers")
'''
Search page for publishers.
'''
@route('/publishers/search')
@view('publishers/search')
def publisher_search():
return
'''
Search publisher by name.
'''
@route('/publishers/search/name', method="POST")
@view('publishers/found')
def publisher_search_name():
# obtain the name of the publisher
name = request.forms.get('name')
# filter only the publishers with that name
publishers = Publisher.select(lambda p: p.name == name)
# set the data for the screen
return dict(publishers=publishers)
'''
Search publisher by country.
'''
@route('/publishers/search/country', method="POST")
@view('publishers/found')
def publisher_search_country():
# obtain the country of the publisher
country = request.forms.get('country')
# filter only the publishers with the specific country
publishers = Publisher.select(lambda p: p.country == country)
# set the data for the screen
return dict(publishers=publishers)
#############
### Genre ###
#############
'''
Show all the genres in the system.
'''
@route('/genres')
@view('genres/index')
def genre_all():
# select all the genres from the db
genres = select(g for g in Genre)
# set the genres for the template
return dict(genres=genres)
'''
Show information for specific genres.
'''
@route('/genres/<id>/')
@view('genres/show')
def genre_show(id):
# get the genre by id
genre = Genre[id]
# set the genre for the template
return dict(genre=genre)
'''
Add new genre to the system.
'''
@route('/genres/add')
@view('genres/add')
def genre_add():
return
'''
Take in as input the create form for a genre and
add the the genre to the database.
'''
@route('/genres/add', method='POST')
def genre_create():
# obtain the data for the genre
name = request.forms.get('name')
# create the new genre
Genre(name=name)
# redirect the user to the genre overview page
redirect("/genres")
'''
Edit genre information.
'''
@route('/genres/<id>/edit')
@view('genres/edit')
def genre_edit(id):
# retrieve the genre by id from the database
genre = Genre[id]
# set the data for the edit screen
return dict(genre=genre)
'''
Take in as input the edit form for a genre and
update the database accordingly.
'''
@route('/genres/<id>/edit', method='POST')
def genre_update(id):
# retrieve the genre by id from the database
genre = Genre[id]
# updating the data of the genre
genre.name = request.forms.get('name')
# redirect the user to the genre info page
redirect("/genres/%s/" % id)
'''
Delete genre by id.
'''
@route('/genres/<id>/delete')
def genre_delete(id):
# delete the genre with the specified id from the database
Genre[id].delete()
# redirect the user to the genre overview page
redirect("/genres")
'''
Search page for genres.
'''
@route('/genres/search')
@view('genres/search')
def genre_search():
return
'''
Search genre by name.
'''
@route('/genres/search/name', method="POST")
@view('genres/found')
def genre_search_name():
# obtain the name of the genre
name = request.forms.get('name')
# filter only the genres with that name
genres = Genre.select(lambda g: g.name == name)
# set the data for the screen
return dict(genres=genres)
#################
### Customers ###
#################
'''
Show all the customers in the system.
'''
@route('/customers')
@view('customers/index')
def customer_all():
# select all the customers from the db
customers = select(c for c in Customer)
# set the customers for the template
return dict(customers=customers)
'''
Show information for specific customers.
'''
@route('/customers/<id>/')
@view('customers/show')
def customer_show(id):
# get the customer by id
customer = Customer[id]
# set the customer for the template
return dict(customer=customer)
'''
Add new customer to the system.
'''
@route('/customers/add')
@view('customers/add')
def customer_add():
return
'''
Take in as input the create form for a customer and
add the the customer to the database.
'''
@route('/customers/add', method='POST')
def customer_create():
# obtain the data for the customer
first_name = request.forms.get('first_name')
last_name = request.forms.get('last_name')
phone_number = request.forms.get('phone_number')
address = request.forms.get('address')
city = request.forms.get('city')
country = request.forms.get('country')
# create the new customer
Customer(first_name=first_name,
last_name=last_name,
phone_number=phone_number,
address=address,
city=city,
country=country)
# redirect the user to the customers overview page
redirect("/customers")
'''
Edit customer information.
'''
@route('/customers/<id>/edit')
@view('customers/edit')
def customer_edit(id):
# retrieve the customer by id from the database
customer = Customer[id]
# set the data for the edit screen
return dict(customer=customer)
'''
Take in as input the edit form for a customer and
update the database accordingly.
'''
@route('/customers/<id>/edit', method='POST')
def customer_update(id):
# retrieve the customer by id from the database
customer = Customer[id]
# updating the data of the customer
customer.first_name = request.forms.get('first_name')
customer.last_name = request.forms.get('last_name')
customer.phone_number = request.forms.get('phone_number')
customer.address = request.forms.get('address')
customer.city = request.forms.get('city')
customer.country = request.forms.get('country')
# redirect the user to the customer info page
redirect("/customers/%s/" % id)
'''
Delete customer by id.
'''
@route('/customers/<id>/delete')
def customer_delete(id):
# delete the customer with the specified id from the database
Customer[id].delete()
# redirect the user to the customers overview page
redirect("/customers")
'''
Search page for customers.
'''
@route('/customers/search')
@view('customers/search')
def customer_search():
return
'''
Search customer by name.
'''
@route('/customers/search/name', method="POST")
@view('customers/found')
def customer_search_name():
# obtain the name of the customer
name = request.forms.get('name')
# filter only the customers with that name
customers = Customer.select(lambda c: c.first_name == name or c.last_name == name or c.first_name + " " + c.last_name == name)
# set the data for the screen
return dict(customers=customers)
'''
Search customer by country.
'''
@route('/customers/search/country', method="POST")
@view('customers/found')
def customer_search_country():
# obtain the country of the customer
country = request.forms.get('country')
# filter only the customers from that country
customers = Customer.select(lambda c: c.country == country)
# set the data for the screen
return dict(customers=customers)
##############
### Orders ###
##############
'''
Show all the orders in the system.
'''
@route('/orders')
@view('orders/index')
def order_all():
# select all the orders from the db
orders = select(o for o in Order)
# set the orders for the template
return dict(orders=orders)
'''
Show information for specific order.
'''
@route('/orders/<id>/')
@view('orders/show')
def orders_show(id):
# get the order by id
order = Order[id]
# set the order for the template
return dict(order=order)
'''
Add new order to the system.
'''
@route('/orders/add')
@view('orders/add')
def order_add():
# select all the customers from the db
customers = Customer.select()
# select all the books from the db
books = Book.select()
return dict(customers=customers, books=books)
'''
Take in as input the create form for an order and
add the the order to the database.
'''
@route('/orders/add', method='POST')
def order_create():
# obtain the data for the order
customer = request.forms.get('customer')
purchase_date = datetime.now().strftime("%Y-%m-%d")
books = request.forms.getlist('books')
# create the new order
Order(customer=customer,
purchase_date=purchase_date,
books=[Book[id] for id in books],
total=sum([Book[id].price for id in books]))
# redirect the user to the orders overview page
redirect("/orders")
'''
Edit order information.
'''
@route('/orders/<id>/edit')
@view('orders/edit')
def order_edit(id):
# retrieve the order by id from the database
order = Order[id]
# select all the customers from the db
customers = Customer.select()
# select all the books from the db
books = Book.select()
# set the data for the edit screen
return dict(order=order, customers=customers, books=books)
'''
Take in as input the edit form for an order and
update the database accordingly.
'''
@route('/orders/<id>/edit', method='POST')
def order_update(id):
# retrieve the order by id from the database
order = Order[id]
# updating the data of the order
order.customer = request.forms.get('customer')
order.purchase_date = request.forms.get('date')
order.books = [Book[id] for id in request.forms.getlist('books')]
order.total = sum([Book[id].price for id in request.forms.getlist('books')])
# redirect the user to the order info page
redirect("/orders/%s/" % id)
'''
Delete order by id.
'''
@route('/orders/<id>/delete')
def order_delete(id):
# delete the order with the specified id from the database
Order[id].delete()