def retrieve(self, request, pk=None): """ Return an image, enforcing access control """ queryset = self.get_queryset() image = get_object_or_404(queryset, pk=pk) image_path = str(image.id) + '_' + image.image_type.name + '.' + image.file_extension # enforce access control profile = generic_model_access.get_profile(self.request.user.username) if not system_has_access_control(profile.user.username, image.security_marking): raise errors.PermissionDenied() content_type = 'image/' + image.file_extension try: with media_storage.open(image_path) as f: return HttpResponse(f.read(), content_type=content_type) except IOError: logger.error('No image found for pk {}'.format(pk)) return Response(status=status.HTTP_404_NOT_FOUND)
def retrieve(self, request, pk=None): """ Return an image, enforcing access control """ queryset = self.get_queryset() image = get_object_or_404(queryset, pk=pk) # enforce access control if not system_has_access_control(self.request.user.username, image.security_marking): raise errors.PermissionDenied('Security marking too high for current user') image_path = str(image.id) + '_' + image.image_type.name + '.' + image.file_extension content_type = 'image/' + image.file_extension try: with media_storage.open(image_path) as f: return HttpResponse(f.read(), content_type=content_type) except IOError: logger.error('No image found for pk {}'.format(pk)) return Response(status=status.HTTP_404_NOT_FOUND)
def extract_listings_database(save_images=None): """ Function used to extract listings from database into python objects """ output_list = [] for current_listing in models.Listing.objects.order_by( 'title').iterator(): listing = {} listing['agency'] = current_listing.agency.short_name listing['title'] = current_listing.title listing['listing_type'] = current_listing.listing_type.title listing['description'] = current_listing.description if current_listing.launch_url: listing['launch_url'] = current_listing.launch_url.replace( DEMO_APP_ROOT, '{DEMO_APP_ROOT}') else: listing['launch_url'] = '{DEMO_APP_ROOT}/default/index.html' listing['version_name'] = current_listing.version_name if current_listing.unique_name: listing['unique_name'] = current_listing.unique_name else: listing['unique_name'] = current_listing.title.lower().replace( ' ', '_') listing['what_is_new'] = current_listing.what_is_new listing['description_short'] = current_listing.description_short listing['usage_requirements'] = current_listing.usage_requirements listing[ 'system_requirements'] = current_listing.system_requirements listing['is_enabled'] = current_listing.is_enabled listing['is_featured'] = current_listing.is_featured listing['is_deleted'] = current_listing.is_deleted listing['iframe_compatible'] = current_listing.iframe_compatible listing['security_marking'] = current_listing.security_marking listing['is_private'] = current_listing.is_private # "intents": [], listing['doc_urls'] = sorted([{ 'name': doc_url.name, 'url': doc_url.url } for doc_url in models.DocUrl.objects.filter( listing=current_listing).all()], key=lambda doc: doc['name']) listing['owners'] = sorted([ current_owner.user.username for current_owner in current_listing.owners.iterator() ]) # Convert list of tags names into set, then back into list for unique tags name listing['tags'] = sorted( list( set([ current_tag.name for current_tag in current_listing.tags.iterator() ]))) listing['categories'] = sorted([ current_category.title for current_category in current_listing.categories.iterator() ]) listing['contacts'] = sorted([ current_contact.email for current_contact in current_listing.contacts.iterator() ]) screenshot_entry_counter = 0 screenshot_entry_list = [] for screenshot_entry in current_listing.screenshots.iterator(): screenshot_image_types = ['small_image', 'large_image'] screenshot_entry_dict = {} screenshot_entry_dict['order'] = screenshot_entry.order screenshot_entry_dict[ 'description'] = screenshot_entry.description for current_image_type in screenshot_image_types: current_image = getattr(screenshot_entry, current_image_type) current_image_path = str( current_image.id ) + '_' + current_image.image_type.name + '.' + current_image.file_extension with media_storage.open( current_image_path) as current_image_file: filename = str( current_listing.title.replace(' ', '') ) + '_' + str( screenshot_entry_counter ) + '_' + current_image.image_type.name + '.' + current_image.file_extension copy_to_path = COPY_IMG_PATH + filename screenshot_entry_dict[current_image_type] = { 'filename': filename, 'security_marking': current_image.security_marking } if save_images: print('Copying {} to {}'.format( current_image_file.name, copy_to_path)) copy2(current_image_file.name, copy_to_path) screenshot_entry_counter = screenshot_entry_counter + 1 screenshot_entry_list.append(screenshot_entry_dict) listing['screenshots'] = screenshot_entry_list image_types = [ 'small_icon', 'large_icon', 'banner_icon', 'large_banner_icon' ] for current_image_type in image_types: current_image = getattr(current_listing, current_image_type) current_image_path = str( current_image.id ) + '_' + current_image.image_type.name + '.' + current_image.file_extension with media_storage.open( current_image_path) as current_image_file: filename = str( current_listing.title.replace(' ', '') ) + '_' + current_image.image_type.name + '.' + current_image.file_extension listing[current_image_type] = { 'filename': filename, "security_marking": current_image.security_marking } copy_to_path = COPY_IMG_PATH + filename if save_images: print('Copying {} to {}'.format( current_image_file.name, copy_to_path)) copy2(current_image_file.name, copy_to_path) # Reviews review_list = [] for current_review in models.Review.objects.filter( listing=current_listing).order_by( 'edited_date').iterator(): review_dict = {} review_dict['text'] = current_review.text review_dict['rate'] = current_review.rate review_dict['author'] = current_review.author.user.username review_list.append(review_dict) # library_entries library_entries_list = [] for current_library_entry in models.ApplicationLibraryEntry.objects.filter( listing=current_listing).order_by('position').iterator(): library_entry_dict = {} library_entry_dict['folder'] = current_library_entry.folder library_entry_dict[ 'owner'] = current_library_entry.owner.user.username library_entry_dict['position'] = current_library_entry.position library_entries_list.append(library_entry_dict) # listing_activity listing_activity_list = [] for listing_activity_entry in models.ListingActivity.objects.filter( listing=current_listing).order_by( 'activity_date').iterator(): listing_activity_dict = {} listing_activity_dict['action'] = listing_activity_entry.action listing_activity_dict[ 'author'] = listing_activity_entry.author.user.username listing_activity_dict[ 'description'] = listing_activity_entry.description listing_activity_list.append(listing_activity_dict) # Combine Dictionaries into output_dict output_dict = {} output_dict['listing'] = listing output_dict['listing_review_batch'] = review_list output_dict['library_entries'] = library_entries_list output_dict['listing_activity'] = listing_activity_list output_list.append(output_dict) return output_list
def extract_listings_database(save_images=None): """ Function used to extract listings from database into python objects """ output_list = [] for current_listing in models.Listing.objects.order_by('title').iterator(): listing = {} listing['agency'] = current_listing.agency.short_name listing['title'] = current_listing.title listing['listing_type'] = current_listing.listing_type.title listing['description'] = current_listing.description if current_listing.launch_url: listing['launch_url'] = current_listing.launch_url.replace(DEMO_APP_ROOT, '{DEMO_APP_ROOT}') else: listing['launch_url'] = '{DEMO_APP_ROOT}/default/index.html' listing['version_name'] = current_listing.version_name if current_listing.unique_name: listing['unique_name'] = current_listing.unique_name else: listing['unique_name'] = current_listing.title.lower().replace(' ', '_') listing['what_is_new'] = current_listing.what_is_new listing['description_short'] = current_listing.description_short listing['usage_requirements'] = current_listing.usage_requirements listing['system_requirements'] = current_listing.system_requirements listing['is_enabled'] = current_listing.is_enabled listing['is_featured'] = current_listing.is_featured listing['is_deleted'] = current_listing.is_deleted listing['iframe_compatible'] = current_listing.iframe_compatible listing['security_marking'] = current_listing.security_marking listing['is_private'] = current_listing.is_private # "intents": [], listing['doc_urls'] = sorted([{'name': doc_url.name, 'url': doc_url.url} for doc_url in models.DocUrl.objects.filter(listing=current_listing).all()], key=lambda doc: doc['name']) listing['owners'] = sorted([current_owner.user.username for current_owner in current_listing.owners.iterator()]) # Convert list of tags names into set, then back into list for unique tags name listing['tags'] = sorted(list(set([current_tag.name for current_tag in current_listing.tags.iterator()]))) listing['categories'] = sorted([current_category.title for current_category in current_listing.categories.iterator()]) listing['contacts'] = sorted([current_contact.email for current_contact in current_listing.contacts.iterator()]) screenshot_entry_counter = 0 screenshot_entry_list = [] for screenshot_entry in current_listing.screenshots.iterator(): screenshot_image_types = ['small_image', 'large_image'] screenshot_entry_dict = {} screenshot_entry_dict['order'] = screenshot_entry.order screenshot_entry_dict['description'] = screenshot_entry.description for current_image_type in screenshot_image_types: current_image = getattr(screenshot_entry, current_image_type) current_image_path = str(current_image.id) + '_' + current_image.image_type.name + '.' + current_image.file_extension with media_storage.open(current_image_path) as current_image_file: filename = str(current_listing.title.replace(' ', '')) + '_' + str(screenshot_entry_counter) + '_' + current_image.image_type.name + '.' + current_image.file_extension copy_to_path = COPY_IMG_PATH + filename screenshot_entry_dict[current_image_type] = {'filename': filename, 'security_marking': current_image.security_marking} if save_images: print('Copying {} to {}'.format(current_image_file.name, copy_to_path)) copy2(current_image_file.name, copy_to_path) screenshot_entry_counter = screenshot_entry_counter + 1 screenshot_entry_list.append(screenshot_entry_dict) listing['screenshots'] = screenshot_entry_list image_types = ['small_icon', 'large_icon', 'banner_icon', 'large_banner_icon'] for current_image_type in image_types: current_image = getattr(current_listing, current_image_type) current_image_path = str(current_image.id) + '_' + current_image.image_type.name + '.' + current_image.file_extension with media_storage.open(current_image_path) as current_image_file: filename = str(current_listing.title.replace(' ', '')) + '_' + current_image.image_type.name + '.' + current_image.file_extension listing[current_image_type] = {'filename': filename, "security_marking": current_image.security_marking} copy_to_path = COPY_IMG_PATH + filename if save_images: print('Copying {} to {}'.format(current_image_file.name, copy_to_path)) copy2(current_image_file.name, copy_to_path) # Reviews review_list = [] for current_review in models.Review.objects.filter(listing=current_listing).order_by('edited_date').iterator(): review_dict = {} review_dict['text'] = current_review.text review_dict['rate'] = current_review.rate review_dict['author'] = current_review.author.user.username review_list.append(review_dict) # library_entries library_entries_list = [] for current_library_entry in models.ApplicationLibraryEntry.objects.filter(listing=current_listing).order_by('position').iterator(): library_entry_dict = {} library_entry_dict['folder'] = current_library_entry.folder library_entry_dict['owner'] = current_library_entry.owner.user.username library_entry_dict['position'] = current_library_entry.position library_entries_list.append(library_entry_dict) # listing_activity listing_activity_list = [] for listing_activity_entry in models.ListingActivity.objects.filter(listing=current_listing).order_by('activity_date').iterator(): listing_activity_dict = {} listing_activity_dict['action'] = listing_activity_entry.action listing_activity_dict['author'] = listing_activity_entry.author.user.username listing_activity_dict['description'] = listing_activity_entry.description listing_activity_list.append(listing_activity_dict) # listing_visit_count listing_visit_count_list = [] for listing_visit_count_entry in models.ListingVisitCount.objects.filter(listing=current_listing).order_by('-count').iterator(): listing_visit_count_dict = {} listing_visit_count_dict['profile'] = listing_visit_count_entry.profile.user.username listing_visit_count_dict['count'] = listing_visit_count_entry.count listing_visit_count_dict['last_visit_date'] = listing_visit_count_entry.last_visit_date listing_visit_count_list.append(listing_visit_count_dict) # Combine Dictionaries into output_dict output_dict = {} output_dict['listing'] = listing output_dict['listing_review_batch'] = review_list output_dict['library_entries'] = library_entries_list output_dict['listing_activity'] = listing_activity_list output_dict['listing_visit_count'] = listing_visit_count_list output_list.append(output_dict) return output_list