def parse_json_file(self): """ Parse json file for this campaign. If file is corrupted, it will raise Campaign.CorruptedFile exception. """ # campaign data if self.json_path: try: content = S3Data().fetch(self.json_path) content_json = parse_json_string(content) Campaign.validate(content_json, self.uuid) self._content_json = content_json attributes = self.get_attributes() for key, value in content_json.items(): if key in attributes: setattr(self, key, value) except json.decoder.JSONDecodeError: raise JsonModel.CorruptedFile self.types = Campaign.parse_types_string(json.dumps(self.types)) # geometry data if self.geojson_path: try: content = S3Data().fetch(self.geojson_path) geometry = parse_json_string(content) self.geometry = geometry self._content_json['geometry'] = geometry except json.decoder.JSONDecodeError: raise JsonModel.CorruptedFile
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
def update_data(self, data, uploader): """ Update data with new dict. :param data: data that will be inserted :type data: dict :param uploader: uploader who created :type uploader: str """ for key, value in data.items(): setattr(self, key, value) self.geometry = parse_json_string(self.geometry) self.types = Campaign.parse_types_string(self.types.replace('\'', '"')) self.selected_functions = parse_json_string(self.selected_functions) self.save(uploader)
def parse_types_string(types_string): types = parse_json_string(types_string) for type, value in types.items(): json_tags = {} tags = value['tags'] if (isinstance(tags, list)): for tag in value['tags']: tag = tag.replace(']', '') tag_splitted = tag.split('[') tag_key = tag_splitted[0].strip() if len(tag_splitted) == 2: json_tags[tag_key] = tag_splitted[1].split(',') else: json_tags[tag_key] = [] value['tags'] = json_tags return types
def parse_campaign_data(data, uploader): """Validate found dict based on campaign class. uuid should be same as uuid file. :param data: data that will be inserted :type data: dict :param uploader: uploader who created :type uploader: str """ data['version'] = 1 if 'version' in data: data['version'] = data['version'] + 1 data['edited_by'] = uploader data['campaign_creator'] = uploader uuid = data['uuid'] data['types'] = Campaign.parse_types_string(data['types']) data['selected_functions'] = parse_json_string( data['selected_functions']) Campaign.validate(data, uuid) return data
def create(data, uploader): """Validate found dict based on campaign class. uuid should be same as uuid file. :param data: data that will be inserted :type data: dict :param uploader: uploader who created :type uploader: str """ campaign_data = Campaign.parse_campaign_data(data, uploader) geometry = data['geometry'] del data['geometry'] json_str = Campaign.serialize( Campaign.parse_campaign_data(data, uploader)) campaign_key = Campaign.get_json_file(campaign_data['uuid']) campaign_body = json_str S3Data().create(campaign_key, campaign_body) geocampaign_key = Campaign.get_geojson_file(campaign_data['uuid']) geocampaign_body = json.dumps(parse_json_string(geometry)) S3Data().create(geocampaign_key, geocampaign_body)
def edit_campaign(uuid): import datetime from flask import url_for, redirect from campaign_manager.forms.campaign import CampaignForm """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 = parse_json_string( campaign.campaign_managers) form.campaign_viewers.data = parse_json_string( campaign.campaign_viewers) form.campaign_contributors.data = parse_json_string( campaign.campaign_contributors) 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 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)