def get(self, order_id: int = None): client_id = context.identity.id if context.identity.is_in_roles('client') \ else context.query_string['clientId'] try: if order_id is None: offset = context.query_string.get('offset', 0) limit = min(context.query_string.get('limit', 10), 10) if context.query_string['status'] == 'pending': orders = stexchange_client.order_pending( user_id=client_id, market=context.query_string['marketName'], offset=offset, limit=limit, ) elif context.query_string['status'] == 'finished': orders = stexchange_client.order_finished( user_id=client_id, market=context.query_string['marketName'], offset=offset, limit=limit, side=0, start_time=0, end_time=0, ) else: raise HttpNotFound('Bad status.') return [ order_to_dict(self.__fetch_market(), order) for order in orders['records'] ] else: if context.query_string['status'] == 'pending': order = stexchange_client.order_pending_detail( market=context.query_string['marketName'], order_id=int(order_id), ) elif context.query_string['status'] == 'finished': order = stexchange_client.order_finished_detail( order_id=int(order_id), ) else: raise HttpNotFound('Bad status.') return order_to_dict(self.__fetch_market(), order) except StexchangeException as e: raise stexchange_http_exception_handler(e)
def _ensure(id_): instance = WorkGroup.query.filter(WorkGroup.id == id_).one_or_none() if instance is None: raise HttpNotFound('Cannot find any work_group with id %s' % id_) return instance
def wrapper(*args, **kwargs): result = func(*args, **kwargs) if result is None: raise HttpNotFound() if isinstance(result, Query): return cls.dump_query(result) return result
def put(self, id_: int = None): m = Resource.query.filter(Resource.id == id_).one_or_none() if m is None: raise HttpNotFound() m.update_from_request() context.etag_match(m.__etag__) return m
def ensure_issue(id_): issue = Issue.query.filter(Issue.id == id_).one_or_none() if issue is None: raise HttpNotFound('Cannot find any issue with id %s' % id_) return issue
def edit(self, currency_symbol: str): currency = Currency.query.filter( Currency.symbol == currency_symbol).one_or_none() # TODO: These input values should not be normalized if currency is None: raise HttpNotFound() currency.update_from_request() return currency
def create(self): client_id = context.identity.id market = Market.query.filter( Market.name == context.form['marketName']).one_or_none() if market is None: raise HttpBadRequest('Market not found', 'market-not-found') side = 1 if (context.form['side'] == 'sell') else 2 amount = market.base_currency.input_to_normalized( context.form['amount']) price = market.quote_currency.input_to_normalized( context.form.get('price', None)) market.validate_ranges(type_=context.form['side'], total_amount=amount, price=price) try: if context.form['type'] == 'market': if price is not None: raise HttpBadRequest( 'Price should not be sent in market orders', 'bad-price') order = stexchange_client.order_put_market( user_id=client_id, market=market.name, side=side, amount=Currency.format_normalized_string(amount), taker_fee_rate=market.taker_commission_rate, source="nothing", # FIXME ) elif context.form['type'] == 'limit': if price is None: raise HttpBadRequest( 'Price should be sent in market orders', 'bad-price') order = stexchange_client.order_put_limit( user_id=client_id, market=market.name, side=side, amount=Currency.format_normalized_string(amount), price=Currency.format_normalized_string(price), taker_fee_rate=market.taker_commission_rate, maker_fee_rate=market.maker_commission_rate, source="nothing", # FIXME ) else: raise HttpNotFound('Bad status.') return order_to_dict(market, order) except StexchangeException as e: raise stexchange_http_exception_handler(e)
def get(self, withdraw_id: str): cryptocurrency = self.__fetch_cryptocurrency() try: withdraw = stawallet_client.get_withdraw(wallet_id=cryptocurrency.wallet_id, withdraw_id=int(withdraw_id)) if withdraw['user'] != str(context.identity.id): raise HttpNotFound() return withdraw_to_dict(cryptocurrency, withdraw) except StawalletException as e: raise HttpInternalServerError("Wallet access error")
def get(self, hash_id): try: db_id, = hashids.decode(hash_id) except ValueError: raise HttpBadRequest() url = DBSession.query(Url).filter_by(id=db_id).one_or_none() if url is None: raise HttpNotFound() raise HttpFound(url.url)
def reject(self, shetab_address_id: int): bank_card = BankCard.query.filter( BankCard.id == shetab_address_id).one_or_none() if bank_card is None: raise HttpNotFound() if bank_card.is_verified is True: raise HttpConflict('Already accepted') bank_card.error = context.form.get('error') return bank_card
def reject(self, bank_account_id: int): sheba_address = BankAccount.query.filter( BankAccount.id == bank_account_id).one_or_none() if sheba_address is None: raise HttpNotFound() if sheba_address.is_verified is True: raise HttpConflict('Already accepted') sheba_address.error = context.form.get('error') return sheba_address
def get(self, deposit_id: str): cryptocurrency = self.__fetch_cryptocurrency() try: deposit = stawallet_client.get_deposit(wallet_id=cryptocurrency.wallet_id, deposit_id=int(deposit_id)) if int(deposit['invoice']['user']) != context.identity.id: raise HttpNotFound() return deposit_to_dict(cryptocurrency, deposit) except StawalletException as e: logger.info('Wallet access error: ' + e.message) raise HttpInternalServerError("Wallet access error")
def add(self, id: int, issue_id: int): instance = self._ensure(id) issue = WorkGroup.query.filter(Issue.id == issue_id).one_or_none() if issue is None: raise HttpNotFound('Cannot find any issue with id %s' % issue_id) # p1 = Parent() # c1 = Child() # p1.children.append(c1) instance.issue.append(issue) # instance.update_from_request() DBSession.add(instance) return instance
def close(self, ticket_id: int): ticket = Ticket.query.filter(Ticket.id == ticket_id) if context.identity.is_in_roles('client'): ticket = ticket.filter(Ticket.member_id == context.identity.id) ticket = ticket.one_or_none() if ticket is None: raise HttpNotFound('Ticket not found') if ticket.is_closed is False: ticket.is_closed = True else: raise HttpConflict('Ticket already closed') return ticket
def read(self, notification_id: int = None): query = Notification.query.filter(Notification.id == notification_id) if context.identity.is_in_roles('client'): query = query.filter(Notification.member_id == context.identity.id) notification = query.one_or_none() if notification is None: raise HttpNotFound('Notification not found', 'notification-not-found') if notification.is_read is False: notification.is_read = True return notification
def get(self): if(context.query_string.get('code') is None) or \ (client_secret_data['client_id'] is None) or \ (client_secret_data['client_secret'] is None): raise HttpBadRequest body_token = dict() body_token['grant_type'] = 'authorization_code' body_token['code'] = context.query_string.get('code') body_token['client_id'] = client_secret_data['client_id'] body_token['redirect_uri'] = settings.redirect_uri_auth body_token['client_secret'] = client_secret_data['client_secret'] response_token = requests.post(settings.auth_google_uri_token, body_token) if response_token.status_code != 200: raise HttpForbidden() response_get_profile = requests.get( settings.oauth_url_google_api, headers={ 'Authorization': 'OAuth ' + json_library.loads(response_token.text)['access_token'] }) if response_get_profile.status_code != 200: raise HttpNotFound() profile = json_library.loads(response_get_profile.text) if 'refresh_token' in response_token.text: member = Member(given_name=profile['given_name'], family_name=profile['family_name'], email=profile['email'], google_access_token=json_library.loads( response_token.text)['access_token'], google_refresh_token=json_library.loads( response_token.text)['refresh_token']) DBSession.add(member) DBSession.commit() return dict(authorization=JwtPrincipal(profile).dump().decode("utf-8"))
def accept(self, shaparak_out_id: int): reference_id = context.form.get('referenceId') shaparak_out = Cashout.query.filter( Cashout.id == shaparak_out_id).one_or_none() if shaparak_out is None: raise HttpNotFound() if shaparak_out.reference_id is not None: raise HttpBadRequest('This transaction already accepted.') if shaparak_out.error is not None: raise HttpBadRequest('This transaction already has an error.') shaparak_out.reference_id = reference_id return shaparak_out
def get(self, ticket_id: int = None, inner_resource: str = None): query = Ticket.query if context.identity.is_in_roles('client'): query = query.filter(Ticket.member_id == context.identity.id) if ticket_id is not None: ticket = query.filter(Ticket.id == ticket_id).one_or_none() if ticket is None: raise HttpNotFound('Ticket not found') if inner_resource == 'messages': return self.__get_ticket_messages(ticket_id) elif inner_resource is None: return ticket else: return query raise HttpMethodNotAllowed()
def append(self, ticket_id: int): message = context.form.get('message') attachment = context.form.get('attachment', None) ticket = Ticket.query.filter(Ticket.id == ticket_id) if context.identity.is_in_roles('client'): ticket = ticket.filter(Ticket.member_id == context.identity.id) ticket = ticket.one_or_none() if ticket is None: raise HttpNotFound('Ticket not found') ticket_message = TicketMessage() ticket_message.ticket_id = ticket.id ticket_message.member_id = context.identity.id ticket_message.text = message ticket_message.is_answer = True if ticket.member_id != context.identity.id else False try: ticket_message.attachment = attachment except AspectRatioValidationError as ex: raise HttpBadRequest(str(ex), reason='invalid-aspectratio') except DimensionValidationError as ex: raise HttpBadRequest(str(ex), reason='invalid-dimensions') except (AnalyzeError, ContentTypeValidationError) as ex: raise HttpBadRequest(str(ex), reason='invalid-type') DBSession.add(ticket_message) if ticket.is_closed is True: ticket.is_closed = False return ticket_message
def __call__(self, *remaining_paths): if remaining_paths[0] == 'oak': if remaining_paths[1] == 'v2': if remaining_paths[2] == 'clients': from pyfinnotech.tests.helper import valid_mock_client_id if remaining_paths[3] == valid_mock_client_id: if remaining_paths[4] in ['ibanInquiry']: return super().__call__(*remaining_paths[4:]) elif remaining_paths[0] == 'mpg': if remaining_paths[1] == 'v2': if remaining_paths[2] == 'clients': from pyfinnotech.tests.helper import valid_mock_client_id if remaining_paths[3] == valid_mock_client_id: if remaining_paths[4] in ['cards']: return super().__call__(*remaining_paths[4:]) elif remaining_paths[0] == 'dev': if remaining_paths[1] == 'v2': if remaining_paths[2] == 'oauth2': return super().__call__(*remaining_paths[2:]) elif remaining_paths[0] == 'facility': if remaining_paths[1] == 'v2': if remaining_paths[2] == 'clients': from pyfinnotech.tests.helper import valid_mock_client_id if remaining_paths[3] == valid_mock_client_id: if remaining_paths[4] in ['users']: if remaining_paths[6] in ['sms']: if remaining_paths[7] in ['nidVerification']: return super().__call__( *remaining_paths[7:]) elif remaining_paths[4] in ['cardToIban']: return super().__call__(*remaining_paths[4:]) raise HttpNotFound()
def reject(self, shaparak_out_id: int): error = context.form.get('error') shaparak_out = Cashout.query.filter( Cashout.id == shaparak_out_id).one_or_none() if shaparak_out is None: raise HttpNotFound() if shaparak_out.reference_id is not None: raise HttpBadRequest('This transaction already accepted.') if shaparak_out.error is not None: raise HttpBadRequest('This transaction already has an error.') payment_gateway = shaparak_out.payment_gateway shaparak_out.error = error try: # Cash back (without commission) FIXME: Really without commission? stexchange_client.balance_update( user_id=shaparak_out.member_id, asset=shaparak_out.payment_gateway.fiat_symbol, # FIXME business='cashback', # FIXME business_id=shaparak_out.id, change=payment_gateway.fiat.format_normalized_string( shaparak_out.amount), detail=shaparak_out.to_dict(), ) # FIXME: Important !!!! : rollback the updated balance if # DBSession.commit() was not successful except StexchangeException as e: raise stexchange_http_exception_handler(e) return shaparak_out
def present(self, market_id: int, type_: str): # FIXME raise HttpNotFound("Deprecated")