Exemplo n.º 1
0
def generate_kml():
    """Generate KML file from geojson."""
    uuid = request.values.get('uuid', None)
    campaign_name = request.values.get('campaign_name', None)
    campaign = Campaign(uuid)

    # Get json for each type.
    types = campaign.get_s3_types()
    if types is None:
        return Response(json.dumps({'error': 'types not found'}), 400)

    data = []
    for t in types:
        data.append(campaign.get_type_geojsons(t))

    if len(data) == 0:
        return Response(json.dumps({'error': 'Data not found'}), 400)

    features = [i['features'] for sublist in data for i in sublist]

    # for each type, we need to get geojson.
    kml = Kml(name=campaign_name)

    file_name = hashlib.md5(
        uuid.encode('utf-8') + '{:%m-%d-%Y}'.format(datetime.today()).encode(
            'utf-8')).hexdigest() + '.kml'

    file_path = os.path.join(config.CACHE_DIR, file_name)

    # For now, let's work only with points.
    # TODO: include polygons in the kml file.
    features = [[f for f in sublist if f['geometry']['type'] == 'Point']
                for sublist in features]
    features = [item for sublist in features for item in sublist]

    for feature in features:
        tags = feature['properties']['tags']
        extended_data = ExtendedData()
        kml_name = ''

        if 'name' in tags.keys():
            kml_name = tags['name']
        elif 'amenity' in tags.keys():
            kml_name = tags['amenity']

        [
            extended_data.newdata(k, escape(v)) for k, v in tags.items()
            if k != 'name'
        ]
        kml.newpoint(name=kml_name,
                     extendeddata=extended_data,
                     coords=[(feature['geometry']['coordinates'][0],
                              feature['geometry']['coordinates'][1])])
    kml.save(path=file_path)
    if kml:
        # Save file into client storage device.
        return Response(json.dumps({'file_name': file_name}))
Exemplo n.º 2
0
def mock_get_campaign():
    campaign = Campaign()
    campaign.uuid = '111'
    campaign.name = 'test'
    campaign.total_participants_count = 3
    campaign.campaign_creator = 'test_creator'
    campaign.campaign_managers = [
        'test_manager1', 'test_manager2', 'test_creator'
    ]
    campaign._content_json = {'name': campaign.name, 'uuid': campaign.uuid}
    return [campaign]
Exemplo n.º 3
0
def mock_get_campaign():
    campaign = Campaign()
    campaign.uuid = '111'
    campaign.name = 'test'
    campaign.total_participants_count = 3
    campaign.campaign_creator = 'test_creator'
    campaign.campaign_managers = [{
        'name': 'test_manager1',
        'osm_id': '12345'
    }, {
        'name': 'test_manager2',
        'osm_id': '54321'
    }, {
        'name': 'test_creator',
        'osm_id': '98765'
    }]
    campaign._content_json = {'name': campaign.name, 'uuid': campaign.uuid}
    return [campaign]
Exemplo n.º 4
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)
Exemplo n.º 5
0
 def setUp(self):
     self.uuid = 'ff6ff8fcfdd847c48dd1bc3c9107b397'
     self.campaign = Campaign(self.uuid)
Exemplo n.º 6
0
def mock_get_campaigns_with_tag(tag):
    campaign = Campaign()
    campaign.uuid = '111'
    campaign.name = 'test'
    campaign._content_json = {'name': campaign.name, 'uuid': campaign.uuid}
    return [campaign]
Exemplo n.º 7
0
def mock_get_nearest_campaign(coordinate):
    campaign = Campaign()
    campaign.uuid = '111'
    campaign.name = 'test'
    campaign._content_json = {'name': campaign.name, 'uuid': campaign.uuid}
    return [campaign]
Exemplo n.º 8
0
 def get_campaign(self, uuid):
     """Return campaign."""
     return Campaign(uuid=uuid)
Exemplo n.º 9
0
    def runTest(self):
        campaign = Campaign('ff6ff8fcfdd847c48dd1bc3c9107b397')
        url = campaign.generate_static_map_url(simplify=False)

        request = requests.get(url, stream=True)
        self.assertEqual(request.status_code, 200)