示例#1
0
def store_edit(store_id):
    if not current_user.has_capability('editor'):
        abort(403)
    store = Store.query.get_or_404(store_id)
    if not current_user.has_capability(
            'admin') and store.region not in current_user.region:
        abort(403)
    form = StoreForm(obj=store)
    if form.validate_on_submit():
        opening_times_data = {}
        for field in ['all', 'delivery', 'pickup']:
            opening_times_data[field] = getattr(form,
                                                'opening_times_%s' % field)
            delattr(form, 'opening_times_%s' % field)
        form.populate_obj(store)
        setattr(store, 'revisited_%s' % current_user.role, datetime.utcnow())
        db.session.add(store)
        db.session.commit()
        save_opening_times_form(form, opening_times_data, store)
        es_index_store_delay.delay(store.id)
        create_store_revision_delay.delay(store.id)
        upload_files(form, store, 'store')
        flash('Geschäft erfolgreich gespeichert', 'success')
        return redirect('/admin/stores')
    return render_template('store-edit.html',
                           form=form,
                           store=store,
                           opening_times=get_opening_times_for_form(store.id))
def store_new():
    if not current_user.has_capability('editor'):
        abort(403)
    form = StoreNewForm()
    if form.validate_on_submit():
        store = Store()
        opening_times_data = {}
        for field in ['all', 'delivery', 'pickup']:
            opening_times_data[field] = getattr(form,
                                                'opening_times_%s' % field)
            delattr(form, 'opening_times_%s' % field)
        form.populate_obj(store)
        db.session.add(store)
        db.session.commit()
        save_opening_times_form(form, opening_times_data, store)
        es_index_store_delay.delay(store.id)
        create_store_revision_delay.delay(store.id)
        upload_files(form, store, 'store')
        flash('Geschäft erfolgreich gespeichert', 'success')
        return redirect('/admin/stores')
    return render_template('store-new.html', form=form)
示例#3
0
def store_suggestion_show(suggestion_id):
    if not current_user.has_capability('editor'):
        abort(403)
    object_dump = ObjectDump.query.get_or_404(suggestion_id)
    store = Store.query.get_or_404(object_dump.object_id)
    if not current_user.has_capability('admin') and store.region not in current_user.region:
        abort(403)
    suggestion = Store()
    suggestion.load_cache(object_dump.data)
    opening_times_data = {
        'new': [],
        'old': [],
        'both': []
    }
    new_opening_times = object_dump.data.get('opening_time', [])
    opening_times = OpeningTime.query.filter_by(store_id=store.id).order_by(OpeningTime.weekday, OpeningTime.open).all()
    for opening_time in opening_times:
        found = -1
        for i in range(0, len(new_opening_times)):
            if opening_time.weekday != int(new_opening_times[i]['weekday']):
                continue
            if opening_time.open != new_opening_times[i]['open']:
                continue
            if opening_time.close != new_opening_times[i]['close']:
                continue
            found = i
            break
        if found >= 0:
            opening_times_data['both'].append(opening_time)
            del new_opening_times[found]
            continue
        opening_times_data['old'].append(opening_time)
    for new_opening_time in new_opening_times:
        new_opening_time_obj = OpeningTime()
        new_opening_time_obj.load_cache(new_opening_time)
        opening_times_data['new'].append(new_opening_time_obj)
    form = StoreSuggestionMergeForm()
    if form.validate_on_submit():
        if form.abort.data:
            return redirect('/admin/store/suggestions')
        if form.delete.data:
            object_dump.deleted = True
            db.session.add(object_dump)
            db.session.commit()
            flash('Verbesserungsvorschlag wurde erfolgreich gelöscht.', 'success')
            return redirect('/admin/store/suggestions')
        if form.edit.data:
            return redirect('/admin/store/suggestion/%s/edit' % suggestion.id)
        store.load_cache(object_dump.data)
        store.revisited_user = object_dump.created
        db.session.add(store)
        db.session.commit()
        new_opening_times_dict = {'all': [], 'delivery': [], 'pickup': []}
        for new_opening_time in object_dump.data.get('opening_time', []):
            new_opening_times_dict[new_opening_time.get('type', 'all')].append(new_opening_time)
        save_opening_times({'all': True, 'delivery': True, 'pickup': True}, new_opening_times_dict, store)
        es_index_store_delay.delay(store.id)
        create_store_revision_delay.delay(store.id)
        object_dump.settled = True
        db.session.add(object_dump)
        db.session.commit()
        flash('Verbesserungsvorschlag wurde erfolgreich gespeichert.', 'success')
        return redirect('/admin/store/suggestions')
    return render_template(
        'store-suggestion-show.html',
        store=store,
        object_dump=object_dump,
        suggestion=suggestion,
        opening_times_data=opening_times_data,
        form=form
    )