1
1
import os
2
2
from functools import wraps
3
- from werkzeug import secure_filename
3
+ from werkzeug . utils import secure_filename
4
4
from flask import request , Blueprint , render_template , jsonify , flash , \
5
- redirect , url_for as flask_url_for , g , abort
6
- from my_app import db , app , ALLOWED_EXTENSIONS , babel
5
+ redirect , url_for as flask_url_for , g , abort , current_app
6
+ from my_app import db , ALLOWED_EXTENSIONS , babel
7
7
from my_app .catalog .models import Product , Category , ProductForm , CategoryForm
8
- from sqlalchemy .orm . util import join
8
+ from sqlalchemy .orm import join
9
9
from flask_babel import lazy_gettext as _
10
10
import geoip2 .database
11
11
from geoip2 .errors import AddressNotFoundError
14
14
catalog = Blueprint ('catalog' , __name__ )
15
15
16
16
17
- @app .before_request
17
+ @catalog .before_request
18
18
def before ():
19
19
if request .view_args and 'lang' in request .view_args :
20
20
g .current_lang = request .view_args ['lang' ]
21
21
request .view_args .pop ('lang' )
22
22
23
23
24
- @app .context_processor
24
+ @catalog .context_processor
25
25
def inject_url_for ():
26
26
return {
27
27
'url_for' : lambda endpoint , ** kwargs : flask_url_for (
@@ -33,11 +33,6 @@ def inject_url_for():
33
33
url_for = inject_url_for ()['url_for' ]
34
34
35
35
36
- @babel .localeselector
37
- def get_locale ():
38
- return g .get ('current_lang' , 'en' )
39
-
40
-
41
36
def template_or_json (template = None ):
42
37
""""Return a dict from your view and this will either
43
38
pass it to a template or render json. Use like:
@@ -48,7 +43,7 @@ def decorated(f):
48
43
@wraps (f )
49
44
def decorated_fn (* args , ** kwargs ):
50
45
ctx = f (* args , ** kwargs )
51
- if request .is_xhr or not template :
46
+ if request .headers . get ( "X-Requested-With" ) == "XMLHttpRequest" or not template :
52
47
return jsonify (ctx )
53
48
else :
54
49
return render_template (template , ** ctx )
@@ -61,9 +56,9 @@ def allowed_file(filename):
61
56
filename .lower ().rsplit ('.' , 1 )[1 ] in ALLOWED_EXTENSIONS
62
57
63
58
64
- @app .errorhandler (404 )
59
+ @catalog .errorhandler (404 )
65
60
def page_not_found (e ):
66
- app .logger .error (e )
61
+ current_app .logger .error (e )
67
62
return render_template ('404.html' ), 404
68
63
69
64
@@ -73,7 +68,7 @@ def page_not_found(e):
73
68
@template_or_json ('home.html' )
74
69
def home ():
75
70
products = Product .query .all ()
76
- app .logger .info (
71
+ current_app .logger .info (
77
72
'Home page with total of %d products' % len (products )
78
73
)
79
74
return {'count' : len (products )}
@@ -83,15 +78,15 @@ def home():
83
78
def product (id ):
84
79
product = Product .query .filter_by (id = id ).first ()
85
80
if not product :
86
- app .logger .warning ('Requested product not found.' )
81
+ current_app .logger .warning ('Requested product not found.' )
87
82
abort (404 )
88
83
return render_template ('product.html' , product = product )
89
84
90
85
91
86
@catalog .route ('/<lang>/products' )
92
87
@catalog .route ('/<lang>/products/<int:page>' )
93
88
def products (page = 1 ):
94
- products = Product .query .paginate (page , 10 )
89
+ products = Product .query .paginate (page = page , per_page = 10 )
95
90
return render_template ('products.html' , products = products )
96
91
97
92
@@ -110,25 +105,25 @@ def create_product():
110
105
if image and allowed_file (image .filename ):
111
106
filename = secure_filename (image .filename )
112
107
session = boto3 .Session (
113
- aws_access_key_id = app .config ['AWS_ACCESS_KEY' ],
114
- aws_secret_access_key = app .config ['AWS_SECRET_KEY' ]
108
+ aws_access_key_id = current_app .config ['AWS_ACCESS_KEY' ],
109
+ aws_secret_access_key = current_app .config ['AWS_SECRET_KEY' ]
115
110
)
116
111
s3 = session .resource ('s3' )
117
- bucket = s3 .Bucket (app .config ['AWS_BUCKET' ])
112
+ bucket = s3 .Bucket (current_app .config ['AWS_BUCKET' ])
118
113
if bucket not in list (s3 .buckets .all ()):
119
114
bucket = s3 .create_bucket (
120
- Bucket = app .config ['AWS_BUCKET' ],
115
+ Bucket = current_app .config ['AWS_BUCKET' ],
121
116
CreateBucketConfiguration = {
122
117
'LocationConstraint' : 'ap-south-1'
123
118
},
124
119
)
125
120
bucket .upload_fileobj (
126
121
image , filename ,
127
122
ExtraArgs = {'ACL' : 'public-read' })
128
- reader = geoip2 .database .Reader ('GeoLite2-City_20190416 /GeoLite2-City.mmdb' )
123
+ reader = geoip2 .database .Reader ('GeoLite2-City_20230113 /GeoLite2-City.mmdb' )
129
124
try :
130
125
match = reader .city (request .remote_addr )
131
- except AddressNotFoundError :
126
+ except geoip2 . errors . AddressNotFoundError :
132
127
match = None
133
128
134
129
product = Product (
@@ -168,7 +163,7 @@ def product_search(page=1):
168
163
Category .name .like ('%' + category + '%' )
169
164
)
170
165
return render_template (
171
- 'products.html' , products = products .paginate (page , 10 )
166
+ 'products.html' , products = products .paginate (page = page , per_page = 10 )
172
167
)
173
168
174
169
0 commit comments