def users(): if request.method == 'POST': user = User.from_json(request.json) session = Session() session.add(user) session.commit() return user.__json__()
def grocery_list_items(): if request.method == 'POST': grocery_list_item = GroceryListItem.from_json(request.json) session = Session() session.add(grocery_list_item) session.commit() return grocery_list_item.__json__()
def setUp(self): from db.session import Session, DBState from db.models import User, Item, DiscuzMember, DiscuzMemberCount from db.base import Model Model.metadata.drop_all(DBState.engine) Model.metadata.create_all(DBState.engine) s = Session() [ s.add(i) for i in [ User(id=1, username='******', jiecao=100000, ppoint=1000, title='', email='*****@*****.**'), User(id=2, username='******', jiecao=100000, ppoint=1000, title='', email='*****@*****.**'), DiscuzMember(uid=1, username='******'), DiscuzMember(uid=2, username='******'), DiscuzMemberCount(uid=1, jiecao=100000), DiscuzMemberCount(uid=2, jiecao=100000), Item(id=1, owner_id=1, sku='foo', status='backpack'), Item(id=2, owner_id=2, sku='bar', status='backpack'), ] if not options.freeplay or 'DiscuzMember' not in i.__class__.__name__ ] s.commit()
def add_iventory(): """Add parts to DB.""" session = Session() inventory = ( StockPart( part=Part(name='Valve Body 32"'), count=100, ), StockPart( part=Part(name='Valve Body 12"'), count=50, ), StockPart( part=Part(name='Valve Slip 32"'), count=90, ), StockPart( part=Part(name='Valve Slip 12"'), count=50, ), StockPart( part=Part(name='Screw 14 x 2 1/2"'), count=1000, ), ) session.add_all(inventory) session.commit()
def create(cls, username, password): session = Session() session.execute( "INSERT INTO user(username, password) VALUES ('{username}', '{password}')".format( username=username, password=password ) ) session.commit()
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]}
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)
def create_user(params: dict): """ Create a user """ password_hash = hash_password(params.get("password")) del params["password"] params["password_hash"] = password_hash user = models.User(**params) db = Session() db.add(user) db.commit() db.close() return user
def setUp(self): from db.session import Session, DBState from db.models import User, Item, DiscuzMember, DiscuzMemberCount from db.base import Model Model.metadata.drop_all(DBState.engine) Model.metadata.create_all(DBState.engine) s = Session() [s.add(i) for i in [ User(id=1, username='******', jiecao=100000, ppoint=1000, title='', email='*****@*****.**'), User(id=2, username='******', jiecao=100000, ppoint=1000, title='', email='*****@*****.**'), DiscuzMember(uid=1, username='******'), DiscuzMember(uid=2, username='******'), DiscuzMemberCount(uid=1, jiecao=100000), DiscuzMemberCount(uid=2, jiecao=100000), Item(id=1, owner_id=1, sku='foo', status='backpack'), Item(id=2, owner_id=2, sku='bar', status='backpack'), ] if not options.freeplay or 'DiscuzMember' not in i.__class__.__name__] s.commit()
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__()
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__()
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__()
def CreateCustomer(self, request, context): """ Create a customer :param request: A CreateCustomerRequest :param context: The grpc context """ # Validate customer errors = validate_customer(request.customer) if errors: return customers_pb2.CreateCustomerResponse( status=status_pb2.Status(code=400, data_errors=errors), ) # Add customer to database db = Session() customer = Customer(request.customer) try: db.add(customer) db.commit() return customers_pb2.CreateCustomerResponse( customer=customer.to_message(customers_pb2.Customer), status=status_pb2.Status(code=200), ) except Exception as e: db.rollback() return customers_pb2.CreateCustomerResponse( status=status_pb2.Status(code=500, runtime_errors=[ exception_to_runtime_error(e) ]), ) finally: db.close()