Exemple #1
0
def create(activity_id=None):
    # Need to be logged in + actie group or admin etc.
    if not ModuleAPI.can_write('activity'):
        return abort(403)

    if activity_id:

        activity = Activity.query.get(activity_id)

        if not activity:
            abort(404)

        title = _("Edit") + " " + str(activity.name)
    else:
        activity = Activity()
        title = _('Create activity')

    form = CreateForm(request.form, activity)

    if request.method == 'POST':
        if form.validate_on_submit():

            picture = activity.picture

            form.populate_obj(activity)

            file = request.files['picture']

            if file.filename and allowed_file(file.filename):
                picture = secure_filename(file.filename)

                if not activity.picture and os.path.isfile(
                        os.path.join('app/static/activity_pictures', picture)):
                    flash(_('An image with this name already exists.'),
                          'danger')
                    return render_template('activity/create.htm',
                                           activity=activity,
                                           form=form,
                                           title=title)

                fpath = os.path.join('app/static/activity_pictures', picture)
                file.save(fpath)
                os.chmod(fpath, 0o644)

                # Remove old picture
                if activity.picture:
                    try:
                        os.remove(os.path.join(
                            'app/static/activity_pictures',
                            activity.picture))
                    except OSError:
                        print(_('Cannot delete image, image does not exist') +
                              ": " + str(activity.picture))

            elif not picture:
                picture = None

            activity.venue = 1  # Facebook ID location, not used yet  # noqa

            # Set a custom_form if it actually exists
            form_id = int(form.form_id.data)
            if form_id == 0:
                form_id = None

            activity.form_id = form_id

            activity.picture = picture
            activity.owner_id = current_user.id

            if activity.id and activity.google_event_id:
                flash(_('The activity has been edited.'), 'success')

                google.update_activity(activity.google_event_id,
                                       form.nl_name.data,
                                       form.nl_description.data,
                                       form.location.data,
                                       form.start_time.data.isoformat(),
                                       form.end_time.data.isoformat())
            else:
                flash(_('The activity has been created.'), 'success')

                google_activity = google.insert_activity(
                    form.nl_name.data, form.nl_description.data,
                    form.location.data, form.start_time.data.isoformat(),
                    form.end_time.data.isoformat())

                if google_activity:
                    activity.google_event_id = google_activity['id']

            db.session.add(activity)
            db.session.commit()

            return redirect(url_for('activity.get_activity',
                                    activity_id=activity.id))
        else:
            flash_form_errors(form)

    return render_template('activity/create.htm', activity=activity, form=form,
                           title=title)
Exemple #2
0
def create(activity_id=None):
    if activity_id:

        activity = Activity.query.get(activity_id)

        if not activity:
            abort(404)

        title = _("Edit") + " " + str(activity.name)
    else:
        activity = Activity()
        title = _('Create activity')

    form = init_form(CreateForm, obj=activity)

    if request.method == 'POST':

        if form.validate_on_submit():
            form.populate_obj(activity)

            # Facebook ID location, not used yet
            activity.venue = 1

            # Set a custom_form if it actually exists
            form_id = int(form.form_id.data)
            if form_id == 0:
                form_id = None

            activity.form_id = form_id
            activity.owner_id = current_user.id

            if activity.id and activity.google_event_id:
                flash(_('The activity has been edited.'), 'success')

                google.update_activity(activity.google_event_id,
                                       form.nl_name.data,
                                       form.nl_description.data,
                                       form.location.data,
                                       form.start_time.data.isoformat(),
                                       form.end_time.data.isoformat())
            else:
                flash(_('The activity has been created.'), 'success')

                google_activity = google.insert_activity(
                    form.nl_name.data, form.nl_description.data,
                    form.location.data, form.start_time.data.isoformat(),
                    form.end_time.data.isoformat())

                if google_activity:
                    activity.google_event_id = google_activity['id']

            db.session.add(activity)

            file = request.files.get('picture')

            if file and file.filename:
                picture = file_service.add_file(FileCategory.ACTIVITY_PICTURE,
                                                file, file.filename)

                old_picture_id = activity.picture_file_id
                activity.picture_file_id = picture.id

                if old_picture_id:
                    old_picture = file_service.get_file_by_id(old_picture_id)
                    file_service.delete_file(old_picture)

            db.session.commit()

            return redirect(url_for('activity.get_activity',
                                    activity_id=activity.id))

    return render_template('activity/edit.htm', activity=activity, form=form,
                           title=title)