def RecipeCSVExportView(request): # Create the HttpResponse object with the appropriate CSV header. response = HttpResponse(content_type='text/csv') response[ 'Content-Disposition'] = 'attachment; filename="pants-recipes.csv"' # Use dictionary writer to export nutrition data dicts. # Fields are all standard items plus 'name' and calories which should be 1st fields = [ 'name', 'kilocalories_serve', 'kilocalories', # Total of recipe 'protein_per_j', 'fibre_per_j', 'protein_per_cost', 'fibre_per_cost', 'protein_serve', 'fibre_serve', 'carbohydrate_serve', 'fat_serve', 'grams_serve', 'cost_serve', 'cost_per_kg', 'pf_per_j', 'rank', ] + list(settings.NUTRITION_DATA_ITEMS) + [ 'tags', # TODO: Add a column with ingredient/component names to export? ] writer = csv.DictWriter( response, fieldnames=fields, extrasaction='ignore', # ignore extra data if present in dicts ) writer.writeheader() user = request.user for rec in owner_or_global(Recipe, user).iterator(): data = rec.nutrition_data data['name'] = rec.name data['tags'] = rec.tags.values_list('name', flat=True) writer.writerow(data) return response
def get_queryset(self): # required for access control (can't view other users objects) user = self.request.user return owner_or_global(Recipe, user)
def get_queryset(self): user = self.request.user return owner_or_global(Recipe, user)
def get_queryset(self): self.tag = get_object_or_404(RecipeTag, name=self.args[0]) user = self.request.user return owner_or_global(Recipe, user).filter(tags=self.tag)
def get_queryset(self): user = self.request.user return owner_or_global(Recipe, user).filter(Q(tags__isnull=True))