Commit cffc26d0d9e198edb51c359a04b3b40eb26a6df3
Fix PEP8 styles. Fix wrong info in README.
- Fix PEP8 in most python files in classes.
- Fix README saying '--recursive' in git clone.
| | | | 82 | | 82 | |
---|
83 | * Clone the repository from <https://git.pantoto.org/sweet-web/sweet-web-engine> | 83 | * Clone the repository from <https://git.pantoto.org/sweet-web/sweet-web-engine> |
---|
84 | | 84 | |
---|
85 | > `` $ git clone --recursive https://git.pantoto.org/sweet-web/sweet-web-engine.git `` | | > `` $ git clone --recursive https://git.pantoto.org/sweet-web/sweet-web-engine.git `` |
---|
| | 85 | > `` $ git clone https://git.pantoto.org/sweet-web/sweet-web-engine.git `` | 86 | | 86 | |
---|
87 | * It is recommended to do the installation inside a python virtual | 87 | * It is recommended to do the installation inside a python virtual |
---|
88 | environment. | 88 | environment. |
---|
| | | | 3 | | 3 | |
---|
4 | # Script to run the application server in development mode | 4 | # Script to run the application server in development mode |
---|
5 | | 5 | |
---|
6 | import sys, os | | import sys, os |
---|
| | 6 | import sys | | | 7 | import os |
---|
7 | | 8 | |
---|
8 | # Get the path to the base directory of the app | 9 | # Get the path to the base directory of the app |
---|
9 | BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__))) | 10 | BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__))) |
---|
| | | | 3 | __init__.py | 3 | __init__.py |
---|
4 | """ | 4 | """ |
---|
5 | | 5 | |
---|
6 | from flask import Flask, request, jsonify, render_template, make_response, g | | from flask import Flask, request, jsonify, render_template, make_response, g |
---|
7 | import os | 6 | import os |
---|
8 | import logging | 7 | import logging |
---|
9 | from logging.handlers import RotatingFileHandler | 8 | from logging.handlers import RotatingFileHandler |
---|
10 | | 9 | |
---|
| | 10 | from flask import Flask, request, jsonify, render_template, make_response |
---|
| | 11 | |
---|
11 | from classes.database import db | 12 | from classes.database import db |
---|
12 | from config import DefaultConfig | 13 | from config import DefaultConfig |
---|
13 | from classes import views | 14 | from classes import views |
---|
… | | … | |
---|
71 | db.app = app | 71 | db.app = app |
---|
72 | oauth.init_app(app) | 72 | oauth.init_app(app) |
---|
73 | | 73 | |
---|
| | 74 | |
---|
74 | # return the current db instance | 75 | # return the current db instance |
---|
75 | # TODO: is this needed so much? | 76 | # TODO: is this needed so much? |
---|
76 | def getDBInstance(): | 77 | def getDBInstance(): |
---|
… | | … | |
---|
165 | log_handler = RotatingFileHandler(log_file, maxBytes=max_size, | 165 | log_handler = RotatingFileHandler(log_file, maxBytes=max_size, |
---|
166 | backupCount=10) | 166 | backupCount=10) |
---|
167 | | 167 | |
---|
168 | if app.config.has_key('LOG_LEVEL'): | | if app.config.has_key('LOG_LEVEL'): |
---|
| | 168 | if 'LOG_LEVEL' in app.config: | 169 | log_level = app.config['LOG_LEVEL'] or 'ERROR' | 169 | log_level = app.config['LOG_LEVEL'] or 'ERROR' |
---|
170 | else: | 170 | else: |
---|
171 | log_level = 'ERROR' | 171 | log_level = 'ERROR' |
---|
| | | | 2 | """ | 2 | """ |
---|
3 | __init__.py | 3 | __init__.py |
---|
4 | """ | 4 | """ |
---|
| | 5 | |
---|
5 | from database import db | 6 | from database import db |
---|
6 | from oauth import oauth | 7 | from oauth import oauth |
---|
7 | import models | 8 | import models |
---|
| | | | 3 | | 3 | |
---|
4 | from sqlalchemy.exc import DontWrapMixin | 4 | from sqlalchemy.exc import DontWrapMixin |
---|
5 | | 5 | |
---|
| | 6 | |
---|
6 | class AlreadyExistsError(Exception, DontWrapMixin): | 7 | class AlreadyExistsError(Exception, DontWrapMixin): |
---|
7 | pass | 8 | pass |
---|
8 | | 9 | |
---|
| | 10 | |
---|
9 | class InvalidPayload(Exception, DontWrapMixin): | 11 | class InvalidPayload(Exception, DontWrapMixin): |
---|
| | 12 | pass |
---|
| | 13 | |
---|
| | 14 | |
---|
| | 15 | class ContextDoNotExist(Exception, DontWrapMixin): |
---|
10 | pass | 16 | pass |
---|
| | | | 36 | _redirect_uris = db.Column(db.Text) | 36 | _redirect_uris = db.Column(db.Text) |
---|
37 | _default_scopes = db.Column(db.Text) | 37 | _default_scopes = db.Column(db.Text) |
---|
38 | | 38 | |
---|
39 | | | |
---|
40 | @property | 39 | @property |
---|
41 | def client_id(self): | 40 | def client_id(self): |
---|
42 | return self.id | 41 | return self.id |
---|
… | | … | |
---|
72 | def __str__(self): | 72 | def __str__(self): |
---|
73 | return '<Client: %s :: ID: %s>' % (self.name, self.id) | 73 | return '<Client: %s :: ID: %s>' % (self.name, self.id) |
---|
74 | | 74 | |
---|
75 | | | |
---|
76 | # create and persist the client to the database | 75 | # create and persist the client to the database |
---|
77 | def persist(self): | 76 | def persist(self): |
---|
78 | db.session.add(self) | 77 | db.session.add(self) |
---|
… | | … | |
---|
153 | #TODO: find out how to better structure the following code | 153 | #TODO: find out how to better structure the following code |
---|
154 | | 154 | |
---|
155 | # OAuthLib decorators used by OAuthLib in the OAuth flow | 155 | # OAuthLib decorators used by OAuthLib in the OAuth flow |
---|
156 | | | |
---|
157 | @oauth.clientgetter | 156 | @oauth.clientgetter |
---|
158 | def loadClient(client_id): | 157 | def loadClient(client_id): |
---|
159 | current_app.logger.debug('@oauth.clientgetter') | 158 | current_app.logger.debug('@oauth.clientgetter') |
---|
160 | #return Client.query.filter_by(id=client_id).first() | 159 | #return Client.query.filter_by(id=client_id).first() |
---|
161 | return Client.query.get(client_id) | 160 | return Client.query.get(client_id) |
---|
162 | | 161 | |
---|
| | 162 | |
---|
163 | @oauth.grantgetter | 163 | @oauth.grantgetter |
---|
164 | def loadGrant(client_id, code): | 164 | def loadGrant(client_id, code): |
---|
165 | current_app.logger.debug('@oauth.grantgetter') | 165 | current_app.logger.debug('@oauth.grantgetter') |
---|
166 | return Grant.query.filter_by(client_id=client_id, code=code).first() | 166 | return Grant.query.filter_by(client_id=client_id, code=code).first() |
---|
167 | | 167 | |
---|
| | 168 | |
---|
168 | @oauth.grantsetter | 169 | @oauth.grantsetter |
---|
169 | def saveGrant(client_id, code, request, *args, **kwargs): | 170 | def saveGrant(client_id, code, request, *args, **kwargs): |
---|
170 | current_app.logger.debug('@oauth.grantsetter') | 171 | current_app.logger.debug('@oauth.grantsetter') |
---|
171 | expires = datetime.utcnow() + timedelta(seconds=100) | 172 | expires = datetime.utcnow() + timedelta(seconds=100) |
---|
172 | grant = Grant( | 173 | grant = Grant( |
---|
173 | client_id = client_id, | | client_id = client_id, |
---|
174 | code = code['code'], | | code = code['code'], |
---|
175 | redirect_uri = request.redirect_uri, | | redirect_uri = request.redirect_uri, |
---|
176 | _scopes = ' '.join(request.scopes), | | _scopes = ' '.join(request.scopes), |
---|
177 | user = User.getCurrentUser(), | | user = User.getCurrentUser(), |
---|
178 | expires = expires | | expires = expires |
---|
| | 174 | client_id=client_id, | | | 175 | code=code['code'], |
---|
| | 176 | redirect_uri=request.redirect_uri, |
---|
| | 177 | _scopes=' '.join(request.scopes), |
---|
| | 178 | user=User.getCurrentUser(), |
---|
| | 179 | expires=expires |
---|
179 | ) | 180 | ) |
---|
180 | db.session.add(grant) | 181 | db.session.add(grant) |
---|
181 | db.session.commit() | 182 | db.session.commit() |
---|
182 | return grant | 183 | return grant |
---|
183 | | 184 | |
---|
| | 185 | |
---|
184 | @oauth.tokengetter | 186 | @oauth.tokengetter |
---|
185 | def loadToken(access_token=None, refresh_token=None): | 187 | def loadToken(access_token=None, refresh_token=None): |
---|
186 | current_app.logger.debug('@oauth.tokengetter') | 188 | current_app.logger.debug('@oauth.tokengetter') |
---|
… | | … | |
---|
191 | elif refresh_token: | 191 | elif refresh_token: |
---|
192 | return Token.query.filter_by(refresh_token=refresh_token).first() | 192 | return Token.query.filter_by(refresh_token=refresh_token).first() |
---|
193 | | 193 | |
---|
| | 194 | |
---|
194 | @oauth.tokensetter | 195 | @oauth.tokensetter |
---|
195 | def saveToken(token, request, *args, **kwargs): | 196 | def saveToken(token, request, *args, **kwargs): |
---|
196 | current_app.logger.debug('@oauth.tokensetter') | 197 | current_app.logger.debug('@oauth.tokensetter') |
---|
… | | … | |
---|
206 | expires = datetime.utcnow() + timedelta(seconds=expires_in) | 206 | expires = datetime.utcnow() + timedelta(seconds=expires_in) |
---|
207 | | 207 | |
---|
208 | tok = Token( | 208 | tok = Token( |
---|
209 | access_token = token['access_token'], | | access_token = token['access_token'], |
---|
210 | refresh_token = token['refresh_token'], | | refresh_token = token['refresh_token'], |
---|
211 | token_type = token['token_type'], | | token_type = token['token_type'], |
---|
212 | _scopes = token['scope'], | | _scopes = token['scope'], |
---|
213 | expires = expires, | | expires = expires, |
---|
214 | client_id = request.client.id, | | client_id = request.client.id, |
---|
215 | user = request.user | | user = request.user |
---|
| | 209 | access_token=token['access_token'], | | | 210 | refresh_token=token['refresh_token'], |
---|
| | 211 | token_type=token['token_type'], |
---|
| | 212 | _scopes=token['scope'], |
---|
| | 213 | expires=expires, |
---|
| | 214 | client_id=request.client.id, |
---|
| | 215 | user=request.user |
---|
216 | ) | 216 | ) |
---|
217 | db.session.add(tok) | 217 | db.session.add(tok) |
---|
218 | db.session.commit() | 218 | db.session.commit() |
---|
219 | return tok | 219 | return tok |
---|
220 | | 220 | |
---|
| | 221 | |
---|
221 | @oauth.usergetter | 222 | @oauth.usergetter |
---|
222 | def getUser(): | 223 | def getUser(): |
---|
223 | return User.getCurrentUser() | 224 | return User.getCurrentUser() |
---|
224 | | 225 | |
---|
225 | | 226 | |
---|
226 | | | |
---|
227 | # Authorized Clients | 227 | # Authorized Clients |
---|
228 | class AuthorizedClients(db.Model): | 228 | class AuthorizedClients(db.Model): |
---|
229 | """ | 229 | """ |
---|
… | | … | |
---|
258 | | 258 | |
---|
259 | @staticmethod | 259 | @staticmethod |
---|
260 | def getByUser(user): | 260 | def getByUser(user): |
---|
261 | authorized_clients = [row.client for row in \ | | authorized_clients = [row.client for row in \ |
---|
| | 261 | authorized_clients = [row.client for row in | 262 | AuthorizedClients.query.filter_by(user_id=user.id).all()] | 262 | AuthorizedClients.query.filter_by(user_id=user.id).all()] |
---|
263 | | 263 | |
---|
264 | current_app.logger.debug('authorized clients %s', authorized_clients) | 264 | current_app.logger.debug('authorized clients %s', authorized_clients) |
---|
| | | | 5 | from datetime import datetime | 5 | from datetime import datetime |
---|
6 | import json | 6 | import json |
---|
7 | | 7 | |
---|
| | 8 | from sqlalchemy.exc import IntegrityError |
---|
| | 9 | |
---|
8 | from swtstore.classes import db | 10 | from swtstore.classes import db |
---|
9 | from swtstore.classes.models.types import JSONType | 11 | from swtstore.classes.models.types import JSONType |
---|
10 | from swtstore.classes.exceptions import AlreadyExistsError | 12 | from swtstore.classes.exceptions import AlreadyExistsError |
---|
| | | | 8 | from swtstore.classes.database import db | 8 | from swtstore.classes.database import db |
---|
9 | # custom SQLAlchemy type JSONType | 9 | # custom SQLAlchemy type JSONType |
---|
10 | from swtstore.classes.models.types import JSONType | 10 | from swtstore.classes.models.types import JSONType |
---|
11 | from swtstore.classes.utils import urlnorm # normalize URLs | | from swtstore.classes.utils import urlnorm # normalize URLs |
---|
| | 11 | from swtstore.classes.utils import urlnorm # normalize URLs | 12 | from swtstore.classes.models import Context, User | 12 | from swtstore.classes.models import Context, User |
---|
| | 13 | from swtstore.classes.exceptions import InvalidPayload, ContextDoNotExist |
---|
13 | | 14 | |
---|
| | 15 | |
---|
14 | class Sweet(db.Model): | 16 | class Sweet(db.Model): |
---|
15 | """ customary docstring """ | 17 | """ customary docstring """ |
---|
16 | | 18 | |
---|
… | | … | |
---|
32 | | 32 | |
---|
33 | created = db.Column(db.DateTime, default=datetime.utcnow) | 33 | created = db.Column(db.DateTime, default=datetime.utcnow) |
---|
34 | | 34 | |
---|
35 | | | |
---|
36 | def __init__(self, who, what, where, how): | 35 | def __init__(self, who, what, where, how): |
---|
37 | current_app.logger.info('initing sweet..') | 36 | current_app.logger.info('initing sweet..') |
---|
38 | self.who = who | 37 | self.who = who |
---|
… | | … | |
---|
39 | self.where = urlnorm(where) | 39 | self.where = urlnorm(where) |
---|
40 | self.how = how | 40 | self.how = how |
---|
41 | | 41 | |
---|
42 | | | |
---|
43 | def __repr__(self): | 42 | def __repr__(self): |
---|
44 | return '[Sweet Object: <%s : @%s: #%s : %s>]' % (self.id, self.who, | 43 | return '[Sweet Object: <%s : @%s: #%s : %s>]' % (self.id, self.who, |
---|
45 | self.what, self.where) | 44 | self.what, self.where) |
---|
… | | … | |
---|
58 | | 58 | |
---|
59 | return None | 59 | return None |
---|
60 | | 60 | |
---|
61 | | | |
---|
62 | # create multiple sweets from a list of JSON | 61 | # create multiple sweets from a list of JSON |
---|
63 | @staticmethod | 62 | @staticmethod |
---|
64 | def createSweets(who, payload): | 63 | def createSweets(who, payload): |
---|
65 | # the payload has to be a list; a list of swts | 64 | # the payload has to be a list; a list of swts |
---|
66 | for each in payload: | 65 | for each in payload: |
---|
67 | if 'what' not in each and 'where' not in\ | | if 'what' not in each and 'where' not in\ |
---|
68 | each and 'how' not in each: | | each and 'how' not in each: |
---|
| | 66 | if 'what' not in each and 'where' not in each and 'how' not in\ | | | 67 | each: |
---|
69 | | 68 | |
---|
70 | raise InvalidPayload('Invalid payload %s \n for creating\ | | raise InvalidPayload('Invalid payload %s \n for creating\ |
---|
| | 69 | raise InvalidPayload('Invalid payload %s \n while creating\ | 71 | mutiple sweets' % (each)) | 70 | mutiple sweets' % (each)) |
---|
72 | return None | 71 | return None |
---|
73 | | 72 | |
---|
… | | … | |
---|
77 | what = Context.getByName(each['what']) | 77 | what = Context.getByName(each['what']) |
---|
78 | | 78 | |
---|
79 | if what is None: | 79 | if what is None: |
---|
80 | current_app.logger.info('Context "%s" do not exist. Aborting', | | current_app.logger.info('Context "%s" do not exist. Aborting', |
---|
81 | what) | | what) |
---|
82 | g.error = 'Context do not exist' | | g.error = 'Context do not exist' |
---|
83 | abort(400) # this context doesn't exist! | | abort(400) # this context doesn't exist! |
---|
| | 80 | raise ContextDoNotExist('Context %s do not exist!' % | | | 81 | (each['what'])) |
---|
84 | | 82 | |
---|
85 | current_app.logger.debug('SWEET PAYLOAD\n---\n%s\n%s\n%s\n%s\n----', | 83 | current_app.logger.debug('SWEET PAYLOAD\n---\n%s\n%s\n%s\n%s\n----', |
---|
86 | who, what, each['where'], each['how']) | | who, what, each['where'], each['how']) |
---|
| | 84 | who, what, each['where'], each['how']) | 87 | | 85 | |
---|
88 | new_sweet = Sweet(who, what, each['where'], each['how']) | 86 | new_sweet = Sweet(who, what, each['where'], each['how']) |
---|
89 | | 87 | |
---|
… | | … | |
---|
125 | #'created': self.created.isoformat() | 125 | #'created': self.created.isoformat() |
---|
126 | 'created': self.created.strftime('%a, %d %b %Y, %I:%M %p UTC'), | 126 | 'created': self.created.strftime('%a, %d %b %Y, %I:%M %p UTC'), |
---|
127 | } | 127 | } |
---|
128 | | | |
---|
129 | | 128 | |
---|
130 | # create and persist the sweet to the database | 129 | # create and persist the sweet to the database |
---|
131 | def persist(self): | 130 | def persist(self): |
---|
| | | | 3 | # class:: Types | 3 | # class:: Types |
---|
4 | # extend SQLAlchemy Types | 4 | # extend SQLAlchemy Types |
---|
5 | | 5 | |
---|
6 | from datetime import datetime | | from datetime import datetime |
---|
7 | import json | 6 | import json |
---|
8 | | 7 | |
---|
9 | from sqlalchemy import types | 8 | from sqlalchemy import types |
---|
| | 9 | |
---|
10 | | 10 | |
---|
11 | class JSONType(types.TypeDecorator): | 11 | class JSONType(types.TypeDecorator): |
---|
12 | """ | 12 | """ |
---|
| | | | 1 | from flask import Module, jsonify, request, make_response, abort, g, json,\ | | from flask import Module, jsonify, request, make_response, abort, g, json,\ |
---|
2 | current_app | | current_app |
---|
| | 1 | from flask import Module, jsonify, request, make_response | | | 2 | from flask import abort, g, json, current_app |
---|
3 | | 3 | |
---|
4 | from swtstore.classes.models import Context | | from swtstore.classes.models import Context |
---|
5 | from swtstore.classes.models import Sweet | | from swtstore.classes.models import Sweet |
---|
6 | from swtstore.classes.exceptions import AlreadyExistsError, InvalidPayload | | from swtstore.classes.exceptions import AlreadyExistsError, InvalidPayload |
---|
7 | from swtstore.classes.utils import urlnorm # normalize URLs | | from swtstore.classes.utils import urlnorm # normalize URLs |
---|
| | 4 | from swtstore.classes.models import Context, Sweet | | | 5 | from swtstore.classes.exceptions import AlreadyExistsError, InvalidPayload,\ |
---|
| | 6 | ContextDoNotExist |
---|
| | 7 | from swtstore.classes.utils import urlnorm # normalize URLs |
---|
8 | from swtstore.classes.utils.httputils import makeCORSHeaders | 8 | from swtstore.classes.utils.httputils import makeCORSHeaders |
---|
9 | from swtstore.classes import oauth | 9 | from swtstore.classes import oauth |
---|
10 | | 10 | |
---|
… | | … | |
---|
67 | payload = request.json or request.data | 67 | payload = request.json or request.data |
---|
68 | if not payload: | 68 | if not payload: |
---|
69 | current_app.logger.error('data not found in payload!') | 69 | current_app.logger.error('data not found in payload!') |
---|
70 | g.error= 'data not found in payload!' | | g.error= 'data not found in payload!' |
---|
| | 70 | g.error = 'data not found in payload!' | 71 | abort(400) | 71 | abort(400) |
---|
72 | | 72 | |
---|
73 | current_app.logger.debug('new sweet payload recvd.. %s', payload) | 73 | current_app.logger.debug('new sweet payload recvd.. %s', payload) |
---|
… | | … | |
---|
78 | | 78 | |
---|
79 | try: | 79 | try: |
---|
80 | swts = Sweet.createSweets(who, payload) | 80 | swts = Sweet.createSweets(who, payload) |
---|
81 | except InvalidPayload(msg): | | except InvalidPayload(msg): |
---|
82 | current_app.logger.error('Invalid Payload in request') | | current_app.logger.error('Invalid Payload in request') |
---|
| | 81 | except (InvalidPayload, ContextDoNotExist) as e: | | | 82 | current_app.logger.error('Error creating sweets. Error: %s', e) |
---|
83 | abort(400) | 83 | abort(400) |
---|
84 | | 84 | |
---|
85 | response.status_code = 200 | 85 | response.status_code = 200 |
---|
… | | … | |
---|
99 | response = makeCORSHeaders(response, origin) | 99 | response = makeCORSHeaders(response, origin) |
---|
100 | | 100 | |
---|
101 | if request.method == 'OPTIONS': | 101 | if request.method == 'OPTIONS': |
---|
102 | reponse.status_code = 200 | | reponse.status_code = 200 |
---|
| | 102 | response.status_code = 200 | 103 | return response | 103 | return response |
---|
104 | | 104 | |
---|
105 | args = request.args | 105 | args = request.args |
---|
… | | … | |
---|
146 | current_app.logger.debug('getContextByName : %s', context) | 146 | current_app.logger.debug('getContextByName : %s', context) |
---|
147 | return jsonify(context.to_dict()) | 147 | return jsonify(context.to_dict()) |
---|
148 | | 148 | |
---|
| | 149 | |
---|
149 | # Get a specific context with its definition; based on id | 150 | # Get a specific context with its definition; based on id |
---|
150 | @api.route('/contexts/<int:id>', methods=['GET']) | 151 | @api.route('/contexts/<int:id>', methods=['GET']) |
---|
151 | def getContextById(id): | 152 | def getContextById(id): |
---|
… | | … | |
---|
194 | current_app.logger.debug('new context created: %s', new_context) | 194 | current_app.logger.debug('new context created: %s', new_context) |
---|
195 | | 195 | |
---|
196 | # all ok. save the new context | 196 | # all ok. save the new context |
---|
197 | res = new_context.persist() | | res = new_context.persist() |
---|
| | 197 | new_context.persist() | 198 | | 198 | |
---|
199 | response.status_code = 200 | 199 | response.status_code = 200 |
---|
200 | return response | 200 | return response |
---|
| | | | 1 | # -*- coding utf-8 -*- | 1 | # -*- coding utf-8 -*- |
---|
2 | # classes/views/apps.py | 2 | # classes/views/apps.py |
---|
3 | | 3 | |
---|
4 | from flask import Module, jsonify, request, render_template, redirect,\ | | from flask import Module, jsonify, request, render_template, redirect,\ |
---|
5 | url_for, flash, abort | | url_for, flash, abort |
---|
| | 4 | from flask import Module, request, render_template, redirect,\ | | | 5 | url_for, abort |
---|
6 | | 6 | |
---|
7 | from hashlib import md5 | | from hashlib import md5 |
---|
8 | from werkzeug.security import gen_salt | 7 | from werkzeug.security import gen_salt |
---|
9 | | 8 | |
---|
10 | from swtstore.classes.models import Client, User | 9 | from swtstore.classes.models import Client, User |
---|
… | | … | |
---|
29 | abort(404) | 29 | abort(404) |
---|
30 | | 30 | |
---|
31 | new_app = Client( | 31 | new_app = Client( |
---|
32 | id = gen_salt(40), | | id = gen_salt(40), |
---|
33 | client_secret = gen_salt(50), | | client_secret = gen_salt(50), |
---|
34 | name = request.form.get('name'), | | name = request.form.get('name'), |
---|
35 | description = request.form.get('desc'), | | description = request.form.get('desc'), |
---|
36 | user_id = current_user.id, | | user_id = current_user.id, |
---|
37 | _host_url = request.form.get('host_url'), | | _host_url = request.form.get('host_url'), |
---|
38 | _redirect_uris = urlnorm(request.form.get('redirect_uris')), | | _redirect_uris = urlnorm(request.form.get('redirect_uris')), |
---|
39 | _default_scopes = ' '.join(request.form.get('scopes').split(',')), | | _default_scopes = ' '.join(request.form.get('scopes').split(',')), |
---|
40 | _is_private = False | | _is_private = False |
---|
| | 32 | id=gen_salt(40), | | | 33 | client_secret=gen_salt(50), |
---|
| | 34 | name=request.form.get('name'), |
---|
| | 35 | description=request.form.get('desc'), |
---|
| | 36 | user_id=current_user.id, |
---|
| | 37 | _host_url=request.form.get('host_url'), |
---|
| | 38 | _redirect_uris=urlnorm(request.form.get('redirect_uris')), |
---|
| | 39 | _default_scopes=' '.join(request.form.get('scopes').split(',')), |
---|
| | 40 | _is_private=False |
---|
41 | ) | 41 | ) |
---|
42 | new_app.persist() | 42 | new_app.persist() |
---|
43 | | 43 | |
---|
| | | | 1 | # -*- coding utf-8 -*- | 1 | # -*- coding utf-8 -*- |
---|
2 | # classes/views/context.py | 2 | # classes/views/context.py |
---|
3 | | 3 | |
---|
4 | from flask import Module, jsonify, request, render_template, redirect,\ | | from flask import Module, jsonify, request, render_template, redirect,\ |
---|
5 | url_for, json, current_app | | url_for, json, current_app |
---|
| | 4 | from flask import Module, request, render_template, redirect,\ | | | 5 | url_for, json, current_app, abort |
---|
6 | | 6 | |
---|
7 | from swtstore.classes.models import Context, User | 7 | from swtstore.classes.models import Context, User |
---|
8 | | 8 | |
---|
9 | | 9 | |
---|
10 | context = Module(__name__) | 10 | context = Module(__name__) |
---|
11 | | 11 | |
---|
| | 12 | |
---|
12 | @context.route('/register', methods=['GET', 'POST']) | 13 | @context.route('/register', methods=['GET', 'POST']) |
---|
13 | def register(): | 14 | def register(): |
---|
14 | current_user = User.getCurrentUser() | 15 | current_user = User.getCurrentUser() |
---|
… | | … | |
---|
20 | return render_template('context/register.html') | 20 | return render_template('context/register.html') |
---|
21 | | 21 | |
---|
22 | if request.method == 'POST': | 22 | if request.method == 'POST': |
---|
23 | if not request.form.get('name') or not request.form.get('defn'): | | if not request.form.get('name') or not request.form.get('defn'): |
---|
24 | abort(400) | | abort(400) |
---|
| | 23 | if not request.form.get('name') or not request.form.get('defn'): | | | 24 | abort(400) |
---|
25 | | 25 | |
---|
26 | current_app.logger.debug('New Context: defn: %s ', | 26 | current_app.logger.debug('New Context: defn: %s ', |
---|
27 | request.form.get('defn')) | 27 | request.form.get('defn')) |
---|
… | | … | |
---|
29 | current_app.logger.debug('Resulting json_ld %s', json_ld) | 29 | current_app.logger.debug('Resulting json_ld %s', json_ld) |
---|
30 | | 30 | |
---|
31 | new_context = Context( | 31 | new_context = Context( |
---|
32 | name = request.form.get('name'), | | name = request.form.get('name'), |
---|
33 | definition = json_ld, | | definition = json_ld, |
---|
34 | user_id = current_user.id | | user_id = current_user.id |
---|
| | 32 | name=request.form.get('name'), | | | 33 | definition=json_ld, |
---|
| | 34 | user_id=current_user.id |
---|
35 | ) | 35 | ) |
---|
36 | current_app.logger.debug('New Context created: %s', new_context) | 36 | current_app.logger.debug('New Context created: %s', new_context) |
---|
37 | new_context.persist() | 37 | new_context.persist() |
---|
| | | | 2 | # classes/views/frontend.py | 2 | # classes/views/frontend.py |
---|
3 | | 3 | |
---|
4 | | 4 | |
---|
5 | from flask import Module, jsonify, request, render_template, redirect,\ | | from flask import Module, jsonify, request, render_template, redirect,\ |
---|
6 | url_for, g, current_app | | url_for, g, current_app |
---|
| | 5 | from flask import Module, render_template | 7 | | 6 | |
---|
8 | from swtstore.classes.models import Sweet, User | | from swtstore.classes.models import Sweet, User |
---|
| | 7 | from swtstore.classes.models import Sweet | 9 | | 8 | |
---|
10 | | 9 | |
---|
11 | frontend = Module(__name__) | 10 | frontend = Module(__name__) |
---|
| | 11 | |
---|
12 | | 12 | |
---|
13 | @frontend.route('/', methods=['GET']) | 13 | @frontend.route('/', methods=['GET']) |
---|
14 | def index(): | 14 | def index(): |
---|
| | | | 1 | # -*- coding utf-8 -*- | 1 | # -*- coding utf-8 -*- |
---|
2 | # classes/views/oauth.py | 2 | # classes/views/oauth.py |
---|
3 | | 3 | |
---|
4 | from flask import Module, jsonify, request, render_template, redirect,\ | | from flask import Module, jsonify, request, render_template, redirect,\ |
---|
5 | url_for, current_app | | url_for, current_app |
---|
6 | import requests | | import requests |
---|
| | 4 | from flask import Module, jsonify, request, render_template, current_app | 7 | | 5 | |
---|
8 | from swtstore.classes import oauth | 6 | from swtstore.classes import oauth |
---|
9 | from swtstore.classes.models import Client, AuthorizedClients, User | 7 | from swtstore.classes.models import Client, AuthorizedClients, User |
---|
… | | … | |
---|
33 | else: | 33 | else: |
---|
34 | return render_template('oauth/authorize.html', **kwargs) | 34 | return render_template('oauth/authorize.html', **kwargs) |
---|
35 | | 35 | |
---|
36 | | | |
---|
37 | confirm = request.form.get('confirm', 'no') | 36 | confirm = request.form.get('confirm', 'no') |
---|
38 | authorized = request.form.get('authorized', 'no') | 37 | authorized = request.form.get('authorized', 'no') |
---|
39 | current_app.logger.debug('confirm authorize from user: %s', confirm) | 38 | current_app.logger.debug('confirm authorize from user: %s', confirm) |
---|
… | | … | |
---|
55 | #print request.form | 55 | #print request.form |
---|
56 | current_app.logger.debug('access token touched..') | 56 | current_app.logger.debug('access token touched..') |
---|
57 | return None | 57 | return None |
---|
| | 58 | |
---|
58 | | 59 | |
---|
59 | @Oauth.route('/errors') | 60 | @Oauth.route('/errors') |
---|
60 | def error(): | 61 | def error(): |
---|
| | | | 2 | # classes/views/sweet.py | 2 | # classes/views/sweet.py |
---|
3 | | 3 | |
---|
4 | | 4 | |
---|
5 | from flask import Module, jsonify, request, render_template, redirect,\ | | from flask import Module, jsonify, request, render_template, redirect,\ |
---|
6 | url_for, abort, json | | url_for, abort, json |
---|
| | 5 | from flask import Module, render_template, abort | 7 | | 6 | |
---|
8 | from swtstore.classes.models import Context, Sweet, User | | from swtstore.classes.models import Context, Sweet, User |
---|
| | 7 | from swtstore.classes.models import Sweet | 9 | | 8 | |
---|
10 | | 9 | |
---|
11 | sweet = Module(__name__) | 10 | sweet = Module(__name__) |
---|
| | 11 | |
---|
12 | | 12 | |
---|
13 | @sweet.route('/<int:id>', methods=['GET']) | 13 | @sweet.route('/<int:id>', methods=['GET']) |
---|
14 | def showSweet(id): | 14 | def showSweet(id): |
---|
| | | | 4 | import requests | 4 | import requests |
---|
5 | | 5 | |
---|
6 | # flask imports | 6 | # flask imports |
---|
7 | from flask import Module, jsonify, request, render_template, session,\ | | from flask import Module, jsonify, request, render_template, session,\ |
---|
8 | make_response, url_for, redirect, json, current_app | | make_response, url_for, redirect, json, current_app |
---|
| | 7 | from flask import Module, request, render_template, session,\ | | | 8 | make_response, url_for, redirect, json, current_app |
---|
9 | | 9 | |
---|
10 | # swtstore imports | 10 | # swtstore imports |
---|
11 | from swtstore.classes.models import User, Sweet, Context, Client,\ | 11 | from swtstore.classes.models import User, Sweet, Context, Client,\ |
---|
12 | AuthorizedClients | | AuthorizedClients |
---|
| | 12 | AuthorizedClients | 13 | | 13 | |
---|
14 | from swtstore.classes.utils.httputils import makeCORSHeaders | | from swtstore.classes.utils.httputils import makeCORSHeaders |
---|
15 | from swtstore.config import DefaultConfig | 14 | from swtstore.config import DefaultConfig |
---|
16 | | 15 | |
---|
17 | | 16 | |
---|
… | | … | |
---|
18 | | 18 | |
---|
19 | user = Module(__name__) | 19 | user = Module(__name__) |
---|
20 | | 20 | |
---|
| | 21 | |
---|
21 | @user.route('/login', methods=['POST']) | 22 | @user.route('/login', methods=['POST']) |
---|
22 | def login(): | 23 | def login(): |
---|
23 | | 24 | |
---|
… | | … | |
---|
68 | response.status_code = 500 | 68 | response.status_code = 500 |
---|
69 | return response | 69 | return response |
---|
70 | | 70 | |
---|
| | 71 | |
---|
71 | @user.route('/logout', methods=['POST']) | 72 | @user.route('/logout', methods=['POST']) |
---|
72 | def logout(): | 73 | def logout(): |
---|
73 | | 74 | |
---|
… | | … | |
---|
82 | response.status_code = 200 | 82 | response.status_code = 200 |
---|
83 | return response | 83 | return response |
---|
84 | | 84 | |
---|
| | 85 | |
---|
85 | @user.route('/me', methods=['GET', 'POST']) | 86 | @user.route('/me', methods=['GET', 'POST']) |
---|
86 | def profile(): | 87 | def profile(): |
---|
87 | | 88 | |
---|
… | | … | |
---|
138 | apps = Client.getClientsByCreator(user.id) | 138 | apps = Client.getClientsByCreator(user.id) |
---|
139 | return render_template('user/apps.html', apps=apps) | 139 | return render_template('user/apps.html', apps=apps) |
---|
140 | | 140 | |
---|
| | 141 | |
---|
141 | @user.route('/me/authorized_apps', methods=['GET', 'POST']) | 142 | @user.route('/me/authorized_apps', methods=['GET', 'POST']) |
---|
142 | def authorizedApps(): | 143 | def authorizedApps(): |
---|
143 | | 144 | |
---|
… | | … | |
---|
149 | if request.method == 'GET': | 149 | if request.method == 'GET': |
---|
150 | authorized_clients = AuthorizedClients.getByUser(user) | 150 | authorized_clients = AuthorizedClients.getByUser(user) |
---|
151 | return render_template('user/authorized_apps.html', | 151 | return render_template('user/authorized_apps.html', |
---|
152 | authorized_clients=authorized_clients) | | authorized_clients=authorized_clients) |
---|
| | 152 | authorized_clients=authorized_clients) | 153 | | 153 | |
---|
154 | # else POST request | 154 | # else POST request |
---|
155 | client_id = request.form.get('revoke-id', '') | 155 | client_id = request.form.get('revoke-id', '') |
---|
| | | | 1 | | 1 | |
---|
| | 2 | |
---|
2 | class DefaultConfig(): | 3 | class DefaultConfig(): |
---|
3 | | 4 | |
---|
4 | """ | 5 | """ |
---|
… | | … | |
---|
21 | # been done prior to editing this line. | 21 | # been done prior to editing this line. |
---|
22 | # Refer https://wiki.debian.org/PostgreSql#User_access for creating users | 22 | # Refer https://wiki.debian.org/PostgreSql#User_access for creating users |
---|
23 | # in postgresql. | 23 | # in postgresql. |
---|
24 | SQLALCHEMY_DATABASE_URI =\ | | SQLALCHEMY_DATABASE_URI =\ |
---|
25 | 'dialect+driver://username:password@host:port/database' | | 'dialect+driver://username:password@host:port/database' |
---|
| | 24 | SQLALCHEMY_DATABASE_URI = 'dialect+driver://username:password@host:port/database' | 26 | | 25 | |
---|
27 | # Log level for the application | 26 | # Log level for the application |
---|
28 | LOG_LEVEL = 'ERROR' | 27 | LOG_LEVEL = 'ERROR' |
---|