def event_delete_tag(user, org, event_id, tag_id): """ Remove a tag from an event. """ e = Event.query\ .filter_by(id=event_id, org_id=org.id)\ .first() if not e: raise NotFoundError( 'An Event with ID {} does not exist.' .format(event_id)) if tag_id not in e.tag_ids: raise RequestError( 'An Event with ID {} does not currently have an association ' 'with a Tag with ID {}.' .format(event_id, tag_id)) # remove tag from event for tag in e.tags: if tag.id == tag_id: e.tags.remove(tag) # update metrics associated with these content item ids if len(e.content_item_ids): rollup_metric.content_summary_from_events(org, e.content_item_ids) db.session.add(e) db.session.commit() # return modified event return jsonify(e)
def event_add_thing(user, org, event_id, content_item_id): """ Add a thing to an event. """ e = Event.query.filter_by(id=event_id, org_id=org.id).first() if not e: raise NotFoundError( 'An Event with ID {} does not exist.'.format(event_id)) if not e.status == 'approved': raise RequestError( 'You must first approve an Event before adding additional ContentItems.') c = ContentItem.query\ .filter_by(id=content_item_id, org_id=org.id)\ .first() if not c: raise RequestError( 'A ContentItem with ID {} does not exist.' .format(content_item_id)) # add content item to event if c.id not in e.content_item_ids: e.content_items.append(c) db.session.add(e) db.session.commit() # update event-level metrics for this content item id rollup_metric.content_summary_from_events(org, [content_item_id]) # return modified event return jsonify(e)
def event_delete_content_item(user, org, event_id, content_item_id): """ Remove a thing from an event. """ e = Event.query\ .filter_by(id=event_id, org_id=org.id)\ .first() if not e: raise NotFoundError( 'An Event with ID {} does not exist.'.format(event_id)) c = ContentItem.query\ .filter_by(id=content_item_id, org_id=org.id)\ .first() if not c: raise RequestError( 'A ContentItem with ID {} does not exist.'.format(content_item_id)) if content_item_id not in e.content_item_ids: raise RequestError( 'An Event with ID {} does not currently have an association ' 'with a ContentItem with ID {}'.format(event_id, content_item_id)) # remove the content item form the event. e.content_items.remove(c) db.session.add(e) db.session.commit() # update event-level metrics for this content item id rollup_metric.content_summary_from_events(org, [content_item_id]) # return modified event return jsonify(e)
def event_add_thing(user, org, event_id, content_item_id): """ Add a thing to an event. """ e = Event.query.filter_by(id=event_id, org_id=org.id).first() if not e: raise NotFoundError( 'An Event with ID {} does not exist.'.format(event_id)) if not e.status == 'approved': raise RequestError( 'You must first approve an Event before adding additional ContentItems.' ) c = ContentItem.query\ .filter_by(id=content_item_id, org_id=org.id)\ .first() if not c: raise RequestError( 'A ContentItem with ID {} does not exist.'.format(content_item_id)) # add content item to event if c.id not in e.content_item_ids: e.content_items.append(c) db.session.add(e) db.session.commit() # update event-level metrics for this content item id rollup_metric.content_summary_from_events(org, [content_item_id]) # return modified event return jsonify(e)
def event_delete_tag(user, org, event_id, tag_id): """ Remove a tag from an event. """ e = Event.query\ .filter_by(id=event_id, org_id=org.id)\ .first() if not e: raise NotFoundError( 'An Event with ID {} does not exist.'.format(event_id)) if tag_id not in e.tag_ids: raise RequestError( 'An Event with ID {} does not currently have an association ' 'with a Tag with ID {}.'.format(event_id, tag_id)) # remove tag from event for tag in e.tags: if tag.id == tag_id: e.tags.remove(tag) # update metrics associated with these content item ids if len(e.content_item_ids): rollup_metric.content_summary_from_events(org, e.content_item_ids) db.session.add(e) db.session.commit() # return modified event return jsonify(e)
def event_delete(user, org, event_id): """ Delete an individual event. Here, we do not explicitly "delete" the event, but update it's status instead. This will help when polling recipes for new events since we'll be able to ensure that we do not create duplicate events. """ e = Event.query\ .filter_by(id=event_id, org_id=org.id)\ .first() if not e: raise NotFoundError( 'An Event with ID {} does not exist.' .format(event_id)) if arg_bool('force', False): db.session.delete(e) db.session.commit() return delete_response() # update event metrics associated with these content item ids if len(e.content_item_ids): rollup_metric.content_summary_from_events(org, e.content_item_ids) # remove associations # from: # http://stackoverflow.com/questions/9882358/how-to-delete-rows-from-a-table-using-an-sqlalchemy-query-without-orm d = events_tags\ .delete(events_tags.c.event_id == event_id) db.session.execute(d) d = content_items_events\ .delete(content_items_events.c.event_id == event_id) db.session.execute(d) # update event e.status = 'deleted' db.session.add(e) db.session.commit() # return modified event return delete_response()
def event_delete(user, org, event_id): """ Delete an individual event. Here, we do not explicitly "delete" the event, but update it's status instead. This will help when polling recipes for new events since we'll be able to ensure that we do not create duplicate events. """ e = Event.query\ .filter_by(id=event_id, org_id=org.id)\ .first() if not e: raise NotFoundError( 'An Event with ID {} does not exist.'.format(event_id)) if arg_bool('force', False): db.session.delete(e) db.session.commit() return delete_response() # update event metrics associated with these content item ids if len(e.content_item_ids): rollup_metric.content_summary_from_events(org, e.content_item_ids) # remove associations # from: # http://stackoverflow.com/questions/9882358/how-to-delete-rows-from-a-table-using-an-sqlalchemy-query-without-orm d = events_tags\ .delete(events_tags.c.event_id == event_id) db.session.execute(d) d = content_items_events\ .delete(content_items_events.c.event_id == event_id) db.session.execute(d) # update event e.status = 'deleted' db.session.add(e) db.session.commit() # return modified event return delete_response()
def event_add_tag(user, org, event_id, tag_id): """ Add a tag to an event. """ e = Event.query\ .filter_by(id=event_id, org_id=org.id)\ .first() if not e: raise NotFoundError( 'An Event with ID {} does not exist.' .format(event_id)) if not e.status == 'approved': raise RequestError( 'You must first approve an Event before adding additional Tags.') tag = Tag.query\ .filter_by(id=tag_id, org_id=org.id)\ .first() if not tag: raise NotFoundError( 'Tag with ID {} does not exist.' .format(tag_id)) if tag.type != 'impact': raise RequestError( 'Events can only be assigned Impact Tags.') if tag.id not in e.tag_ids: e.tags.append(tag) # update event metrics associated with these content item ids if len(e.content_item_ids): rollup_metric.content_summary_from_events(org, e.content_item_ids) db.session.add(e) db.session.commit() # return modified event return jsonify(e)
def event_delete_content_item(user, org, event_id, content_item_id): """ Remove a thing from an event. """ e = Event.query\ .filter_by(id=event_id, org_id=org.id)\ .first() if not e: raise NotFoundError( 'An Event with ID {} does not exist.'.format(event_id)) c = ContentItem.query\ .filter_by(id=content_item_id, org_id=org.id)\ .first() if not c: raise RequestError( 'A ContentItem with ID {} does not exist.' .format(content_item_id)) if content_item_id not in e.content_item_ids: raise RequestError( 'An Event with ID {} does not currently have an association ' 'with a ContentItem with ID {}'.format(event_id, content_item_id)) # remove the content item form the event. e.content_items.remove(c) db.session.add(e) db.session.commit() # update event-level metrics for this content item id rollup_metric.content_summary_from_events(org, [content_item_id]) # return modified event return jsonify(e)
def event_add_tag(user, org, event_id, tag_id): """ Add a tag to an event. """ e = Event.query\ .filter_by(id=event_id, org_id=org.id)\ .first() if not e: raise NotFoundError( 'An Event with ID {} does not exist.'.format(event_id)) if not e.status == 'approved': raise RequestError( 'You must first approve an Event before adding additional Tags.') tag = Tag.query\ .filter_by(id=tag_id, org_id=org.id)\ .first() if not tag: raise NotFoundError('Tag with ID {} does not exist.'.format(tag_id)) if tag.type != 'impact': raise RequestError('Events can only be assigned Impact Tags.') if tag.id not in e.tag_ids: e.tags.append(tag) # update event metrics associated with these content item ids if len(e.content_item_ids): rollup_metric.content_summary_from_events(org, e.content_item_ids) db.session.add(e) db.session.commit() # return modified event return jsonify(e)
def merge_tags(user, org, from_tag_id, to_tag_id): """ Merge two tags of the same type and all their associations. from_tag_id is deleted and it's associations are merged into to_tag_id """ from_t = Tag.query\ .filter_by(id=from_tag_id, org_id=org.id)\ .first() if not from_t: raise NotFoundError( 'Tag with ID "{}" does not exist."' .format(from_tag_id)) to_t = Tag.query\ .filter_by(id=to_tag_id, org_id=org.id)\ .first() if not to_t: raise NotFoundError( 'Tag with ID "{}" does not exist."' .format(to_tag_id)) if not from_t.type == to_t.type: raise RequestError('You can only merge tags of the same type.') if from_t.type == 'subject': # re associate content stmt = update(content_items_tags)\ .where(content_items_tags.c.tag_id == from_tag_id)\ .values(tag_id=to_tag_id) db.session.execute(stmt) # remove from tag db.session.delete(from_t) db.session.commit() if from_t.type == 'impact': # re associate events stmt = update(events_tags)\ .where(events_tags.c.tag_id == from_tag_id)\ .values(tag_id=to_tag_id) db.session.execute(stmt) # get all associated content item IDS. content_item_ids = [ c.id for e in from_t.events for c in e.content_item_ids] content_item_ids.append( [c.id for e in to_t.events for c in e.content_item_ids]) # remove from tag db.session.delete(from_t) db.session.commit() # update event metrics if len(content_item_ids): # update event-level metrics for this content item id rollup_metric.content_summary_from_events(org, content_item_ids) return jsonify(to_t)
def event_update(user, org, event_id): """ Modify an individual event. """ e = Event.query\ .filter_by(id=event_id, org_id=org.id)\ .first() if not e: raise NotFoundError( 'An Event with ID {} does not exist.' .format(event_id)) # get request data req_data = request_data() # fetch tag and thing tag_ids = listify_data_arg('tag_ids') content_item_ids = listify_data_arg('content_item_ids') # a list of content items to apply impact tags to. if len(tag_ids): tags = Tag.query\ .filter_by(org_id=org.id)\ .filter(Tag.id.in_(tag_ids))\ .all() if not len(tags): raise RequestError( 'Tag(s) with ID(s) {} do(es) not exist.' .format(tag_ids)) for tag in tags: # validate tag if tag.type != 'impact': raise RequestError('Events can only be assigned Impact Tags.') # add it if tag.id not in e.tag_ids: e.tags.append(tag) if len(content_item_ids): content_items = ContentItem.query\ .filter_by(org_id=org.id)\ .filter(ContentItem.id.in_(content_item_ids))\ .all() if not len(content_items): raise RequestError( 'ContentItem(s) with ID(s) {} do(es) not exist.' .format(tag_ids)) # add content items for c in content_items: if c.id not in e.content_item_ids: e.content_items.append(c) # filter out any non-columns columns = get_table_columns(Event) for k in req_data.keys(): if k not in columns: req_data.pop(k) # update fields for k, v in req_data.items(): setattr(e, k, v) # ensure no one sneakily/accidentally # updates an organization id e.org_id = org.id # update event metrics associated with these content item ids if len(e.content_item_ids): rollup_metric.content_summary_from_events(org, e.content_item_ids) # commit changes db.session.add(e) db.session.commit() # return modified event return jsonify(e)
def event_update(user, org, event_id): """ Modify an individual event. """ e = Event.query\ .filter_by(id=event_id, org_id=org.id)\ .first() if not e: raise NotFoundError( 'An Event with ID {} does not exist.'.format(event_id)) # get request data req_data = request_data() # fetch tag and thing tag_ids = listify_data_arg('tag_ids') content_item_ids = listify_data_arg('content_item_ids') # a list of content items to apply impact tags to. if len(tag_ids): tags = Tag.query\ .filter_by(org_id=org.id)\ .filter(Tag.id.in_(tag_ids))\ .all() if not len(tags): raise RequestError( 'Tag(s) with ID(s) {} do(es) not exist.'.format(tag_ids)) for tag in tags: # validate tag if tag.type != 'impact': raise RequestError('Events can only be assigned Impact Tags.') # add it if tag.id not in e.tag_ids: e.tags.append(tag) if len(content_item_ids): content_items = ContentItem.query\ .filter_by(org_id=org.id)\ .filter(ContentItem.id.in_(content_item_ids))\ .all() if not len(content_items): raise RequestError( 'ContentItem(s) with ID(s) {} do(es) not exist.'.format( tag_ids)) # add content items for c in content_items: if c.id not in e.content_item_ids: e.content_items.append(c) # filter out any non-columns columns = get_table_columns(Event) for k in req_data.keys(): if k not in columns: req_data.pop(k) # update fields for k, v in req_data.items(): setattr(e, k, v) # ensure no one sneakily/accidentally # updates an organization id e.org_id = org.id # update event metrics associated with these content item ids if len(e.content_item_ids): rollup_metric.content_summary_from_events(org, e.content_item_ids) # commit changes db.session.add(e) db.session.commit() # return modified event return jsonify(e)