@login_required()
def generate_csv(request):
file = 'inventory.csv'
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="%s"' % file
equipment = Equipment.objects.filter(is_active=True)
writer = csv.writer(response, delimiter=';')
for eq in equipment:
writer.writerow([eq.field_1, eq.field_2, ...])
return response
class Vegetable(models.Model):
tomato = models.OneToOneField(Tomato, verbose_name='Томат', on_delete=models.CASCADE, blank=True, null=True)
potatoes = models.OneToOneField(Potato, verbose_name='Potatoes', on_delete=models.CASCADE, blank=True, null=True)
eggplant = models.OneToOneField(Eggplant, verbose_name='Eggplant', on_delete=models.CASCADE, blank=True, null=True)
gifts = models.ManyToManyField('self', blank=True, symmetrical=False)
@property
def get_price(self):
tomato = self.tomato.price if self.tomato is not None else 0
potatoes = self.potatoes.price if self.potatoes is not None else 0
eggplant = self.eggplant.price if self.eggplant is not None else 0
return sum([tomato, potatoes, eggplant])
@property
def get_gift_price(self):
return sum([x.get_price for x in self.gifts.all()])
@property
def get_final_price:
return self.price - self.price * self.discount
def images_upload_folder(instance, filename):
return '{model}s/{filename}'.format(model=instance._meta.model_name, filename=filename)
class BannerImage(TimeStampedModel):
banner = models.ForeignKey(Banner, related_name='images')
image = models.ImageField(upload_to=images_upload_folder, blank=True)
sort = models.IntegerField(default=1000, verbose_name='Сортировка')
is_active = models.BooleanField(default=True, verbose_name='Включить')
objects = models.Manager()
active = IsActiveManager()
def __str__(self):
return "{banner} > {file}".format(banner=self.banner.title, file=self.image.file)
class BannerImageInLine(admin.TabularInline):
model = BannerImage
class BannerAdmin(admin.ModelAdmin):
inlines = [
BannerImageInLine
]
admin.site.register(Banner, BannerAdmin)
def category(request, slug):
context = {
'posts': Post.objects.filter(category__slug=slug)
}
return render(request, 'category.html', context)
url(r'^humans.txt$', TemplateView.as_view(template_name='humans.txt', content_type='text/plain')),
url(r'^robots.txt$', TemplateView.as_view(template_name='robots.txt', content_type='text/plain')),
url(r'^shell/$', TemplateView.as_view(template_name="python/shell.html"), name='shell'),
# python section landing pages
url(r'^about/$', TemplateView.as_view(template_name="python/about.html"), name='about'),
class Comment(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
post = models.ForeignKey(Post, related_name="comments")
content = models.TextField()
time = models.DateTimeField(auto_now_add = True)