def test_get_channel(): with session() as s: o = Object(name='Test Object') assert o.get_channel('test') == 'test-None' s.add(o) s.commit() assert o.get_channel('test') == f'test-{o.id}'
def request_edit(task_id): data = json.loads(request.data.decode("utf-8")) result = {'result': 'canceled'} good_request = True session = db.session() try: task = session.query(Task).filter(Task.id == task_id).one_or_none() if task: for key in data.keys(): if task.__getattribute__(key) != data[key]: if data[key] == 'None': data[key] = None task.__setattr__(key, data[key]) session.add(task) session.commit() result['task'] = row2dict(task) except Exception as e: good_request = False print(e) finally: if good_request: result['result'] = 'done' return jsonify(result), 201 else: abort(400) session.close()
def test_get_style(): with session() as s: o = Object(name='Test Player') p = Player(username='******') s.add(o) s.commit() o.player = p s.commit() assert not p.text_styles s = o.get_style(None) assert len(p.text_styles) == 1 assert s == TextStyle.style.default.arg
def test_delete_object(): with session() as s: c = CommunicationChannel(name='Test Channel') o = Object(name='Test Player') s.add_all([o, c]) s.commit() o.player = Player(username='******') s.add(o.player) s.commit() i = o.player_id c.banned.append(o) c.listeners.append(o) s.commit() s.delete(o) assert Player.get(i) is None assert not c.listeners assert not c.banned
def test_delete_shop(): with session() as s: o = Object(name='Test Shop Object') s.add(o) s.commit() shop = Shop() o.shop = shop s.add(shop) s.commit() item = Object(name='Test Item', fertile=True) s.add(item) s.commit() s.add(shop.add_item(item, 1.0)) s.commit() s.delete(shop) s.commit() assert shop.id is not None assert Shop.get(shop.id) is None assert not ShopItem.query(shop_id=shop.id).count()
def get_task(): session = db.session() result = {'result': 'canceled'} good_request = True try: tasks = session.query(Task).filter(Task.deleted_at == None).all() tasks_dicts = [row2dict(task) for task in tasks] result["count"] = len(tasks) result["data"] = tasks_dicts except Exception as e: print(e) good_request = False finally: if good_request: result['result'] = 'done' return jsonify(result), 201 else: abort(400) session.close()
def request_delete(task_id): result = {'result': 'canceled'} good_request = True session = db.session() try: task = session.query(Task).filter(Task.id == task_id).one_or_none() if task: task.deleted_at = datetime.now() session.add(task) session.commit() except Exception as e: print(e) good_request = False finally: if good_request: result['result'] = 'done' return jsonify(result), 201 else: abort(400) session.close()
def test_delete_communication_channel(): with session() as s: c = CommunicationChannel.count() assert CommunicationChannel.query().delete() == c # Make new channels. c1 = CommunicationChannel(name='Test1') c2 = CommunicationChannel(name='Test 2') s.add_all((c1, c2)) s.commit() for x in range(10): for channel in (c1, c2): s.add( CommunicationChannelMessage(channel_id=channel.id, text=f'Message {x}.')) s.commit() c = CommunicationChannelMessage.count() assert c != 0 deleted = len(c1.messages) s.delete(c1) s.commit() assert CommunicationChannelMessage.count() == (c - deleted) assert not CommunicationChannelMessage.query(channel_id=c1.id).count()
def request_create(): data = json.loads(json.loads(request.data.decode("utf-8"))) result = {'result': 'canceled'} session = db.session() good_request = True try: task = Task(subject=data['subject'], description=data['description'], status="inwork", priority=data['priority']) session.add(task) session.commit() result['task'] = row2dict(task) except Exception as e: print(e) good_request = False finally: if good_request: result['result'] = 'done' return jsonify(result) else: abort(400) session.close()
def main(): """Do stuff.""" args = parser.parse_args() logging.basicConfig(level='INFO') started = time() load_db(args.infile) logging.info('Objects loaded: %d (%.2f seconds).', Base.number_of_objects(), time() - started) with session() as s: for cls in get_classes(): if cls in (Hotkey, Command, Credit, Task): continue # Don't delete those. q = cls.query() logging.info('Deleting %d objects from table %s.', q.count(), cls.__table__.name) q.delete() s.commit() started = time() dump_db(args.outfile) logging.info('Objects dumped: %d (%.2f seconds).', Base.number_of_objects(), time() - started)
def no_expire(): s = db.session() s.expire_on_commit = False yield s.expire_on_commit = True
def insert_user(self, ge_user): phone_number = None if 'DELETED' in ge_user['phone'] else ge_user[ 'phone'] if not phone_number: print("Phone Deleted, Skipping") return if ge_user['status'] == 'Deleted': print("User Deleted, Skipping") return processed_phone = proccess_phone_number(phone_number) existing_user = User.query.filter_by( phone=processed_phone).execution_options(show_all=True).first() if existing_user: print(f'User already exists for phone {processed_phone}') return business_usage = None if ge_user.get('business_type') is not None: sempo_category = GE_BUSINESS_CATEGORY_MAPPINGS.get( ge_user['business_type']) if sempo_category: business_usage = TransferUsage.query.filter_by( name=sempo_category).first() organsation = db.session.query(Organisation).get( self.sempo_organisation_id) try: sempo_user = create_transfer_account_user( first_name=ge_user['first_name'], last_name=ge_user['last_name'], organisation=organsation, phone=phone_number, preferred_language=ge_user['preferred_language'], location=ge_user['location'], business_usage=business_usage) sempo_user.pin_hash = ge_user['encrypted_pin'] sempo_user.is_activated = ge_user[ 'status'] == 'Active' # Is this the correct way to find this out? sempo_user.default_transfer_account.is_approved = True sempo_user.is_disabled = False sempo_user.is_phone_verified = True sempo_user.is_self_sign_up = False sempo_user.terms_accepted = False sempo_user.created = ge_user['created_at'] sempo_user.is_market_enabled = int(ge_user['market_enabled']) == 1 sempo_user.custom_attributes = self.create_custom_attributes( ge_user) if ge_user['token_agents.id'] is not None: sempo_user.set_held_role('TOKEN_AGENT', 'grassroots_token_agent') else: # Is this the correct way to find this out or can a benificiary also be a token agent # Or is there some field where you can find this out? sempo_user.set_held_role('BENEFICIARY', 'beneficiary') if ge_user['group_accounts.id'] is not None: sempo_user.set_held_role('GROUP_ACCOUNT', 'grassroots_group_account') db.session.flush() return sempo_user except (IntegrityError, InvalidRequestError) as e: print(e) db.session().rollback()
from server.db import (Base, Currency, Object, CreditCard, session, Room, finalise_db) from server.server import server assert server is not None # Stop pytest from moaning. class Message(Exception): """A message was received.""" class CustomPlayer: def message(self, *args, **kwargs): raise Message(*args, **kwargs) Base.metadata.create_all() finalise_db() with session() as s: s.add(Currency(value=1.0)) s.commit() c = CreditCard(value=5) s.add(c) s.commit() r = Room(name='Test Room') s.add(r) s.commit() p = Object(name='Test Player', location_id=r.id) o = Object(name='Credit Card', holder=p, credit_card_id=c.id) s.add_all([p, o])