-
Notifications
You must be signed in to change notification settings - Fork 0
/
games.py
212 lines (175 loc) · 7.01 KB
/
games.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
from app import db
from flask_wtf import FlaskForm
from wtforms import Form, TextField, SelectField, SubmitField, validators, ValidationError
from flask import (
Blueprint, flash, g, redirect, render_template, request, url_for
)
from werkzeug.exceptions import abort
from auth import login_required, User
from flask_table import Table, Col, LinkCol, ButtonCol, DatetimeCol
from flask_babelex import gettext, ngettext, lazy_gettext
from flask_user import current_user, login_required, roles_required
bp = Blueprint('games', __name__, url_prefix='/games')
game_categories=[('game', lazy_gettext(u'Game'))
]
class Game(db.Model):
__tablename__ = "games"
id = db.Column(db.Integer, primary_key = True)
title = db.Column(db.String, nullable = False )
publisher = db.Column(db.String, nullable = False)
isbn13 = db.Column(db.String(13), nullable = True)
# renter information's:
renter_name = db.Column(db.String, db.ForeignKey("users.username"), nullable =True)
rented_time = db.Column(db.DateTime, nullable = True)
renter = db.relationship("User", backref=db.backref("books", order_by = id), lazy = True)
category = db.Column(db.String, nullable = False)
class SearchForm(FlaskForm):
search = TextField('')
submit = SubmitField(lazy_gettext(u'Search'))
def validate_isbn10(form, field):
if field.data:
if len(field.data) != 10:
raise ValidationError(lazy_gettext(u'ISBN 10 must contain only numeric characters'))
if not field.data.isdigit():
raise ValidationError(lazy_gettext(u'ISBN 10 must contain only numeric characters'))
def validate_isbn13(form, field):
if field.data:
if len(field.data) != 13:
raise ValidationError(lazy_gettext(u'ISBN 13 must be 10 character long'))
if not field.data.isdigit():
raise ValidationError(lazy_gettext(u'ISBN 13 must contain only numeric characters'))
class GameForm(FlaskForm):
title = TextField(lazy_gettext(u'Title'),[validators.InputRequired()])
publisher = TextField(lazy_gettext(u'Publisher'),[validators.InputRequired()])
author = TextField(lazy_gettext(u'Author'))
isbn10 = TextField('ISBN 10',[validate_isbn10])
isbn13 = TextField('ISBN 13',[validate_isbn13])
category = SelectField(lazy_gettext(u'Category'), choices=book_categories)
submit = SubmitField(lazy_gettext(u'Save'))
@bp.route('/add', methods=['GET','POST'])
@login_required
def new_book():
"""
Add a new book to the database
"""
form = BookForm()
if form.validate_on_submit():
# save the album
book = Book()
save_changes(book, form, new=True)
flash(lazy_gettext(u'Book created successfully!'),'success')
return redirect('/')
return render_template('books/new.html', form=form)
def save_changes( book, form, new = False):
"""
Save the changes to a given book
"""
import datetime
book.title = form.title.data
book.publisher = form.publisher.data
book.author = form.author.data
book.isbn10 = form.isbn10.data
book.isbn13 = form.isbn13.data
book.renter_id = ''
book.rented_time = None
book.category = form.category.data
if new:
# Add the book to the database
db.session.add(book)
db.session.commit()
class CategoryCol(Col):
def td_format(self,content):
values=dict(book_categories)
if values[content] is not None:
return values[content]
else:
return lazy_gettext(u'Unknown Category')
class BookResults(Table):
classes = ['table']
id = Col('Id', show=False)
title = Col(lazy_gettext(u'Title'))
publisher = Col(lazy_gettext(u'Publisher'))
author = Col(lazy_gettext(u'Author'))
isbn10 = Col('ISBN 10')
isbn13 = Col('ISBN 13')
category = CategoryCol(lazy_gettext(u'Category'))
renter_name = Col(lazy_gettext(u'Renter'))
rented_time = DatetimeCol(lazy_gettext(u'Rented time'))
rent = ButtonCol(lazy_gettext(u'Rent / Give Back'), '.rent', url_kwargs=dict(id='id'))
@bp.route('/', methods=['GET', 'POST'])
@login_required
def index():
results = Book.query.all()
table = BookResults(results, no_items=lazy_gettext(u'No books in the database'))
if current_user.has_roles('Admin'):
table.add_column('edit', LinkCol(lazy_gettext(u'Edit'),'.edit',url_kwargs=dict(id='id')))
print(table)
return render_template('books/index.html', table=table)
@bp.route('/search', methods=['GET', 'POST'])
@login_required
def search():
search = BookSearchForm()
if search.validate_on_submit():
return search_results(search)
return render_template('books/search.html', form=search)
@bp.route('/results')
@login_required
def search_results (search):
results = []
search_string = search.search.data
if search_string:
import re
search_string_list = re.sub("[^\W]"," ",search_string).split()
qry = db.session.query(Book).filter(Book.title == search_string)
results = qry.all()
else:
qry = db.session.query(Book)
results = qry.all()
if not results:
flash(lazy_gettext(u'No results found!'),'info')
return redirect(url_for('search'))
else:
# display results
table = BookResults(results)
if current_user.has_roles('Admin'):
table.add_column('edit', LinkCol(lazy_gettext(u'Edit'),'.edit',url_kwargs=dict(id='id')))
table.border = True
return render_template('books/results.html', table=table, form=search)
@bp.route('/item/<int:id>', methods=['GET', 'POST'])
@roles_required('Admin')
def edit(id):
qry = db.session.query(Book).filter(Book.id==id)
book = qry.first()
if book:
form = BookForm(formdata=request.form, obj=book)
if request.method == 'POST' and form.validate():
# Save modifications
save_changes(book, form)
flash(lazy_gettext(u'Book updated successfully!'),'success')
return redirect('/')
return render_template('books/edit_book.html', form=form)
else:
flash(lazy_gettext(u'ERROR Book #{id} doesn''t exist').format(id=id))
return redirect(url_for('.index'))
@bp.route('/rent_item/<int:id>', methods=['GET', 'POST'])
@login_required
def rent(id):
qry = db.session.query(Book).filter(Book.id==id)
book = qry.first()
if book:
import datetime
if book.renter_name == current_user.username:
book.renter_name = None
book.rented_time = None
db.session.commit()
elif book.renter_name is None:
qry = db.session.query(User).filter(User.id==current_user.id)
user = qry.first()
book.renter_name = user.username
book.rented_time = datetime.datetime.now()
db.session.commit()
else:
flash(lazy_gettext(u'ERROR Book {title} is already rented by someone else.').format(title=book.title))
else:
flash(lazy_gettext(u'ERROR Book #{id} doesn''t exist').format(id=id))
return redirect('/')