def add_wallet(body): """ Add a new wallet and associates it with the owner :param body: Wallet object that needs to be inserted :type body: dict | bytes :rtype: None """ try: if connexion.request.is_json: get_db().add(Wallet(**body)) get_db().commit() return NoContent, 201 return NoContent, 400 except Exception as e: return {'message': str(e)}, 500
def add_ask(body): """ Add a new ask to the market Add token buying offering :param body: Ask object that needs to be added to the market :type body: dict | bytes :rtype: None """ try: if connexion.request.is_json: get_db().add(Ask(**body)) get_db().commit() return NoContent, 201 return NoContent, 400 except Exception as e: return {'message': str(e)}, 500
def add_entry(body): """ Enter a new entry to the book Add token buying offering :param body: Book entry object that needs to be added to the book :type body: dict | bytes :rtype: None """ try: if connexion.request.is_json: get_db().add(Book(**body)) get_db().commit() return NoContent, 201 return NoContent, 400 except Exception as e: return {'message': str(e)}, 500
def add_user(body): """ Creates a new user Only the admin user can create users. :param body: The user account to create :type body: dict | bytes :rtype: None """ try: if connexion.request.is_json: get_db().add(User(**body)) get_db().commit() return NoContent, 201 return NoContent, 400 except Exception as e: return {'message': str(e)}, 500
def add_bid(body): """ Add a new bid to the market Add a token buying offer to the marketplace. :param body: Bid object that needs to be added to the market quantity is the amount of token and price is VND price of one token :type body: dict | bytes :rtype: None """ try: if connexion.request.is_json: get_db().add(Bid(**body)) get_db().commit() return NoContent, 201 return NoContent, 400 except Exception as e: return {'message': str(e)}, 500
def del_entry(id): """del_entry Deletes a single book entry based on the ID supplied :param id: ID of the object to fetch :type id: int :rtype: None """ try: entry = get_db().query(Book).filter(Book.id == id).one_or_none() if entry is not None: get_db().query(Book).filter(Book.id == id).delete() get_db().commit() return NoContent, 204 else: return NoContent, 404 except Exception as e: return {'message': str(e)}, 500
def del_wallet(id): """ Deletes a wallet based on the ID supplied :param id: ID of the object to fetch :type id: int :rtype: None """ try: wallet = get_db().query(Wallet).filter(Wallet.id == id).one_or_none() if wallet is not None: get_db().query(Wallet).filter(Wallet.id == id).delete() get_db().commit() return NoContent, 204 else: return NoContent, 404 except Exception as e: return {'message': str(e)}, 500
def del_user(id): """ Deletes a user Users can only delete their own account, admin can delete everyone else. :param id: ID of the object to fetch :type id: int :rtype: None """ try: user = get_db().query(User).filter(User.id == id).one_or_none() if user is not None: get_db().query(User).filter(User.id == id).delete() get_db().commit() return NoContent, 204 else: return NoContent, 404 except Exception as e: return {'message': str(e)}, 500
def del_ask(id): """ Deletes and existing ask Deletes a single bet based on the ID supplied :param id: ID of the object to fetch :type id: int :rtype: None """ try: ask = get_db().query(Ask).filter(Ask.id == id).one_or_none() if ask is not None: get_db().query(Ask).filter(Ask.id == id).delete() get_db().commit() return NoContent, 204 else: return NoContent, 404 except Exception as e: return {'message': str(e)}, 500
def get_entry(id): """get_entry Returns an entry from the book based on a single ID :param id: ID of the object to fetch :type id: int :rtype: Book """ try: entry = get_db().query(Book).filter(Book.id == id).one_or_none() return entry.dump() if entry is not None else ('Not found', 404) except Exception as e: return {'message': str(e)}, 500
def update_wallet(id, body): """ Update an existing wallet :param id: ID of the object to fetch :type id: int :param body: Wallet that needs to be modified :type body: dict | bytes :rtype: None """ try: wallet = get_db().query(Wallet).filter(Wallet.id == id).one_or_none() if wallet is not None: if connexion.request.is_json: wallet.update(**body) get_db().commit() return NoContent, 204 return NoContent, 400 else: return NoContent, 404 except Exception as e: return {'message': str(e)}, 500
def get_wallet(id): """ Returns a wallet info based on a single ID :param id: ID of the object to fetch :type id: int :rtype: Wallet """ try: wallet = get_db().query(Wallet).filter(Wallet.id == id).one_or_none() return wallet.dump() if wallet is not None else ('Not found', 404) except Exception as e: return {'message': str(e)}, 500
def get_ask(id): """ Get ask details by ID Returns a user based on a single ID :param id: ID of the object to fetch :type id: int :rtype: Ask """ try: ask = get_db().query(Ask).filter(Ask.id == id).one_or_none() return ask.dump() if ask is not None else ('Not found', 404) except Exception as e: return {'message': str(e)}, 500
def update_bid(id, body): """ Update an existing bid Modify bid status only :param id: ID of the object to fetch :type id: int :param body: Bid object that needs to be modified :type body: dict | bytes :rtype: None """ try: bid = get_db().query(Bid).filter(Bid.id == id).one_or_none() if bid is not None: if connexion.request.is_json: bid.update(**body) get_db().commit() return NoContent, 204 return NoContent, 400 else: return NoContent, 404 except Exception as e: return {'message': str(e)}, 500
def update_user(id, body): """ Update a user Users can only update their own account, except the admin, who can edit anyone. :param id: ID of the object to fetch :type id: int :param body: The user account to create :type body: dict | bytes :rtype: None """ try: user = get_db().query(User).filter(User.id == id).one_or_none() if user is not None: if connexion.request.is_json: user.update(**body) get_db().commit() return NoContent, 204 return NoContent, 400 else: return NoContent, 404 except Exception as e: return {'message': str(e)}, 500
def update_ask(id, body): """ Update an existing ask Modify bid status only :param id: ID of the object to fetch :type id: int :param body: Ask object that needs to be added to the marketplace :type body: dict | bytes :rtype: None """ try: ask = get_db().query(Ask).filter(Ask.id == id).one_or_none() if ask is not None: if connexion.request.is_json: ask.update(**body) get_db().commit() return NoContent, 204 return NoContent, 400 else: return NoContent, 404 except Exception as e: return {'message': str(e)}, 500
def get_user(id): """ Retrieves a user Users can only retrieve their own account, except for the admin, who can retrieve anyone. :param id: ID of the object to fetch :type id: int :rtype: User """ try: user = get_db().query(User).filter(User.id == id).one_or_none() return user.dump() if user is not None else ('Not found', 404) except Exception as e: return {'message': str(e)}, 500
def get_bid(id): """ Get bid details by ID Returns a bid based on a single ID :param id: ID of the object to fetch :type id: int :rtype: Bid """ try: bid = get_db().query(Bid).filter(Bid.id == id).one_or_none() return bid.dump() if bid is not None else ('Not found', 404) except Exception as e: return {'message': str(e)}, 500
def list_users(offset=None, limit=None): """ Returns all users in the database. Only the admin can access this. :param offset: The number of items to skip before starting to collect the result set. :type offset: int :param limit: The numbers of items to return. :type limit: int :rtype: List[User] """ try: q = get_db().query(User).limit(limit).offset(offset) return [u.dump() for u in q] except Exception as e: return {'message': str(e)}, 500
def list_wallets(owner=None, offset=None, limit=None): """ Lists wallets List of wallets of the users :param owner: Owner id as a filter :type owner: int :param offset: The number of items to skip before starting to collect the result set. :type offset: int :param limit: The numbers of items to return. :type limit: int :rtype: List[Wallet] """ try: q = get_db().query(Wallet) if owner is not None: q = q.filter(Wallet.owner == owner) q = q.limit(limit).offset(offset) return [w.dump() for w in q] except Exception as e: return {'message': str(e)}, 500
def list_entries(symbol=None, owner=None, offset=None, limit=None): """Lists the Book Lists entries from the book, and tells who own how many shares :param symbol: Symbol as a filter :type symbol: str :param owner: Owner id as a filter :type owner: int :param offset: The number of items to skip before starting to collect the result set. :type offset: int :param limit: The numbers of items to return. :type limit: int :rtype: List[Book] """ try: q = get_db().query(Book) if symbol is not None: q = q.filter(Book.symbol == symbol) q = q.limit(limit).offset(offset) return [e.dump() for e in q] except Exception as e: return {'message': str(e)}, 500
def list_bids(active=None, symbol=None, offset=None, limit=None): """ Lists Bids Lists buying offers :param active: Active status. Available for buy or not anymore :type active: bool :param symbol: Company or share symbol :type symbol: str :param offset: The number of items to skip before starting to collect the result set. :type offset: int :param limit: The numbers of items to return. :type limit: int :rtype: List[Bid] """ try: q = get_db().query(Bid) if active is not None: q = q.filter(Bid.active == active) if symbol is not None: q = q.filter(Bid.symbol == symbol) q = q.limit(limit).offset(offset) return [u.dump() for u in q] except Exception as e: return {'message': str(e)}, 500
def list_asks(active=None, symbol=None, offset=None, limit=None): """ Lists Asks Lists of selling offers in the marketplace :param active: Active status. Available for sell or not anymore :type active: bool :param symbol: Company or share symbol :type symbol: str :param offset: The number of items to skip before starting to collect the result set. :type offset: int :param limit: The numbers of items to return. :type limit: int :rtype: List[Ask] """ try: q = get_db().query(Ask) if active is not None: q = q.filter(Ask.active == active) if symbol is not None: q = q.filter(Ask.symbol == symbol) q = q.limit(limit).offset(offset) return [a.dump() for a in q] except Exception as e: return {'message': str(e)}, 500
def get_frontpage_products(): db = get_db() products = list(db.products.find()) random.shuffle(products) return products
def shutdown_session(exception=None): orm.get_db().remove()
def get_product_by_dest(dest): db = get_db() res = db.products.find_one({'page_url': dest}) if 'see_also' in res: res['see_also'] = {text: '/' + get_product_by_id(int_id)['page_url'] for text, int_id in res['see_also'].iteritems()} return res
def get_product_by_id(int_id): db = get_db() return db.products.find_one({'int_id': int_id})