Exemple #1
0
def edit_campaign(uuid):
    import datetime
    from flask import url_for, redirect
    from campaign_manager.forms.campaign import CampaignForm
    from campaign_manager.models.campaign import Campaign
    """Get campaign details.
    """
    try:
        campaign = Campaign.get(uuid)
        context = campaign.to_dict()

        if request.method == 'GET':
            form = CampaignForm()
            form.name.data = campaign.name
            form.campaign_managers.data = campaign.campaign_managers
            form.remote_projects.data = campaign.remote_projects
            form.types.data = campaign.types
            form.description.data = campaign.description
            form.geometry.data = json.dumps(campaign.geometry)
            form.map_type.data = campaign.map_type
            form.selected_functions.data = json.dumps(
                campaign.selected_functions)
            form.start_date.data = datetime.datetime.strptime(
                campaign.start_date, '%Y-%m-%d')
            if campaign.end_date:
                form.end_date.data = datetime.datetime.strptime(
                    campaign.end_date, '%Y-%m-%d')
        else:
            form = CampaignForm(request.form)
            if form.validate_on_submit():
                data = form.data
                data.pop('types_options')
                data.pop('csrf_token')
                data.pop('submit')
                campaign.update_data(data, form.uploader.data)
                Campaign.compute(campaign.uuid)

                return redirect(
                    url_for('campaign_manager.get_campaign',
                            uuid=campaign.uuid))
    except Campaign.DoesNotExist:
        return Response('Campaign not found')
    context['oauth_consumer_key'] = OAUTH_CONSUMER_KEY
    context['oauth_secret'] = OAUTH_SECRET
    context['map_provider'] = map_provider()
    context['url'] = '/edit/%s' % uuid
    context['action'] = 'edit'
    context['functions'] = get_selected_functions()
    context['title'] = 'Edit Campaign'
    context['maximum_area_size'] = MAX_AREA_SIZE
    context['uuid'] = uuid
    context['types'] = {}
    context['campaign_creator'] = campaign.campaign_creator
    context['link_to_omk'] = campaign.link_to_omk
    try:
        context['types'] = json.dumps(get_types()).replace(
            'True', 'true').replace('False', 'false')
    except ValueError:
        pass
    return render_template('create_campaign.html', form=form, **context)
Exemple #2
0
def create_campaign():
    import uuid
    from flask import url_for, redirect
    from campaign_manager.forms.campaign import CampaignForm
    """Get campaign details.
    """

    form = CampaignForm(request.form)
    if form.validate_on_submit():
        data = form.data
        data.pop('csrf_token')
        data.pop('submit')
        data.pop('types_options')

        data['uuid'] = uuid.uuid4().hex

        Campaign.create(data, form.uploader.data)
        Campaign.compute(data["uuid"])
        campaign = Campaign(data['uuid'])
        campaign.save()
        campaign.save_to_user_campaigns(data['user_id'], data['uuid'],
                                        Permission.ADMIN.name)

        return redirect(
            url_for('campaign_manager.get_campaign', uuid=data['uuid']))

    context = dict(oauth_consumer_key=OAUTH_CONSUMER_KEY,
                   oauth_secret=OAUTH_SECRET,
                   map_provider=map_provider())
    context['url'] = '/create'
    context['action'] = 'create'
    context['functions'] = get_selected_functions()
    context['title'] = 'Create Campaign'
    context['maximum_area_size'] = MAX_AREA_SIZE
    context['uuid'] = uuid.uuid4().hex
    context['types'] = {}
    context['link_to_omk'] = False
    context['feature_templates'] = get_types()
    try:
        context['types'] = json.dumps(get_types()).replace(
            'True', 'true').replace('False', 'false')
    except ValueError:
        pass
    return render_template('create_campaign.html', form=form, **context)