def patch(self, contact_id): kwargs = contact_patch_parser.parse_args() db_session.query(Contact).filter_by(id=contact_id).update(kwargs) db_session.commit() contact = Contact.query.get(contact_id) return contact
def test_delete(client): c = Contact(username='******', first_name='To', last_name='Delete') db_session.add(c) db_session.commit() # `c` should be a deleted object by the end of this test, # so grab hold of the id here: contact_id = c.id r = client.delete(f'/contact/{contact_id}') assert r.status_code == 200 q = Contact.query.filter_by(id=contact_id) assert q.count() == 0
def post(self): kwargs = contact_post_parser.parse_args() if 'email' in kwargs: addresses = kwargs.pop('email') contact = Contact(**kwargs) db_session.add(contact) try: db_session.commit() # Should really catch sqlite.IntegrityError here but # doing so skips the except block... except Exception: db_session.rollback() raise ContactUsernameAlreadyExistsException if addresses: for address in addresses: contact.emails.append(Email(email_address=address)) db_session.add(contact) db_session.commit() return contact
def delete(self, contact_id): c = db_session.query(Contact).filter_by(id=contact_id).one_or_none() db_session.delete(c) # Use `db_session.delete` to ensure a cascade delete db_session.commit() return {'message': f'Contact with id {contact_id} deleted.'}