예제 #1
0
async def decrement_actor_vote(actor_id: int):
    try:
        Session.query(Actor).filter(Actor.actor_id == actor_id).update(
            {Actor.votes: Actor.votes - 1})
        Session.commit()
        content = {"Success": "Actor votes decreased."}
        status_code = 200
    except:
        content = {"Not Found": "Actor Not Found."}
        status_code = 404
    return JSONResponse(status_code=status_code,
                        content=content,
                        headers=headers)
예제 #2
0
def authenticate_api_key(key, secret):
    """Authenticate an API key"""
    s = Session()
    user = s.query(User).filter_by(api_key=key, api_secret=secret).first()
    s.close()

    return user
예제 #3
0
async def get_actors():
    try:
        actors = [actor.to_json() for actor in Session.query(Actor).all()]
        content = {"actors": actors}
        return JSONResponse(content=content, headers=headers)
    except Exception as e:
        content = {"Server error": "something went wrong!"}
        return JSONResponse(status_code=500, content=content, headers=headers)
예제 #4
0
def search(grocery_name):
    if request.method == 'GET':
        session = Session()
        result = session.query(Grocery).filter_by(name=grocery_name).first()
        if result is None:
            return ('Grocery not found.', 404)
        else:
            return result.__json__()
예제 #5
0
def grocery_list(grocery_list_id):
	if request.method == 'GET':
		session = Session()
		result = session.query(GroceryList).filter_by(grocery_list_id=grocery_list_id).first()
		if result is None:
			return ('Grocery list not found.', 404)
		else:
			return result.__json__()
예제 #6
0
def grocery_list_item(grocery_list_item_id):
    if request.method == 'GET':
        session = Session()
        result = session.query(GroceryListItem).filter_by(
            grocery_list_item_id=grocery_list_item_id).first()
        if result is None:
            return ('Grocery list item not found.', 404)
        else:
            return result.__json__()
    elif request.method == 'DELETE':
        session = Session()
        grocery_list_item = session.query(GroceryListItem).filter_by(
            grocery_list_item_id=grocery_list_item_id).first()
        if grocery_list_item is None:
            return ('Grocery list item not found.', 404)
        else:
            session.delete(grocery_list_item)
            session.commit()
            return grocery_list_item.__json__()
예제 #7
0
async def get_actor_votes(actor_id: int):
    try:
        votes = Session.query(
            Actor.votes).filter(Actor.actor_id == actor_id).scalar()

        content = {"votes": votes}
        return JSONResponse(content=content, headers=headers)
    except:
        content = {"Not Found": "Actor Not Found."}
        return JSONResponse(status_code=404, content=content, headers=headers)
예제 #8
0
async def get_actor(actor_id: int):
    try:
        actor = Session.query(Actor).filter(
            Actor.actor_id == actor_id).scalar()

        content = {"actor": actor.to_json()}
        return JSONResponse(content=content, headers=headers)
    except:
        content = {"Not Found": "Actor Not Found."}
        return JSONResponse(status_code=404, content=content, headers=headers)
예제 #9
0
def user_lists(user_id):
    if request.method == 'GET':  #Retrieves all of a user's lists
        session = Session()
        result = session.query(GroceryList).filter_by(user_id=user_id).all()
        response = []
        for grocery_list in result:
            json = grocery_list.__json__()
            grocery_list_items_preview = (
                session.query(GroceryListItem).order_by(
                    GroceryListItem.grocery_list_item_id)[:SAMPLE_IMAGES])
            json['grocery_list_items_preview'] = grocery_list_items_preview
            response.append(json)
        return {'grocery_lists': response}
    elif request.method == 'POST':  #Creates a new list for the user
        grocery_list = GroceryList(user_id=user_id, name=request.json['name'])
        session = Session()
        session.add(grocery_list)
        session.commit()
        return grocery_list.__json__()
예제 #10
0
def groceries():
    if request.method == 'POST':  #makes new grocery
        grocery = Grocery.from_json(request.json)
        session = Session()
        session.add(grocery)
        session.commit()
        return grocery.__json__()
    elif request.method == 'GET':  #grabs all groceries from SQL database
        session = Session()
        result = session.query(Grocery).order_by(Grocery.grocery_id).all()
        return {'groceries': [grocery.__json__() for grocery in result]}
예제 #11
0
def authenticate_auth_token(token):
    """Authenticate a jwt auth token"""
    try:
        payload = jwt.decode(token, TOKEN_SECRET_KEY)
    except:
        return None

    user_id = payload["sub"]

    s = Session()
    user = s.query(User).filter_by(id=user_id).first()
    s.close()

    return user
예제 #12
0
    def ListCustomers(self, request, context):
        """
        List all customers

        :param request: A ListCustomersRequest
        :param context: the grpc context
        """
        db = Session()
        customers = db.query(Customer).all()

        return customers_pb2.ListCustomersResponse(
            customers=[
                c.to_message(customers_pb2.Customer) for c in customers
            ],
            status=status_pb2.Status(code=200))
예제 #13
0
def user_list(user_id, list_id):
    if request.method == 'GET':  #Grabs all items from a user's grocery list
        session = Session()
        result = session.query(GroceryListItem).filter_by(
            grocery_list_id=list_id).order_by(
                GroceryListItem.grocery_list_item_id).all()
        if result is None:
            return ('Grocery not found.', 404)
        else:
            return {
                'grocery_items':
                [grocery_list_item.__json__() for grocery_list_item in result]
            }
    elif request.method == 'POST':  #Adds an item to a user's grocery list
        grocery_list_item = GroceryListItem(
            grocery_list_id=list_id, grocery_id=request.json['grocery_id'])
        session = Session()
        session.add(grocery_list_item)
        session.commit()
        return grocery_list_item.__json__()
예제 #14
0
    def on_post(self, req, resp):

        ean13 = req.parsed['ean13']
        password = req.parsed['password']

        worker = session.query(Worker)\
            .filter(Worker.ean13 == ean13)\
            .filter(Worker.password == password)

        if not worker.scalar():
            raise falcon.HTTPForbidden()

        try:
            # import pdb; pdb.set_trace()
            resp.set_cookie('user_session',
                            make_session(credential=str(ean13),
                                         user_data=req.host + req.user_agent,
                                         user_id=worker[0].id),
                            path='/')
        except Exception:
            raise falcon.HTTPUnauthorized()