def handle(): data = json.loads(request.data) # handle only "list" change event if "listAfter" in data["action"]["data"]: board_list_id = data["action"]["data"]["listAfter"]["id"] card_id = data["model"]["id"] associations = AssociationsRepository.find_by_card_id(card_id) hubspot = create_client() for association in associations: deal_id = association.deal_id deal = hubspot.crm.deals.basic_api.get_by_id(deal_id) mapping = (session.query(Mapping).filter_by( board_list_id=board_list_id, pipeline_id=deal.properties["pipeline"], ).first()) session.commit() if mapping is not None: simple_public_object_input = SimplePublicObjectInput( properties={"dealstage": mapping.pipeline_stage_id}, ) hubspot.crm.deals.basic_api.update( deal_id=deal_id, simple_public_object_input=simple_public_object_input, ) return "", HTTPStatus.OK
def list(): events = session.query(Event).order_by( Event.occurred_at.desc()).limit(50).all() session.commit() hubspot = create_client() inputs = [SimplePublicObjectId(id=e.object_id) for e in events] batch_read_input_simple_public_object_id = BatchReadInputSimplePublicObjectId( inputs=inputs, ) contacts = hubspot.crm.contacts.batch_api.read( batch_read_input_simple_public_object_id= batch_read_input_simple_public_object_id, ).results contacts_dict = { int(contact.id): { "fullname": (contact.properties["firstname"] or "") + " " + (contact.properties["lastname"] or "") } for contact in contacts } return render_template( "events/list.html", events=events, contacts_dict=contacts_dict, now=datetime.datetime.now(), )
def find_one(cls): settings = session.query(Settings).first() if settings is None: settings = Settings() session.add(settings) return settings
def updates(): after = datetime.datetime.fromisoformat(request.args.get("after")) updates_count = session.query(Event).filter( Event.created_at > after).count() session.commit() return jsonify({ "updatesCount": updates_count, })
def delete_by_id(cls, mapping_id): session.delete(session.query(Mapping).get(mapping_id))
def get(cls, mapping_id): return session.query(Mapping).get(mapping_id)
def find_by(cls, **kwargs): return session.query(Mapping).filter_by(**kwargs).all()
def is_deal_associated(cls, deal_id): count = session.query(Association).filter_by(deal_id=deal_id).count() return count > 0
def delete_by_deal_id(cls, deal_id): session.query(Association).filter_by(deal_id=deal_id).delete()
def find_by_card_id(cls, card_id): associations = session.query(Association).filter_by( card_id=card_id).all() return associations
def find_one_by_deal_id(cls, deal_id): association = session.query(Association).filter_by( deal_id=deal_id).first() return association
def get(cls, webhooks_id): return session.query(Webhook).get(webhooks_id)
def find_outdated(cls, url): print(url, flush=True) return session.query(Webhook).filter(Webhook.url != url).all()
def find_by(cls, **kwargs): return session.query(Webhook).filter_by(**kwargs).all()