예제 #1
0
 def return_exception(self, ex):
     http_code = 400
     if isinstance(ex, BusinessException):
         exception = {
             'exceptions': [{
                 'type': ex.type,
                 'message': ex.message,
             }]
         }
     elif isinstance(ex, NotAuthorizedException):
         http_code = 401
         exception = {
             'exceptions': [{
                 'type': ex.type,
                 'message': ex.message
             }]
         }
     else:
         http_code = 500
         exception = {
             'exceptions': [{
                 'message': [str(x) for x in ex.args],
             }]
         }
     logging.debug(traceback.format_exc())
     __return = {'code': http_code, 'exceptions': exception}
     print(Server().SERVER_DEBUG)
     if Server().SERVER_DEBUG == "True":
         __return['traceback'] = traceback.format_exc()
     return make_response(jsonify(__return), http_code)
 def get_login_token(self, dealer):
     expires_at = datetime.datetime.utcnow() + datetime.timedelta(
         minutes=240)
     token = jwt.encode({
         'public_id': dealer.public_id,
         'exp': expires_at
     },
                        Server().SECRET_KEY)
     return {"token": token, "expires_at": expires_at}
예제 #3
0
    def decorated(*args, **kwargs):
        token = None

        if 'x-access-token' in request.headers:
            token = request.headers['x-access-token']
        if not token:
            return make_response(jsonify({'message': 'Token is invalid'}), 401)
        try:
            data = jwt.decode(token, Server().SECRET_KEY)
        except Exception as ex:
            return make_response(jsonify({'message': 'Token is invalid'}), 401)

        return f(data, *args, **kwargs)
예제 #4
0
import json
from flask import Blueprint, request
from project.config import Server
from project.framework.decorators import token_required
from project.services.purchase_service import PurchaseService

purchase_route = Blueprint('purchase_route', __name__)

server = Server()
basePath = server.get_base_path()
PURCHASE_PREFIX = basePath + '/purchase'


@purchase_route.route(PURCHASE_PREFIX + "/", methods=["POST"])
@token_required
def add_purchase(access_token):
    params = json.loads(request.data)
    result = PurchaseService(access_token).add_purchase(params)
    return result


@purchase_route.route(PURCHASE_PREFIX + "/", methods=["PUT"])
@token_required
def upd_purchase(access_token):
    params = json.loads(request.data)
    result = PurchaseService(access_token).upd_purchase(params)
    return result


@purchase_route.route(PURCHASE_PREFIX + "/<id>", methods=["DELETE"])
@token_required
# -*- coding: utf-8 -*-

#from peewee import *
import datetime
from project.config import Server
from mongoengine import *
#mysql_db = MySQLDatabase(Server().MYSQL_DATABASE, user=Server().MYSQL_USER, password=Server().MYSQL_PASSWORD, host=Server().MYSQL_HOST)

connect('dev', host=Server().NOSQL_HOST)

#class BaseRepository(Document):

#class Meta:
#    database = mysql_db

#def prepare_update_params(self, params):
#    for key, value in params.iteritems():
#        setattr(self, key, value)

#def save(self, *args, **kwargs):
#    if self._pk is None:
#        self.created_at = datetime.datetime.now()
#    self.updated_at = datetime.datetime.now()
#    return super(BaseRepository, self).save(*args, **kwargs)