Exemplo n.º 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)
Exemplo n.º 2
0
def get_campaign_data(uuid):
    from campaign_manager.models.campaign import Campaign
    from campaign_manager.aws import S3Data
    """Get campaign details.
    """
    try:
        campaign = Campaign.get(uuid)
    except:
        abort(404)

    context = campaign.to_dict()
    context['s3_campaign_url'] = S3Data().url(uuid)
    campaign_manager_names = []
    for manager in parse_json_string(campaign.campaign_managers):
        campaign_manager_names.append(manager['name'])

    campaign_viewer_names = []
    for viewer in parse_json_string(campaign.campaign_viewers):
        campaign_viewer_names.append(viewer['name'])

    campaign_contributor_names = []
    for contributor in parse_json_string(campaign.campaign_contributors):
        campaign_contributor_names.append(contributor['name'])

    context['oauth_consumer_key'] = OAUTH_CONSUMER_KEY
    context['oauth_secret'] = OAUTH_SECRET
    context['map_provider'] = map_provider()
    context['campaign_manager_names'] = campaign_manager_names
    context['campaign_viewer_names'] = campaign_viewer_names
    context['campaign_contributor_names'] = campaign_contributor_names
    context['participants'] = len(campaign.campaign_managers)

    context['pct_covered_areas'] = campaign.calculate_areas_covered()

    if campaign.map_type != '':
        context['attribution'] = find_attribution(campaign.map_type)

    # Start date
    try:
        start_date = datetime.strptime(campaign.start_date, '%Y-%m-%d')
        context['start_date_date'] = start_date.strftime('%d %b')
        context['start_date_year'] = start_date.strftime('%Y')
    except TypeError:
        context['start_date_date'] = '-'
        context['start_date_year'] = '-'
    context['current_status'] = campaign.get_current_status()
    if context['current_status'] == 'active':
        context['current_status'] = 'running'

    # End date
    try:
        end_date = datetime.strptime(campaign.end_date, '%Y-%m-%d')
        context['end_date_date'] = end_date.strftime('%d %b')
        context['end_date_year'] = end_date.strftime('%Y')
    except TypeError:
        context['end_date_date'] = '-'
        context['end_date_year'] = '-'
    return context
Exemplo n.º 3
0
def styleguide():
    """Styleguide

    This page shows a library of UI components.
    """

    context = dict(oauth_consumer_key=OAUTH_CONSUMER_KEY,
                   oauth_secret=OAUTH_SECRET,
                   map_provider=map_provider())

    return render_template('styleguide.html', **context)
Exemplo n.º 4
0
def learn():
    """MapCampaigner Docs

    Information about to use MapCampaigner.
    """

    context = dict(oauth_consumer_key=OAUTH_CONSUMER_KEY,
                   oauth_secret=OAUTH_SECRET,
                   map_provider=map_provider())

    return render_template('learn.html', **context)
Exemplo n.º 5
0
def home():
    """Landing page

    The first page users land on when they use the app.
    """

    context = dict(oauth_consumer_key=OAUTH_CONSUMER_KEY,
                   oauth_secret=OAUTH_SECRET,
                   map_provider=map_provider())

    return render_template('home.html', **context)
Exemplo n.º 6
0
def home_all():
    """Home page view.

    On this page a summary campaign manager view will shown with all campaigns.
    """

    context = dict(oauth_consumer_key=OAUTH_CONSUMER_KEY,
                   oauth_secret=OAUTH_SECRET,
                   all=True,
                   map_provider=map_provider())

    # noinspection PyUnresolvedReferences
    return render_template('index.html', **context)
Exemplo n.º 7
0
def campaigns_list(osm_id):
    """List the user's campaigns

    A summary campaign manager view with all the users campaigns
    """

    context = dict(oauth_consumer_key=OAUTH_CONSUMER_KEY,
                   oauth_secret=OAUTH_SECRET,
                   all=True,
                   map_provider=map_provider(),
                   bucket_url=S3Data().bucket_url(),
                   osm_id=osm_id)

    return render_template('campaign_index.html', **context)
Exemplo n.º 8
0
def get_campaign(uuid):
    from campaign_manager.models.campaign import Campaign
    from campaign_manager.aws import S3Data
    """Get campaign details.
    """

    try:
        campaign = Campaign.get(uuid)
    except:
        abort(404)

    context = campaign.to_dict()
    context['s3_campaign_url'] = S3Data().url(uuid)

    context['types'] = list(
        map(lambda type: type[1]['type'], context['types'].items()))

    context['oauth_consumer_key'] = OAUTH_CONSUMER_KEY
    context['oauth_secret'] = OAUTH_SECRET
    context['map_provider'] = map_provider()
    context['participants'] = len(campaign.campaign_managers)

    context['pct_covered_areas'] = campaign.calculate_areas_covered()

    if campaign.map_type != '':
        context['attribution'] = find_attribution(campaign.map_type)

    # Start date
    try:
        start_date = datetime.strptime(campaign.start_date, '%Y-%m-%d')
        context['start_date_date'] = start_date.strftime('%d %b')
        context['start_date_year'] = start_date.strftime('%Y')
    except TypeError:
        context['start_date_date'] = '-'
        context['start_date_year'] = '-'
    context['current_status'] = campaign.get_current_status()
    if context['current_status'] == 'active':
        context['current_status'] = 'running'

    # End date
    try:
        end_date = datetime.strptime(campaign.end_date, '%Y-%m-%d')
        context['end_date_date'] = end_date.strftime('%d %b')
        context['end_date_year'] = end_date.strftime('%Y')
    except TypeError:
        context['end_date_date'] = '-'
        context['end_date_year'] = '-'

    return render_template('campaign_detail.html', **context)
Exemplo n.º 9
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)