Example #1
0
def search(collection_name, query):
    # MODEL
    model_name = inflect_engine.singular_noun(collection_name.title(), 1)
    model = app.model[model_name]

    # GENERIC METHOD
    print('model_name', model_name, GENERIC_SEARCH_MODEL_NAMES)
    if model_name not in GENERIC_SEARCH_MODEL_NAMES:
        return None

    # CREATE GENERIC FILTER
    search_filter = get_search_filter([model], query)

    # SPECIAL FILTER
    if model == app.model.Offerer:
        search_filter = and_(
            search_filter, model.id.in_([o.id for o in current_user.offerers]))
    elif model in [app.model.Event, app.model.Thing]:
        ## TODO:
        # check that the searched entities are associated with
        # an offerer of the current_user
        pass

    # FILTER
    return model.query\
                .filter(search_filter)
Example #2
0
def patch_occasion(occasionType, occasionId):
    model_name = inflect_engine.singular_noun(occasionType.title(), 1)

    # UPDATE THE OCCASION
    occasion = app.model[model_name].query\
                                    .filter_by(id=dehumanize(occasionId))\
                                    .first_or_404()
    occasion_dict = request.json.get('occasion')
    if occasion_dict:
        update(occasion, occasion_dict)
        app.model.PcObject.check_and_save(occasion)
    first_occurence = occasion.occurences[0]
    venue = first_occurence.venue
    offerer = venue.managingOfferer

    # UPDATE CORRESPONDING EVENT OCCURENCES
    event_occurences = request.json.get('eventOccurences')
    if event_occurences:
        for event_occurence_dict in event_occurences:
            if event_occurence_dict.get('DELETE') == '_delete_':
                app.model.Offer\
                         .query\
                         .filter_by(eventOccurenceId=dehumanize(event_occurence_dict['id']))\
                         .delete()
                app.model.EventOccurence\
                         .query\
                         .filter_by(id=dehumanize(event_occurence_dict['id']))\
                         .delete()
                app.db.session.commit()
            else:
                create_event_occurence(event_occurence_dict, occasion, offerer,
                                       venue)
    return jsonify(occasion._asdict(include=OCCASION_INCLUDES)), 200
Example #3
0
def post_occasion(occasionType):
    model_name = inflect_engine.singular_noun(occasionType.title(), 1)

    # CREATE THE OCCASION (EVENT OR THING)
    occasion = app.model[model_name]()
    occasion_dict = request.json['occasion']
    update(occasion, occasion_dict)
    app.model.PcObject.check_and_save(occasion)

    # DETERMINE OFFERER
    offerer = app.model.Offerer.query\
                       .filter_by(id=dehumanize(occasion_dict['offererId']))\
                       .first_or_404()

    # DETERMINE VENUE
    venue = app.model.Venue.query\
                           .filter_by(id=dehumanize(occasion_dict['venueId']))\
                           .first_or_404()

    # CREATE CORRESPONDING EVENT OCCURENCES
    event_occurences = request.json.get('eventOccurences')
    if event_occurences:
        for event_occurence_dict in event_occurences:
            create_event_occurence(event_occurence_dict, occasion, offerer,
                                   venue)

    return jsonify(occasion._asdict(include=OCCASION_INCLUDES)), 201
Example #4
0
def get_occasion(occasionType, occasionId):
    model_name = inflect_engine.singular_noun(occasionType.title(), 1)
    occasion = app.model[model_name]\
                  .query.filter_by(id=dehumanize(occasionId))\
                  .first_or_404()
    occasion_dict = occasion._asdict(include=OCCASION_INCLUDES)
    return jsonify(occasion_dict)
Example #5
0
def get(collection_name, filter=None, resolve=None, **kwargs):
    if resolve is None:
        resolve = lambda obj: obj
    model_name = get_camel_string(
        inflect_engine.singular_noun(collection_name, 1))
    model = app.model[model_name[0].upper() + model_name[1:]]
    query = model.query.filter() if filter is None else model.query.filter(
        filter)
    include = includes.get(collection_name)
    return listify(query, include, resolve, **kwargs)
Example #6
0
def post_storage_file(collectionName, id, index):
    model_name = inflect_engine.singular_noun(collectionName.title(), 1)
    if model_name in GENERIC_STORAGE_MODEL_NAMES:
        model = app.model[model_name]
        entity = model.query.filter_by(id=dehumanize(id)).first_or_404()
        if model_name == 'Mediation':
            offerer = entity.event.occurences[0].offer[0].offerer
            if offerer not in current_user.offerers:
                return jsonify({
                    'text':
                    "user is not allowed to add mediation in this offerer"
                }), 400
        entity.save_thumb(request.files['file'].read(), int(index))
        return jsonify(entity._asdict()), 200
    else:
        return jsonify({'text':
                        "upload is not authorized for this model"}), 400