def import_row(self, row): # generate slugs row['name'] = row['name'].strip() row['email'] = row['email'].lower() if row['url'] and not row['url'].startswith(('http://', 'https://')): row['url'] = 'http://' + row['url'] row['slug'] = slugify(row['name']) row['classification'] = self.get_classification( row.pop('classification__slug', None)) categories = parse_tags(row.pop('categories', '')) categories = list(self.get_categories(categories)) tags = parse_tags(row.pop('tags', '')) # Backwards compatible handling of topic__slug topic_slug = row.pop('topic__slug', None) if topic_slug: tags.append(self.get_topic(topic_slug)) # resolve foreign keys row['jurisdiction'] = self.get_jurisdiction( row.pop('jurisdiction__slug')) parent = row.pop('parent__name', None) if parent: row['parent'] = PublicBody.objects.get(slug=slugify(parent)) # get optional values for n in ('description', 'other_names', 'request_note', 'website_dump'): row[n] = row.get(n, '') try: if 'id' in row and row['id']: pb = PublicBody.objects.get(id=row['id']) else: pb = PublicBody.objects.get(slug=row['slug']) # If it exists, update it row.pop('id', None) # Do not update id though row['_updated_by'] = self.user PublicBody.objects.filter(id=pb.id).update(**row) pb.laws.clear() pb.laws.add(*row['jurisdiction'].laws) pb.tags.set(*tags) pb.categories.set(*categories) return pb except PublicBody.DoesNotExist: pass row.pop('id', None) # Remove id if present public_body = PublicBody(**row) public_body._created_by = self.user public_body._updated_by = self.user public_body.confirmed = True public_body.site = self.site public_body.save() public_body.laws.add(*row['jurisdiction'].laws) public_body.tags.set(*list(tags)) return public_body
def import_row(self, row): # generate slugs row['name'] = row['name'].strip() row['email'] = row['email'].lower() if row['url'] and not row['url'].startswith(('http://', 'https://')): row['url'] = 'http://' + row['url'] row['slug'] = slugify(row['name']) row['classification_slug'] = slugify(row['classification']) def comma_splitter(tag_string): return [t.strip() for t in tag_string.split(',') if t.strip()] tags = comma_splitter(row.pop('tags', '')) # Backwards compatible handling of topic__slug topic_slug = row.pop('topic__slug', None) if topic_slug: tags.append(self.get_topic(topic_slug)) # resolve foreign keys row['jurisdiction'] = self.get_jurisdiction(row.pop('jurisdiction__slug')) parent = row.pop('parent__name', None) if parent: row['parent'] = PublicBody.objects.get(slug=slugify(parent)) # get optional values for n in ('description', 'other_names', 'request_note', 'website_dump'): row[n] = row.get(n, '') try: if 'id' in row and row['id']: pb = PublicBody.objects.get(id=row['id']) else: pb = PublicBody.objects.get(slug=row['slug']) # If it exists, update it row.pop('id', None) # Do not update id though row['_updated_by'] = self.user PublicBody.objects.filter(id=pb.id).update(**row) pb.laws.clear() pb.laws.add(*row['jurisdiction'].laws) pb.tags.set(*tags) return except PublicBody.DoesNotExist: pass row.pop('id', None) # Remove id if present public_body = PublicBody(**row) public_body._created_by = self.user public_body._updated_by = self.user public_body.confirmed = True public_body.site = self.site public_body.save() public_body.laws.add(*row['jurisdiction'].laws) public_body.tags.set(*list(tags))
def import_row(self, row): # generate slugs row["name"] = row["name"].strip() row["email"] = row["email"].lower() if row["url"] and not row["url"].startswith(("http://", "https://")): row["url"] = "http://" + row["url"] row["slug"] = slugify(row["name"]) row["classification_slug"] = slugify(row["classification"]) tags = parse_tags(row.pop("tags", "")) # Backwards compatible handling of topic__slug topic_slug = row.pop("topic__slug", None) if topic_slug: tags.append(self.get_topic(topic_slug)) # resolve foreign keys row["jurisdiction"] = self.get_jurisdiction(row.pop("jurisdiction__slug")) parent = row.pop("parent__name", None) if parent: row["parent"] = PublicBody.objects.get(slug=slugify(parent)) # get optional values for n in ("description", "other_names", "request_note", "website_dump"): row[n] = row.get(n, "") try: if "id" in row and row["id"]: pb = PublicBody.objects.get(id=row["id"]) else: pb = PublicBody.objects.get(slug=row["slug"]) # If it exists, update it row.pop("id", None) # Do not update id though row["_updated_by"] = self.user PublicBody.objects.filter(id=pb.id).update(**row) pb.laws.clear() pb.laws.add(*row["jurisdiction"].laws) pb.tags.set(*tags) return except PublicBody.DoesNotExist: pass row.pop("id", None) # Remove id if present public_body = PublicBody(**row) public_body._created_by = self.user public_body._updated_by = self.user public_body.confirmed = True public_body.site = self.site public_body.save() public_body.laws.add(*row["jurisdiction"].laws) public_body.tags.set(*list(tags))
def import_row(self, row): # generate slugs row['name'] = row['name'].strip() row['email'] = row['email'].lower() row['slug'] = slugify(row['name']) row['classification_slug'] = slugify(row['classification']) # resolve foreign keys row['topic'] = self.get_topic(row.pop('topic__slug')) row['jurisdiction'] = self.get_jurisdiction( row.pop('jurisdiction__slug')) parent = row.pop('parent__name', None) if parent: row['parent'] = PublicBody.objects.get(slug=slugify(parent)) # get optional values for n in ('description', 'other_names', 'request_note', 'website_dump'): row[n] = row.get(n, '') try: if 'id' in row and row['id']: pb = PublicBody.objects.get(id=row['id']) else: pb = PublicBody.objects.get(slug=row['slug']) # If it exists, update it row.pop('id', None) # Do not update id though row['_updated_by'] = self.user PublicBody.objects.filter(id=pb.id).update(**row) pb.laws.clear() pb.laws.add(*row['jurisdiction'].laws) return except PublicBody.DoesNotExist: pass row.pop('id', None) # Remove id if present public_body = PublicBody(**row) public_body._created_by = self.user public_body._updated_by = self.user public_body.confirmed = True public_body.site = self.site public_body.save() public_body.laws.add(*row['jurisdiction'].laws)
def import_row(self, row): # generate slugs row['name'] = row['name'].strip() row['email'] = row['email'].lower() row['slug'] = slugify(row['name']) row['classification_slug'] = slugify(row['classification']) # resolve foreign keys row['topic'] = self.get_topic(row.pop('topic__slug')) row['jurisdiction'] = self.get_jurisdiction(row.pop('jurisdiction__slug')) parent = row.pop('parent__name', None) if parent: row['parent'] = PublicBody.objects.get(slug=slugify(parent)) # get optional values for n in ('description', 'other_names', 'request_note', 'website_dump'): row[n] = row.get(n, '') try: if 'id' in row and row['id']: pb = PublicBody.objects.get(id=row['id']) else: pb = PublicBody.objects.get(slug=row['slug']) # If it exists, update it row.pop('id', None) # Do not update id though row['_updated_by'] = self.user PublicBody.objects.filter(id=pb.id).update(**row) pb.laws.clear() pb.laws.add(*row['jurisdiction'].laws) return except PublicBody.DoesNotExist: pass row.pop('id', None) # Remove id if present public_body = PublicBody(**row) public_body._created_by = self.user public_body._updated_by = self.user public_body.confirmed = True public_body.site = self.site public_body.save() public_body.laws.add(*row['jurisdiction'].laws)
def import_row(self, row): # generate slugs if 'name' in row: row['name'] = row['name'].strip() if 'email' in row: row['email'] = row['email'].lower() if 'url' in row: if row['url'] and not row['url'].startswith( ('http://', 'https://')): row['url'] = 'http://' + row['url'] if 'slug' not in row and 'name' in row: row['slug'] = slugify(row['name']) if 'classification' in row: row['classification'] = self.get_classification( row.pop('classification', None)) categories = row.pop('categories', '').split(',') categories = list(self.get_categories(categories)) # resolve foreign keys if 'jurisdiction__slug' in row: row['jurisdiction'] = self.get_jurisdiction( row.pop('jurisdiction__slug')) regions = None if 'georegion_id' in row: regions = [self.get_georegion(id=row.pop('georegion_id'))] elif 'georegion_identifier' in row: regions = [ self.get_georegion(identifier=row.pop('georegion_identifier')) ] elif 'regions' in row: regions = row.pop('regions') if regions: regions = [ self.get_georegion(id=r) for r in regions.split(',') ] parent = row.pop('parent__name', None) if parent: row['parent'] = PublicBody._default_manager.get( slug=slugify(parent)) parent = row.pop('parent__id', None) if parent: row['parent'] = PublicBody._default_manager.get(pk=parent) # get optional values for n in ('description', 'other_names', 'request_note', 'website_dump', 'wikidata_item'): if n in row: row[n] = row.get(n, '').strip() if 'lat' in row and 'lng' in row: lat = row.pop('lat') lng = row.pop('lng') if lat and lng: row['geo'] = Point(float(lng), float(lat)) try: if 'id' in row and row['id']: pb = PublicBody._default_manager.get(id=row['id']) elif row.get('source_reference'): pb = PublicBody._default_manager.get( source_reference=row['source_reference']) else: pb = PublicBody._default_manager.get(slug=row['slug']) # If it exists, update it row.pop('id', None) # Do not update id though row.pop('slug', None) # Do not update slug either row['_updated_by'] = self.user row['updated_at'] = timezone.now() PublicBody._default_manager.filter(id=pb.id).update(**row) pb.laws.clear() pb.laws.add(*row['jurisdiction'].laws) if regions: pb.regions.set(regions) pb.categories.set(*categories) return pb except PublicBody.DoesNotExist: pass row.pop('id', None) # Remove id if present pb = PublicBody(**row) pb._created_by = self.user pb._updated_by = self.user pb.created_at = timezone.now() pb.updated_at = timezone.now() pb.confirmed = True pb.site = self.site pb.save() pb.laws.add(*row['jurisdiction'].laws) if regions: pb.regions.set(regions) pb.categories.set(*categories) return pb
def import_row(self, row): # generate slugs if 'name' in row: row['name'] = row['name'].strip() if 'email' in row: row['email'] = row['email'].lower() if 'url' in row: if row['url'] and not row['url'].startswith( ('http://', 'https://')): row['url'] = 'http://' + row['url'] if 'slug' not in row and 'name' in row: row['slug'] = slugify(row['name']) if 'classification' in row: row['classification'] = self.get_classification( row.pop('classification', None)) categories = parse_tags(row.pop('categories', '')) categories = list(self.get_categories(categories)) tags = parse_tags(row.pop('tags', '')) # Backwards compatible handling of topic__slug topic_slug = row.pop('topic__slug', None) if topic_slug: tags.append(self.get_topic(topic_slug)) # resolve foreign keys if 'jurisdiction__slug' in row: row['jurisdiction'] = self.get_jurisdiction( row.pop('jurisdiction__slug')) regions = None if 'georegion_id' in row: regions = [self.get_georegion(id=row.pop('georegion_id'))] elif 'georegion_identifier' in row: regions = [ self.get_georegion(identifier=row.pop('georegion_identifier')) ] elif 'regions' in row: regions = row.pop('regions') if regions: regions = [ self.get_georegion(id=r) for r in regions.split(',') ] parent = row.pop('parent__name', None) if parent: row['parent'] = PublicBody.objects.get(slug=slugify(parent)) parent = row.pop('parent__id', None) if parent: row['parent'] = PublicBody.objects.get(pk=parent) # get optional values for n in ('description', 'other_names', 'request_note', 'website_dump'): if n in row: row[n] = row.get(n, '').strip() try: if 'id' in row and row['id']: pb = PublicBody.objects.get(id=row['id']) else: pb = PublicBody.objects.get(slug=row['slug']) # If it exists, update it row.pop('id', None) # Do not update id though row.pop('slug', None) # Do not update slug either row['_updated_by'] = self.user row['updated_at'] = timezone.now() PublicBody.objects.filter(id=pb.id).update(**row) pb.laws.clear() pb.laws.add(*row['jurisdiction'].laws) pb.tags.set(*tags) if regions: pb.regions.set(*regions) pb.categories.set(*categories) return pb except PublicBody.DoesNotExist: pass row.pop('id', None) # Remove id if present pb = PublicBody(**row) pb._created_by = self.user pb._updated_by = self.user pb.created_at = timezone.now() pb.updated_at = timezone.now() pb.confirmed = True pb.site = self.site pb.save() pb.laws.add(*row['jurisdiction'].laws) pb.tags.set(*list(tags)) if regions: pb.regions.set(*regions) pb.categories.set(*categories) return pb