-
Notifications
You must be signed in to change notification settings - Fork 1
/
mruf.py
1308 lines (1065 loc) · 38 KB
/
mruf.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
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 flask import Flask
from flask import request, session, redirect, url_for, render_template, g
from flask import abort, flash
from flask_sqlalchemy import SQLAlchemy
from decimal import Decimal
import sqlalchemy
import string
import random
import datetime
import parsedatetime
import time
import requests
import re
import pytz
import json
from collections import OrderedDict, defaultdict
import csv
import hashlib
import binascii
# Python 2/3 compatibility.
try:
from urlparse import urlparse, urljoin, parse_qs
from urllib import urlencode
except ImportError:
from urllib.parse import urlparse, urljoin, parse_qs
from urllib.parse import urlencode
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
# Use PBKDF2 from the standard library if available, or fall back to the
# PyPI module.
if hasattr(hashlib, 'pbkdf2_hmac'):
HAVE_HASHLIB_PBKDF2 = True
else:
HAVE_HASHLIB_PBKDF2 = False
import pbkdf2
# The Flask application and its configuration.
app = Flask(__name__)
app.config.from_pyfile('mruf.base.cfg')
app.config.from_pyfile('mruf.site.cfg', silent=True)
app.config.from_envvar('MRUF_CFG', silent=True)
db = SQLAlchemy(app)
# Useful Flask snippets.
# http://flask.pocoo.org/snippets/38/
class MethodRewriteMiddleware(object):
"""Middleware emulating non-GET/POST HTTP request methods from
browsers that don't support them for Ajax calls. The frontend sets a
special key in a POST request indicating which method it wants to
use. We use this for DELETE requests.
"""
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
if 'METHOD_OVERRIDE' in environ.get('QUERY_STRING', ''):
args = parse_qs(environ['QUERY_STRING'])
method = args.get('__METHOD_OVERRIDE__')
if method:
environ['REQUEST_METHOD'] = method[0]
return self.app(environ, start_response)
app.wsgi_app = MethodRewriteMiddleware(app.wsgi_app) # noqa
# http://flask.pocoo.org/snippets/62/
def is_safe_url(target):
"""Ensure that a URL is safe for redirection.
"""
ref_url = urlparse(request.host_url)
test_url = urlparse(urljoin(request.host_url, target))
return test_url.scheme in ('http', 'https') and \
ref_url.netloc == test_url.netloc
# Utilities.
def _hash_pass(password):
if not isinstance(password, bytes):
password = password.encode('utf8')
if HAVE_HASHLIB_PBKDF2:
hash = hashlib.pbkdf2_hmac('sha1', password, app.config['SALT'],
1000, 24)
return binascii.hexlify(hash).decode('ascii')
else:
return pbkdf2.pbkdf2_hex(password, app.config['SALT'])
def _parse_price(s):
if s.startswith('$'):
s = s[1:]
elif s.startswith('-$'):
s = '-' + s[2:]
return Decimal(s).quantize(Decimal('1.00'))
_calendar = parsedatetime.Calendar()
def _parse_dt(s):
"""Parse a (potentially human-written) string indicating a date and
time to a Python `datetime` object.
"""
ts, _ = _calendar.parse(s)
zone = pytz.timezone(g.state['timezone'])
naivedt = datetime.datetime.fromtimestamp(time.mktime(ts))
localdt = zone.localize(naivedt)
return _normdt(localdt)
def _now():
"""Get a `datetime` object reflecting the current time.
"""
return datetime.datetime.utcnow()
def _normdt(dt):
"""Normalize a `datetime` object's timezone. This lets us force all
timestamps used internally to UTC, avoiding lots of weird timezone
issues.
"""
if dt.tzinfo is None:
return dt
return dt.astimezone(pytz.utc).replace(tzinfo=None)
def _random_string(length=20, chars=(string.ascii_letters + string.digits)):
return ''.join(random.choice(chars) for i in range(length))
def _next_url():
if 'next' in request.values:
dest = request.values['next']
if is_safe_url(dest):
return dest
return url_for('main')
# Email.
def mailgun_request(apikey, path, data, base='https://api.mailgun.net/v2'):
req = requests.post(
'{}/{}'.format(base, path),
data=data,
auth=requests.auth.HTTPBasicAuth('api', apikey)
)
return req.json()
def mailgun_send(apikey, domain, from_addr, to_addrs, subject, body,
cc_addrs=(), bcc_addrs=()):
data = {
'from': from_addr,
'to': ','.join(to_addrs),
'subject': subject,
'text': body,
}
if cc_addrs:
data['cc'] = ','.join(cc_addrs)
if bcc_addrs:
data['bcc'] = ','.join(bcc_addrs)
return mailgun_request(apikey, '{}/messages'.format(domain), data)
def send_email(to_addrs, subject, body, cc_addrs=(), bcc_addrs=()):
"""Send an email.
"""
return mailgun_send(
app.config['MAILGUN_API_KEY'],
app.config['MAILGUN_DOMAIN'],
g.state['mail_from'],
to_addrs, subject, body, cc_addrs, bcc_addrs,
)
def send_receipt(order):
"""Send an order receipt email using templates from the database.
"""
farmer_addrs = [u.email for u in User.query.filter_by(admin=True)]
send_email(
[order.customer.email],
g.state['receipt_subject'],
g.state['receipt_body'].format(
name=order.customer.name,
receipt_url=url_for('receipt', order_id=order.id, _external=True),
farm=g.state['farm'],
),
bcc_addrs=farmer_addrs,
)
# Photos.
def flickr_image_url(apikey, photoid, label,
base='https://api.flickr.com/services/rest/'):
req = requests.get(base, params={
'method': 'flickr.photos.getSizes',
'api_key': apikey,
'photo_id': str(photoid),
'format': 'json',
'nojsoncallback': '1',
})
sizes = req.json()['sizes']['size']
for size in sizes:
if size['label'] == label:
return size['source']
def thumbnail_url(url):
match = re.search(r'flickr\.com/photos/[^/\?]+/(\d+)', url, re.I)
if match:
return flickr_image_url(
app.config['FLICKR_API_KEY'],
match.group(1),
'Square'
)
# SQLAchemy types.
class IntegerDecimal(sqlalchemy.types.TypeDecorator):
impl = sqlalchemy.types.Integer
_unitsize = 100
def process_bind_param(self, value, dialect):
value = Decimal(value)
return int(value * self._unitsize)
def process_result_value(self, value, dialect):
return Decimal(value) / self._unitsize
class JSONEncodedDict(sqlalchemy.types.TypeDecorator):
"""Represents an immutable structure as a json-encoded string.
"""
impl = sqlalchemy.types.Text
def process_bind_param(self, value, dialect):
if value is not None:
value = json.dumps(value)
return value
def process_result_value(self, value, dialect):
if value is not None:
value = json.loads(value)
return value
# Models.
class SettingsMixin(object):
"""A mixin class for SQLAlchemy Model classes that provides a
JSON-encoded dictionary of free-form *settings*. These values are
accessed by subscripting the model object.
"""
settings = db.Column(JSONEncodedDict())
settings_default = {}
def __init__(self):
self.settings = dict(self.settings_default)
def __getitem__(self, key):
if self.settings is not None and key in self.settings:
return self.settings[key]
else:
if key in self.settings_default:
return self.settings_default.get(key)
else:
raise KeyError(key)
def __setitem__(self, key, value):
self.update({key: value})
def update(self, mapping):
if self.settings is None:
new_settings = {}
else:
new_settings = dict(self.settings)
new_settings.update(mapping)
self.settings = new_settings
class AutoincrementMixin(object):
"""A mixin for SQLAlchemy models that makes SQLite id columns
auto-increment (i.e., avoid resuing unique ids).
"""
__table_args__ = {'sqlite_autoincrement': True}
class User(db.Model, SettingsMixin, AutoincrementMixin):
"""A User can either be a customer or a farmer (farmers have
administrative access).
"""
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.Unicode(256), unique=True)
name = db.Column(db.Unicode(512))
password = db.Column(db.String(256))
admin = db.Column(db.Boolean)
delivery_notes = db.Column(db.UnicodeText)
settings_default = {
'fruit': False,
}
def __init__(self, email, name, password, admin):
SettingsMixin.__init__(self)
self.email = email
self.name = name
self.password = _hash_pass(password)
self.admin = admin
def __repr__(self):
return '<User {0}>'.format(self.email)
@property
def account_history(self):
"""Get a chronologically ordered sequence of Order and
CreditDebit objects for this user.
"""
out = list(self.transactions) + list(self.orders)
out.sort(key=lambda o: o.date if isinstance(o, CreditDebit)
else o.placed)
return out
@property
def balance(self):
bal = Decimal('0.00')
for txn in self.transactions:
bal += txn.amount
for order in self.orders:
bal -= order.total
return bal
class Order(db.Model, AutoincrementMixin):
"""An `Order` is a collection of `OrderItem` instances along with a
timestamps and a reference to the user who placed the order.
"""
id = db.Column(db.Integer, primary_key=True)
customer_id = db.Column(db.Integer, db.ForeignKey('user.id'))
customer = db.relationship('User',
backref=db.backref('orders', lazy='dynamic'))
placed = db.Column(db.DateTime)
harvested = db.Column(db.DateTime)
def __init__(self, customer):
self.customer = customer
self.placed = _now()
self.harvested = g.state.next_harvest
def __repr__(self):
return '<Order {0} for {1}>'.format(self.id, self.customer.email)
@property
def total(self):
"""The total cost of this order.
"""
total = Decimal('0.00')
for item in self.items:
total += item.cost
return total
@property
def items_by_product(self):
"""A dictionary mapping `Product` instances to `OrderItem`
instances for that product.
"""
out = {}
for item in self.items:
out[item.product] = item
return out
class OrderItem(db.Model, AutoincrementMixin):
"""A single "row" of an `Order`, representing a customer's request
for one particular product.
Includes the quantity as well as the price at the time the order was
placed. Caching the price in this way lets the price change later without
affecting the total for orders placed in the past.
"""
id = db.Column(db.Integer, primary_key=True)
order_id = db.Column(db.Integer, db.ForeignKey('order.id'))
order = db.relationship('Order', backref='items')
product_id = db.Column(db.Integer, db.ForeignKey('product.id'))
product = db.relationship('Product',
backref=db.backref('ordered', lazy='dynamic'))
count = db.Column(db.Integer)
price = db.Column(IntegerDecimal)
def __init__(self, order, product, count):
self.order = order
self.product = product
self.count = count
self.price = product.price
@property
def cost(self):
return self.count * self.price
class Product(db.Model, AutoincrementMixin):
"""An item for sale in the store.
"""
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Unicode(256))
price = db.Column(IntegerDecimal)
available = db.Column(db.Boolean)
link = db.Column(db.UnicodeText())
photo = db.Column(db.UnicodeText())
def __init__(self, name, price, link):
self.name = name
self.price = price
self.link = link
self.infer_photo()
self.order_by = None
def __repr__(self):
return '<Product {0}>'.format(self.name)
def infer_photo(self):
"""Using this product's link, try to guess a photo URL and set
it.
"""
if self.link:
self.photo = thumbnail_url(self.link)
class State(db.Model, SettingsMixin, AutoincrementMixin):
"""A singleton model reflecting the site's settings.
Most settings are packed in a JSON-encoded dictionary. These values
are read and written via item access (`state[key]`). The site's next
harvest date is the exception.
"""
id = db.Column(db.Integer, primary_key=True)
next_harvest = db.Column(db.DateTime())
settings_default = app.config['DEFAULT_SETTINGS']
def __init__(self):
SettingsMixin.__init__(self)
self.next_harvest = _now()
# `settings` is a JSON-encoded immutable dict. This could be made
# lazy using SQLAlchemy's mutation tracking; for the moment, it is
# eager and therefore probably inefficient.
@property
def open(self):
if self.next_harvest is None:
return False
return _normdt(self.next_harvest) > _now()
@property
def location_map(self):
"""An OrderedDict that maps short location names to long names.
"""
out = OrderedDict()
for line in self['locations'].strip().split('\n'):
line = line.strip()
short = re.sub(r'\([^\)]*\)', '', line).strip()
out[short] = line
return out
class CreditDebit(db.Model, AutoincrementMixin):
"""An addition or subtraction a customer's account.
A credit/debit is *not* tied to an order---those are charged
separately. These are used when a customer makes a deposit into
their account, for example, and are created explicitly by farmers.
"""
id = db.Column(db.Integer, primary_key=True)
customer_id = db.Column(db.Integer, db.ForeignKey('user.id'))
customer = db.relationship(
'User',
backref=db.backref('transactions', lazy='dynamic'),
)
amount = db.Column(IntegerDecimal)
date = db.Column(db.DateTime)
description = db.Column(db.UnicodeText())
def __init__(self, customer, amount, description):
self.customer = customer
self.amount = amount
self.date = _now()
self.description = description
# Hooks.
@app.before_first_request
def db_setup():
"""Ensure that schema and initial data is present: create tables,
add an initial user, and create the state object.
"""
db.create_all()
if not State.query.first():
db.session.add(State())
if not User.query.first():
# Create an administrator (farmer) who can create other initial
# accounts.
db.session.add(User(
app.config['INITIAL_USER_EMAIL'],
app.config['INITIAL_USER_NAME'],
app.config['INITIAL_USER_PASSWORD'],
True,
))
db.session.commit()
@app.before_request
def _load_globals():
"""Get the session's user and the global state object for the
request.
"""
if 'userid' in session:
g.user = User.query.filter_by(id=session['userid']).first()
if g.user is not None:
g.admin = g.user.admin
else:
g.user = None
g.admin = False
g.state = State.query.first()
# Jinja2 template elements.
@app.template_filter('price')
def _price_filter(value):
"""A template filter for formatting prices in dollars and cents.
"""
negative = value < 0
if negative:
value = 0 - value
value = Decimal(value).quantize(Decimal('1.00'))
out = u'${0}'.format(value)
if negative:
out = u'-' + out
return out
@app.template_filter('pennies')
def _pennies_filter(value):
"""A template filter that formats a price as an integer number of
pennies. This is useful for plumbing when communicating prices to
the JavaScript frontend without resorting to floating-point.
"""
return u'{}'.format(int(value * 100))
def _unpad(s):
"""Remove padding zeroes from a formatted date string."""
return re.sub(r'(^|\s)0+', r'\1', s)
@app.template_filter('dt')
def _datetime_filter(value, withtime=False):
"""Format a `datetime` object as a human-readable string. `withtime`
indicates whether this should be just a day or a day with a time.
"""
if value is None:
return ''
if not value.tzinfo:
value = pytz.utc.localize(value)
value = value.astimezone(pytz.timezone(g.state['timezone']))
fmt = '%B %d, %Y'
if withtime:
fmt += ', %I:%M %p'
return _unpad(value.strftime(fmt))
# Data access helpers.
def all_harvests():
"""Get a list of harvest dates for which orders have been placed.
"""
res = db.session.query(sqlalchemy.distinct(Order.harvested)) \
.filter(Order.harvested is not None) \
.order_by(Order.harvested) \
.all()
return [r[0] for r in res]
def all_transactions():
"""Get a list of all Order and CreditDebit objects on the entire
site, sorted reverse chronologically.
"""
txns = Order.query.all() + CreditDebit.query.all()
txns.sort(
key=lambda o: o.date if isinstance(o, CreditDebit) else o.placed,
reverse=True,
)
return txns
# Authentication decorators.
def administrative(func):
"""Decorator for pages accessible only to administrators."""
def wrapped(*args, **kwargs):
if not g.admin:
abort(403)
return func(*args, **kwargs)
wrapped.__name__ = func.__name__
return wrapped
def authenticated(func):
"""Decorator for pages accessible only when logged in."""
def wrapped(*args, **kwargs):
if g.user is None:
return redirect('{}?{}'.format(
url_for('main'),
urlencode({'next': request.url}),
))
return func(*args, **kwargs)
wrapped.__name__ = func.__name__
return wrapped
# The routes themselves.
@app.route("/")
def main():
"""The home page. Either show the login form or redirect to a
relevant front page for the logged-in user.
"""
if g.user:
if g.user.admin:
return redirect(url_for('availability'))
else:
return redirect(url_for('order'))
else:
return render_template('login.html', next=_next_url())
@app.route("/login", methods=['POST'])
def login():
"""Handle a login form request. Show the login page on failure or
redirect to an appropriate destination on success.
"""
email = request.form['email']
password = request.form['password']
user = User.query.filter(
db.func.lower(User.email) == email.strip().lower()
).first()
if user is not None and user.password == _hash_pass(password):
# Successful login.
session['userid'] = user.id
session.permanent = True
return redirect(_next_url())
else:
# Login failed.
flash('Please try again.', 'error')
return render_template('login.html')
@app.route("/logout")
def logout():
"""Handle a logout request and redirect home.
"""
if 'userid' in session:
del session['userid']
session.permanent = False
return redirect(url_for('main'))
@app.route("/register", methods=['POST'])
def register():
"""Handle an account registration request.
At the moment, this just sends an email to the farmers asking them
to set up an account.
"""
name = request.form['name']
email = request.form['email']
# Send email to farmers.
subs = {
'name': name,
'email': email,
'farm': g.state['farm'],
'url': url_for('customers', _external=True),
}
send_email(
[u.email for u in User.query.filter_by(admin=True)],
g.state['register_subject'].format(**subs),
g.state['register_body'].format(**subs),
)
flash(g.state['register_success'], 'success')
return render_template('login.html')
@app.route("/reset", methods=['POST'])
def reset_send():
"""Handle a request to reset a password.
This creates a magic, expiring link for the reset and sends it via
email.
"""
email = request.form['email'].strip().lower()
user = User.query.filter(db.func.lower(User.email) == email).first()
# If the user exists, create the link and send the email.
if user:
token = _random_string(16)
user['reset_token'] = token
user['reset_time'] = time.time()
db.session.commit()
app.logger.info('sending reset email to %s', email)
reset_url = url_for('reset', user_id=user.id, token=token,
_external=True)
send_email(
[user.email],
app.config['RESET_SUBJECT'],
app.config['RESET_BODY'].format(
name=user.name,
farm=g.state['farm'],
reset_url=reset_url,
),
)
flash('We sent a password reset email to {}.'.format(email),
'success')
else:
app.logger.info('reset requested for non-existent user %s', email)
flash("We couldn't find {} in our records.".format(email),
'error')
return redirect(url_for('main'))
@app.route("/reset/<int:user_id>/<token>", methods=['GET'])
def reset(user_id, token):
"""Handle a magic reset link and actually reset the user's password.
"""
user = User.query.get_or_404(user_id)
if not user['reset_token'] or not user['reset_time']:
app.logger.info('user does not have a reset token')
abort(404)
if user['reset_token'] != token:
app.logger.info('incorrect reset token')
abort(404)
if time.time() - user['reset_time'] > app.config['RESET_EXPIRATION']:
app.logger.info('reset token has expired')
abort(404)
# Log the user in.
session['userid'] = user.id
session.permanent = True
# "Consume" the reset token.
user['reset_token'] = None
db.session.commit()
# Redirect to the profile page so they can reset their password.
flash('You can reset your password on this page.', 'success')
return redirect(url_for('customer', user_id=user.id))
@app.route("/products", methods=['GET', 'POST'])
def products():
"""Show or add to the list of available products.
"""
if g.admin:
if request.method == 'POST':
product = Product(
request.form['name'],
_parse_price(request.form['price']),
request.form['link'],
)
db.session.add(product)
db.session.commit()
products = Product.query.order_by(Product.name).all()
return render_template('products.html', products=products)
else:
products = Product.query.filter_by(available=True). \
order_by(Product.name).all()
return render_template(
'product_preview.html',
products=products,
)
@app.route("/products/<int:product_id>", methods=['POST', 'DELETE'])
@administrative
def product(product_id):
"""Update or delete an existing product.
"""
product = Product.query.get_or_404(product_id)
if request.method == 'POST':
product.name = request.form['name']
product.price = _parse_price(request.form['price'])
product.link = request.form['link']
product.infer_photo()
db.session.commit()
elif request.method == 'DELETE':
db.session.delete(product)
db.session.commit()
return redirect(url_for('products'))
def _product_info(orders):
"""For a list of orders, get information about every product there
is at least one order for. Return a list of (product, items, total)
triples containing the Product, a list of OrderItems reflecting the
orders, and the total number of that product ordered across all
orders.
"""
# We currently do this filtering "manually," although there is
# almost certainly a real query for this.
order_ids = [o.id for o in orders]
product_info = []
for product in Product.query.order_by(Product.name):
items = product.ordered.filter(OrderItem.order_id.in_(order_ids)).all()
if items:
total = sum(item.count for item in items)
product_info.append((product, items, total))
return product_info
def _product_info_by_location(orders):
"""Get total product information broken down by all delivery
destinations in the set of orders.
"""
orders_by_loc = defaultdict(list)
for o in orders:
orders_by_loc[o.customer.delivery_notes].append(o)
return {
loc: _product_info(os)
for loc, os in orders_by_loc.items()
}
@administrative
def _show_harvest(dt):
"""Helper: render a page depicting the products harvested at a
particular timestamp.
"""
orders = Order.query.filter_by(harvested=dt).all()
product_info = _product_info(orders)
location_info = _product_info_by_location(orders)
return render_template('harvest.html',
orders=orders,
product_info=product_info,
location_info=location_info,
harvestdt=dt)
def _get_harvest(year, month, day):
"""Get a harvest timestamp given a date. Abort with a 404 error if
no such harvest exists.
"""
target = datetime.date(year, month, day)
for harvest in all_harvests():
if harvest.date() == target:
return harvest
abort(404)
@app.route("/harvests/latest")
def latest_harvest():
"""Show the products harvested most recently.
"""
harvests = all_harvests()
if not harvests:
abort(404)
return _show_harvest(harvests[-1])
@app.route("/harvests/<int:year>-<int:month>-<int:day>")
def harvest(year, month, day):
"""Show the products harvested on a particular day.
"""
return _show_harvest(_get_harvest(year, month, day))
@app.route("/harvests/<int:year>-<int:month>-<int:day>.csv")
@administrative
def harvest_csv(year, month, day):
"""Get a CSV file containing total order information.
"""
harvest = _get_harvest(year, month, day)
orders = Order.query.filter_by(harvested=harvest).all()
product_info = _product_info(orders)
# Generate a CSV file.
output = StringIO()
writer = csv.writer(output)
writer.writerow(('Product', 'Total'))
for product, items, total in product_info:
writer.writerow((product.name, total))
csv_data = output.getvalue()
return csv_data, 200, {'Content-Type': 'text/csv'}
@app.route("/harvests/<int:year>-<int:month>-<int:day>/delivery.csv")
@administrative
def delivery_csv(year, month, day):
"""Get a CSV file with delivery addresses.
"""
harvest = _get_harvest(year, month, day)
orders = [o for o in Order.query.filter_by(harvested=harvest).all()
if app.config['DELIVERY_OPTION'] in o.customer.delivery_notes]
# Generate a CSV file.
output = StringIO()
writer = csv.writer(output)
writer.writerow(('Customer', 'Address'))
for order in orders:
writer.writerow((order.customer.name, order.customer['address']))
csv_data = output.getvalue()
return csv_data, 200, {'Content-Type': 'text/csv'}
@app.route("/harvests/<int:year>-<int:month>-<int:day>/orders")
@administrative
def harvest_orders(year, month, day):
"""Get a list of orders suitable for printing as packing slips.
"""
harvest = _get_harvest(year, month, day)
orders = Order.query.filter_by(harvested=harvest).all()
return render_template('harvest_orders.html',
orders=orders,
harvestdt=harvest)
@app.route("/harvests")
@administrative
def harvests():
"""Show a list of harvest dates with links to details.
"""
return render_template('harvests.html', harvests=all_harvests())
def _order_counts():
"""Get a dictionary mapping Products to integer counts from the
order form.
"""
out = {}
prefix = app.config['ORDER_COUNT_PREFIX']
for key, value in request.form.items():
if key.startswith(prefix) and value:
product_id = int(key[len(prefix):])
product = Product.query.filter_by(id=product_id).first()
count = int(value)
if count:
out[product] = int(value)
return out
def _place_order(user):
"""Display or handle an order form.
Used for both customer- and farmer-initiated orders.
"""
if request.method == 'GET':
previous_order = None
for order in user.orders:
if order.harvested == g.state.next_harvest:
previous_order = order
break
if previous_order:
return render_template('already_ordered.html', order=order)
else:
products = Product.query.filter_by(available=True). \
order_by(Product.name)
return render_template(
'order.html',
products=products,
user=user,
)
# Create a new order.
order = Order(user)
db.session.add(order)
for product, count in _order_counts().items():