Esempio n. 1
0
def shutdown_session(exception=None):
    try:
        session.commit()
    except Exception as e:
        session.rollback()
        logging.error(e.message)
    session.remove()
Esempio n. 2
0
def gather_form_data():
    location_dict = defaultdict(lambda: None, {value: field for value, field in request.form.iteritems()})
    location_instance = Location(**location_dict)
    session.add(location_instance)
    session.commit()

    return location_instance
Esempio n. 3
0
def gather_form_data():
    prison_type_dict = defaultdict(
        lambda: None,
        {value: field
         for value, field in request.form.iteritems()})
    prison_type_instance = PrisonType(**prison_type_dict)
    session.add(prison_type_instance)
    session.commit()

    return prison_type_instance
Esempio n. 4
0
def gather_form_data():
    evidence_type_dict = defaultdict(lambda: None, {value: field for value, field in request.form.iteritems()})

    event = request.form.getlist('event')
    evs = event_factory()
    evidence_type_dict['event'] = get_name_from_id(event[0], evs)

    evidence_type_instance = EvidenceType(**evidence_type_dict)
    session.add(evidence_type_instance)
    session.commit()

    return evidence_type_instance
Esempio n. 5
0
def gather_form_data(event=None):
    event_dict = defaultdict(
        lambda: None, {
            value: field if field else None
            for value, field in request.form.iteritems()
        })

    uploaded_docs = request.files.getlist("documents")
    event_dict['documents'] = []
    if uploaded_docs:
        for uploaded_file in uploaded_docs:
            filename = secure_filename(uploaded_file.filename)
            if not filename == '':
                uploaded_file.save(os.path.join(DOC_FOLDER, filename))
                doc = Document()
                doc.filename = filename
                session.add(doc)
                session.commit()
                event_dict['documents'].append(doc)

    location_ids = request.form.getlist('locations')
    event_dict['locations'] = [
        get_name_from_id(x, location_factory()) for x in location_ids
    ]

    prison_ids = request.form.getlist('prisons')
    event_dict['prisons'] = [
        get_name_from_id(x, prison_factory()) for x in prison_ids
    ]

    release_types_ids = request.form.getlist('release_types')
    event_dict['release_types'] = [
        get_name_from_id(x, release_type_factory()) for x in release_types_ids
    ]

    sources = request.form.get('sources')
    sources_list = list(set([x.strip() for x in sources.split(",")]))
    event_dict['sources'] = find_things_by_name(sources_list, Source)

    witnesses = request.form.get('witnesses')
    witnesses_list = list(set([x.strip() for x in witnesses.split(",")]))
    event_dict['witnesses'] = find_things_by_name(witnesses_list, Actor)

    victims = request.form.get('victims')
    victims_list = list(set([x.strip() for x in victims.split(",")]))
    event_dict['victims'] = find_things_by_name(victims_list, Actor)

    perpetrators = request.form.get('perpetrators')
    perpetrators_list = list(set([x.strip() for x in perpetrators.split(",")]))
    event_dict['perpetrators'] = find_things_by_name(perpetrators_list, Actor)

    event_types = request.form.getlist('event_types')
    event_dict['event_types'] = [
        get_name_from_id(x, event_type_factory()) for x in event_types
    ]
    if event.report_id:
        report = session.query(Report).get(event.report_id)
        report.text = request.form.get('report')
        event_dict['report'] = report
    else:
        event_dict['report'] = Report(text=request.form.get('report'))

    event_dict['owner_id'] = current_user.id
    #event_instance = Event(**event_dict)
    #event_instance.id = event.id if event else None

    if event is not None:
        for key, value in event_dict.iteritems():
            setattr(event, key, value)
        session.merge(event)
    else:
        session.add(event_instance)
    session.flush()

    return event