def create_doc_url(self, listing: models.Listing, doc_url: DocUrlData) -> models.DocUrl: instance = models.DocUrl(name=doc_url.name, url=doc_url.url, listing=listing) instance.save() return instance
def migrate_doc_url(listing_mapper): logging.debug('migrating doc_url...') columns = get_columns('doc_url') # ['id', 'version', 'listing_id', 'name', 'url'] assert columns[0] == 'id' assert columns[1] == 'version' assert columns[2] == 'listing_id' assert columns[3] == 'name' assert columns[4] == 'url' values = get_values('doc_url', len(columns)) # logging.debug('category columns: %s' % columns) logging.info('Doc Urls to migrate: {0!s}'.format(len(values))) logging.info('==========================') for i in values: try: old_id = i[0] listing_id = i[2] name = i[3] url = i[4] listing = models.Listing.objects.get(id=listing_mapper[listing_id]) logging.info('Adding doc_url for listing {0!s}, name {1!s}'.format( listing.title, name)) doc_url = models.DocUrl(name=name, url=url, listing=listing) doc_url.save() except Exception as e: logging.error( 'Error adding doc_url entry: {0!s}, values: {1!s}'.format( str(e), i))
def create_listing(listing_builder_dict, object_cache): """ Create Listing Helper Function 10-01-2017 - Total Database Calls: 11842 10-02-2017 - Total Database Calls: 7737 """ listing_data = listing_builder_dict['listing'] listing = models.Listing( title=listing_data['title'], agency=object_cache['Agency.{}'.format(listing_data['agency'])], listing_type=object_cache['ListingType.{}'.format( listing_data['listing_type'])], description=listing_data['description'], launch_url=listing_data['launch_url'].format_map( {'DEMO_APP_ROOT': DEMO_APP_ROOT}), version_name=listing_data['version_name'], unique_name=listing_data['unique_name'], small_icon=object_cache['Listing[{}].small_icon'.format( listing_data['title'])], large_icon=object_cache['Listing[{}].large_icon'.format( listing_data['title'])], banner_icon=object_cache['Listing[{}].banner_icon'.format( listing_data['title'])], large_banner_icon=object_cache['Listing[{}].large_banner_icon'.format( listing_data['title'])], what_is_new=listing_data['what_is_new'], description_short=listing_data['description_short'], usage_requirements=listing_data['usage_requirements'], system_requirements=listing_data['system_requirements'], is_enabled=listing_data['is_enabled'], is_private=listing_data['is_private'], is_featured=listing_data['is_featured'], iframe_compatible=listing_data['iframe_compatible'], security_marking=listing_data['security_marking']) listing.save() # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Contacts # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - for current_contact in listing_data['contacts']: listing.contacts.add( object_cache['Contact.{}'.format(current_contact)]) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Owners # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - for current_owner in listing_data['owners']: listing.owners.add(object_cache['Profile.{}'.format(current_owner)]) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Categories # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - for current_category in listing_data['categories']: listing.categories.add( models.Category.objects.get(title=current_category)) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Tags # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - for current_tag in listing_data['tags']: if object_cache.get('Tag.{}'.format(current_tag)): current_tag_obj = object_cache['Tag.{}'.format(current_tag)] else: current_tag_obj, created = models.Tag.objects.get_or_create( name=current_tag) listing.tags.add(current_tag_obj) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Screenshots # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - for current_screenshot_entry in listing_data['screenshots']: small_image = models.Image.create_image( Image.open(TEST_IMG_PATH + current_screenshot_entry['small_image']['filename']), file_extension=current_screenshot_entry['small_image'] ['filename'].split('.')[-1], security_marking=current_screenshot_entry['small_image'] ['security_marking'], image_type=object_cache['ImageType.small_screenshot'].name) large_image = models.Image.create_image( Image.open(TEST_IMG_PATH + current_screenshot_entry['large_image']['filename']), file_extension=current_screenshot_entry['large_image'] ['filename'].split('.')[-1], security_marking=current_screenshot_entry['large_image'] ['security_marking'], image_type=object_cache['ImageType.large_screenshot'].name) screenshot = models.Screenshot( small_image=small_image, large_image=large_image, listing=listing, description=current_screenshot_entry['description'], order=current_screenshot_entry['order']) screenshot.save() # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Document URLs # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - for current_doc_url_entry in listing_data['doc_urls']: current_doc_url_obj = models.DocUrl(name=current_doc_url_entry['name'], url=current_doc_url_entry['url'], listing=listing) current_doc_url_obj.save() # listing_activity for listing_activity_entry in listing_builder_dict['listing_activity']: listing_activity_action = listing_activity_entry['action'] listing_activity_author = object_cache['Profile.{}'.format( listing_activity_entry['author'])] if listing_activity_action == 'CREATED': listing_model_access.create_listing(listing_activity_author, listing) elif listing_activity_action == 'SUBMITTED': listing_model_access.submit_listing(listing_activity_author, listing) elif listing_activity_action == 'APPROVED_ORG': listing_model_access.approve_listing_by_org_steward( listing_activity_author, listing) elif listing_activity_action == 'APPROVED': listing_model_access.approve_listing(listing_activity_author, listing) return listing
def create_listing(listing_builder_dict): """ Create Listing Helper Function """ listing_data = listing_builder_dict['listing'] # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Icons # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - small_icon = models.Image.create_image( Image.open(TEST_IMG_PATH + listing_data['small_icon']['filename']), file_extension=listing_data['small_icon']['filename'].split('.')[-1], security_marking=listing_data['small_icon']['security_marking'], image_type=models.ImageType.objects.get(name='small_icon').name) large_icon = models.Image.create_image( Image.open(TEST_IMG_PATH + listing_data['large_icon']['filename']), file_extension=listing_data['large_icon']['filename'].split('.')[-1], security_marking=listing_data['large_icon']['security_marking'], image_type=models.ImageType.objects.get(name='large_icon').name) banner_icon = models.Image.create_image( Image.open(TEST_IMG_PATH + listing_data['banner_icon']['filename']), file_extension=listing_data['banner_icon']['filename'].split('.')[-1], security_marking=listing_data['banner_icon']['security_marking'], image_type=models.ImageType.objects.get(name='banner_icon').name) large_banner_icon = models.Image.create_image( Image.open(TEST_IMG_PATH + listing_data['large_banner_icon']['filename']), file_extension=listing_data['large_banner_icon']['filename'].split( '.')[-1], security_marking=listing_data['large_banner_icon']['security_marking'], image_type=models.ImageType.objects.get(name='large_banner_icon').name) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Listing # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - listing = models.Listing( title=listing_data['title'], agency=models.Agency.objects.get(short_name=listing_data['agency']), listing_type=models.ListingType.objects.get( title=listing_data['listing_type']), description=listing_data['description'], launch_url=listing_data['launch_url'].format_map( {'DEMO_APP_ROOT': DEMO_APP_ROOT}), version_name=listing_data['version_name'], unique_name=listing_data['unique_name'], small_icon=small_icon, large_icon=large_icon, banner_icon=banner_icon, large_banner_icon=large_banner_icon, what_is_new=listing_data['what_is_new'], description_short=listing_data['description_short'], requirements=listing_data['requirements'], is_enabled=listing_data['is_enabled'], is_private=listing_data['is_private'], is_featured=listing_data['is_featured'], iframe_compatible=listing_data['iframe_compatible'], security_marking=listing_data['security_marking']) listing.save() # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Contacts # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - for current_contact in listing_data['contacts']: listing.contacts.add(models.Contact.objects.get(email=current_contact)) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Owners # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - for current_owner in listing_data['owners']: listing.owners.add( models.Profile.objects.get(user__username=current_owner)) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Categories # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - for current_category in listing_data['categories']: listing.categories.add( models.Category.objects.get(title=current_category)) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Tags # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - for current_tag in listing_data['tags']: current_tag_obj, created = models.Tag.objects.get_or_create( name=current_tag) listing.tags.add(current_tag_obj) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Screenshots # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - for current_screenshot_entry in listing_data['screenshots']: small_image = models.Image.create_image( Image.open(TEST_IMG_PATH + current_screenshot_entry['small_image']['filename']), file_extension=current_screenshot_entry['small_image'] ['filename'].split('.')[-1], security_marking=current_screenshot_entry['small_image'] ['security_marking'], image_type=models.ImageType.objects.get( name='small_screenshot').name) large_image = models.Image.create_image( Image.open(TEST_IMG_PATH + current_screenshot_entry['large_image']['filename']), file_extension=current_screenshot_entry['large_image'] ['filename'].split('.')[-1], security_marking=current_screenshot_entry['large_image'] ['security_marking'], image_type=models.ImageType.objects.get( name='large_screenshot').name) screenshot = models.Screenshot( small_image=small_image, large_image=large_image, listing=listing, description=current_screenshot_entry['description'], order=current_screenshot_entry['order']) screenshot.save() # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Document URLs # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - for current_doc_url_entry in listing_data['doc_urls']: current_doc_url_obj = models.DocUrl(name=current_doc_url_entry['name'], url=current_doc_url_entry['url'], listing=listing) current_doc_url_obj.save() # listing_activity for listing_activity_entry in listing_builder_dict['listing_activity']: listing_activity_action = listing_activity_entry['action'] listing_activity_author = models.Profile.objects.get( user__username=listing_activity_entry['author']) if listing_activity_action == 'CREATED': listing_model_access.create_listing(listing_activity_author, listing) elif listing_activity_action == 'SUBMITTED': listing_model_access.submit_listing(listing_activity_author, listing) elif listing_activity_action == 'APPROVED_ORG': listing_model_access.approve_listing_by_org_steward( listing_activity_author, listing) elif listing_activity_action == 'APPROVED': listing_model_access.approve_listing(listing_activity_author, listing) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Reviews # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # listing_review_batch create_listing_review_batch(listing, listing_builder_dict['listing_review_batch']) return listing
def create(self, validated_data): # logger.debug('inside ListingSerializer.create', extra={'request':self.context.get('request')}) title = validated_data['title'] user = generic_model_access.get_profile( self.context['request'].user.username) logger.info('creating listing {0!s} for user {1!s}'.format( title, user.user.username), extra={'request': self.context.get('request')}) # TODO required_listings listing = models.Listing( title=title, agency=validated_data['agency'], description=validated_data['description'], launch_url=validated_data['launch_url'], version_name=validated_data['version_name'], unique_name=validated_data['unique_name'], what_is_new=validated_data['what_is_new'], description_short=validated_data['description_short'], requirements=validated_data['requirements'], security_marking=validated_data['security_marking'], listing_type=validated_data['listing_type'], is_private=validated_data['is_private']) image_keys = [ 'small_icon', 'large_icon', 'banner_icon', 'large_banner_icon' ] for image_key in image_keys: if validated_data[image_key]: new_value_image = image_model_access.get_image_by_id( validated_data[image_key].get('id')) if new_value_image is None: raise errors.InvalidInput( 'Error while saving, can not find image by id') if image_key == 'small_icon': listing.small_icon = new_value_image elif image_key == 'large_icon': listing.large_icon = new_value_image elif image_key == 'banner_icon': listing.banner_icon = new_value_image elif image_key == 'large_banner_icon': listing.large_banner_icon = new_value_image listing.save() if validated_data.get('contacts') is not None: for contact in validated_data['contacts']: contact_type_instance = contact_type_model_access.get_contact_type_by_name( contact['contact_type']['name']) new_contact, created = models.Contact.objects.get_or_create( name=contact['name'], email=contact['email'], secure_phone=contact['secure_phone'], unsecure_phone=contact['unsecure_phone'], organization=contact.get('organization', None), contact_type=contact_type_instance) new_contact.save() listing.contacts.add(new_contact) if validated_data.get('owners') is not None: if validated_data['owners']: for owner in validated_data['owners']: listing.owners.add(owner) else: # if no owners are specified, just add the current user listing.owners.add(user) if validated_data.get('categories') is not None: for category in validated_data['categories']: listing.categories.add(category) # tags will be automatically created if necessary if validated_data.get('tags') is not None: for tag in validated_data['tags']: obj, created = models.Tag.objects.get_or_create( name=tag['name']) listing.tags.add(obj) if validated_data.get('intents') is not None: for intent in validated_data['intents']: listing.intents.add(intent) # doc_urls will be automatically created if validated_data.get('doc_urls') is not None: for d in validated_data['doc_urls']: doc_url = models.DocUrl(name=d['name'], url=d['url'], listing=listing) doc_url.save() # screenshots will be automatically created if validated_data.get('screenshots') is not None: for screenshot_dict in validated_data['screenshots']: screenshot = models.Screenshot( small_image=image_model_access.get_image_by_id( screenshot_dict['small_image']['id']), large_image=image_model_access.get_image_by_id( screenshot_dict['large_image']['id']), listing=listing) screenshot.save() # create a new activity model_access.create_listing(user, listing) return listing