def crop_and_upload_image_to_popit(self, image_filename, crop_bounds, moderator_why_allowed, make_primary): original = Image.open(image_filename) # Some uploaded images are CYMK, which gives you an error when # you try to write them as PNG, so convert to RGB: original = original.convert('RGB') cropped = original.crop(crop_bounds) ntf = NamedTemporaryFile(delete=False) cropped.save(ntf.name, 'PNG') # Upload the image to PopIt... person_id = self.queued_image.popit_person_id person = PopItPerson.create_from_popit(self.api, person_id) image_upload_url = '{base}persons/{person_id}/image'.format( base=get_base_url(), person_id=person_id) data = { 'md5sum': get_file_md5sum(ntf.name), 'user_why_allowed': self.queued_image.why_allowed, 'user_justification_for_use': self.queued_image.justification_for_use, 'moderator_why_allowed': moderator_why_allowed, 'mime_type': 'image/png', 'notes': _('Approved from photo moderation queue'), 'uploaded_by_user': self.queued_image.user.username, 'created': None, } if make_primary: data['index'] = 'first' with open(ntf.name) as f: requests.post(image_upload_url, data=data, files={'image': f.read()}, headers={'APIKey': self.api.api_key}) person.invalidate_cache_entries() # Remove the cropped temporary image file: os.remove(ntf.name)
def handle(self, **options): for collection in ('organization', 'person'): api_collection = getattr(self.api, collection + 's') message = "{titled} {base_url}{plural}/{id}" for item in popit_unwrap_pagination(api_collection, embed='', per_page=100): print message.format(titled=collection.title(), base_url=get_base_url(), plural=(collection + "s"), id=item['id']) for image in item.get('images', []): print " Image with URL:", image['url'] fix_image(image) # Some images have an empty 'created' field, which # causes an Elasticsearch indexing error, so change it # to null if that's the case: if not image.get('created'): image['created'] = None fix_dates(item) try: api_collection(item['id']).put(item) except HttpClientError as e: print "HttpClientError", e.content sys.exit(1) # If this is a person, make sure that the # corresponding cache entries are invalidated: if collection == 'person': person = PopItPerson.create_from_dict(item) person.invalidate_cache_entries()
def handle(self, **options): message = "WARNING: this will delete all people, posts, " \ "organizations and\nmemberships from the PopIt instance:" + \ "\n\n " + get_base_url() + "\n\nIf you really want to do " + \ "this, type 'YES':" self.stdout.write(message) user_response = raw_input() if user_response != 'YES': self.stdout.write("Aborting, since you didn't type 'YES'.") return for collection in ( 'memberships', 'posts', 'organizations', 'persons', ): self.stdout.write("Deleting from collection: " + collection) api_collection = getattr(self.api, collection) # We have to be careful here - if you try to step to page # 2 after deleting everything on page 1, then lots of # objects will be missed. Instead, just get the first page # until there's nothing left. while True: results = api_collection.get() for o in results['result']: object_id = o['id'] api_collection(object_id).delete() if not results.get('has_more'): break
def handle(self, **options): for collection in ('organization', 'person'): api_collection = getattr(self.api, collection + 's') message = "{titled} {base_url}{plural}/{id}" for item in popit_unwrap_pagination( api_collection, embed='', per_page=100 ): print message.format( titled=collection.title(), base_url=get_base_url(), plural=(collection + "s"), id=item['id'] ) for image in item.get('images', []): print " Image with URL:", image['url'] fix_image(image) # Some images have an empty 'created' field, which # causes an Elasticsearch indexing error, so change it # to null if that's the case: if not image.get('created'): image['created'] = None fix_dates(item) try: api_collection(item['id']).put(item) except HttpClientError as e: print "HttpClientError", e.content sys.exit(1) # If this is a person, make sure that the # corresponding cache entries are invalidated: if collection == 'person': person = PopItPerson.create_from_dict(item) person.invalidate_cache_entries()
def handle(self, **options): for person_data in popit_unwrap_pagination( self.api.persons, embed='', per_page=100 ): msg = "Person {0}persons/{1}" print msg.format(get_base_url(), person_data['id']) strip_bogus_fields( person_data, [ 'founding_date', 'dissolution_date', 'start_date', 'end_date' ] ) for image in person_data.get('images', []): strip_bogus_fields( image, [ 'birth_date', 'death_date', 'founding_date', 'dissolution_date', 'start_date', 'end_date' ] ) person = PopItPerson.create_from_dict(person_data) person.save_to_popit(self.api) person.invalidate_cache_entries()
def handle(self, **options): for person_data in popit_unwrap_pagination( self.api.persons, embed='', per_page=100 ): needs_update = False for version in person_data.get('versions', []): data = version['data'] if data.get('last_party'): needs_update = True msg = "Fixing person {0}persons/{1}" print msg.format(get_base_url(), person_data['id']) del data['last_party'] if not needs_update: continue person = PopItPerson.create_from_dict(person_data) person.save_to_popit(self.api) person.invalidate_cache_entries()
def crop_and_upload_image_to_popit(self, image_filename, crop_bounds, moderator_why_allowed, make_primary): original = Image.open(image_filename) # Some uploaded images are CYMK, which gives you an error when # you try to write them as PNG, so convert to RGBA (this is # RGBA rather than RGB so that any alpha channel (transparency) # is preserved). original = original.convert('RGBA') cropped = original.crop(crop_bounds) ntf = NamedTemporaryFile(delete=False) cropped.save(ntf.name, 'PNG') # Upload the image to PopIt... person_id = self.queued_image.popit_person_id person = PopItPerson.create_from_popit(self.api, person_id) image_upload_url = '{base}persons/{person_id}/image'.format( base=get_base_url(), person_id=person_id ) data = { 'md5sum': get_file_md5sum(ntf.name), 'user_why_allowed': self.queued_image.why_allowed, 'user_justification_for_use': self.queued_image.justification_for_use, 'moderator_why_allowed': moderator_why_allowed, 'mime_type': 'image/png', 'notes': _('Approved from photo moderation queue'), 'uploaded_by_user': self.queued_image.user.username, 'created': None, } if make_primary: data['index'] = 'first' with open(ntf.name) as f: requests.post( image_upload_url, data=data, files={'image': f.read()}, headers={'APIKey': self.api.api_key} ) person.invalidate_cache_entries() # Remove the cropped temporary image file: os.remove(ntf.name)