This repository has been archived on 2019-10-13. You can view files and clone it, but cannot push or open issues or pull requests.
2018-01-17 01:18:33 +01:00

136 lines
4.9 KiB
Python

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os
from io import BytesIO
from PIL import Image
from django.conf import settings
from django.contrib.auth.models import User
from django.core.files.uploadedfile import SimpleUploadedFile
from django.db import models
from django.utils import timezone
MAX_LENGTH = 256
MAX_FOOD_NAME = 256
MAX_FOOD_LOCATION_LENGTH = 256
MAX_FOOD_PRICE_LENGTH = 10
MAX_FOOD_ALLERGENNAME_LENGTH = 256
MAX_HAPPY_HOUR_LOCATION_LENGTH = 256
MAX_HAPPY_HOUR_DESCRIPTION_LENGTH = 1024
# Create your models here.
class Menu(models.Model):
ERBA = 'ERBA'
MARKUSPLATZ = 'MARKUSPLATZ'
FEKI = 'FEKI'
AUSTRASSE = 'AUSTRASSE'
LOCATION_CHOICES = (
(ERBA, 'Erba'), (MARKUSPLATZ, 'Markusplatz'), (FEKI, 'Feldkirchenstrasse'), (AUSTRASSE, 'Austrasse'))
id = models.AutoField(primary_key=True)
date = models.DateField(default=timezone.now)
location = models.CharField(max_length=MAX_FOOD_LOCATION_LENGTH, choices=LOCATION_CHOICES)
menu = models.ManyToManyField("SingleFood", related_name="foods")
class Meta:
unique_together = ('date', 'location')
def __str__(self):
return "Date: %s, Location: %s" % (self.date.strftime("%d.%m.%Y"), self.location)
class SingleFood(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(unique=True, max_length=MAX_FOOD_NAME)
price_student = models.CharField(max_length=MAX_FOOD_PRICE_LENGTH, blank=True, null=True)
price_employee = models.CharField(max_length=MAX_FOOD_PRICE_LENGTH, blank=True, null=True)
price_guest = models.CharField(max_length=MAX_FOOD_PRICE_LENGTH, blank=True, null=True)
image = models.ForeignKey('FoodImage', on_delete=models.PROTECT, blank=True, null=True)
rating = models.FloatField(default=0)
allergens = models.ManyToManyField("Allergene", blank=True)
def __str__(self):
return "%s - Rating: %f - Student Price: %s" % (self.name, self.rating, self.price_student)
class Allergene(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(unique=True, max_length=MAX_FOOD_ALLERGENNAME_LENGTH)
def __str__(self):
return self.name
class HappyHour(models.Model):
id = models.AutoField(primary_key=True)
date = models.DateField(default=timezone.now)
starttime = models.TimeField(default=timezone.now)
endtime = models.TimeField(default=timezone.now)
location = models.CharField(max_length=MAX_HAPPY_HOUR_LOCATION_LENGTH)
description = models.CharField(max_length=MAX_HAPPY_HOUR_DESCRIPTION_LENGTH)
class Meta:
# TODO: unique description instead of date
unique_together = ('date', 'location', 'starttime', 'endtime')
def __str__(self):
return "Date: %s, Location: %s" % (self.date.strftime("%Y.%m.%d"), self.location)
class UserRating(models.Model):
id = models.AutoField(primary_key=True)
user = models.ForeignKey(User, on_delete=models.PROTECT, unique=False)
food = models.ForeignKey(SingleFood, on_delete=models.PROTECT)
rating = models.FloatField(default=0)
def __str__(self):
return "User: %s - Rating: %s" % (self.user.username, self.rating)
class UserFoodImage(models.Model):
id = models.AutoField(primary_key=True)
user = models.ForeignKey(User, on_delete=models.PROTECT, unique=False)
food = models.ForeignKey(SingleFood, on_delete=models.PROTECT)
image = models.ForeignKey('FoodImage', on_delete=models.PROTECT)
class Meta:
unique_together = ('user', 'food')
def __str__(self):
return "User: %s - Image: %s" % (self.user.username, str(self.image))
class FoodImage(models.Model):
id = models.AutoField(primary_key=True)
image = models.ImageField(upload_to='food/originals/%Y/%m/%W', blank=True, null=True)
thumb = models.ImageField(upload_to='food/thumbs/%Y/%m/%W', blank=True, null=True)
def save(self, force_update=False, force_insert=False, thumb_size=(640, 480)):
image = Image.open(self.image)
if image.mode not in ('L', 'RGB'):
image = image.convert('RGB')
image.thumbnail(thumb_size, Image.ANTIALIAS)
# save the thumbnail to memory
temp_handle = BytesIO()
image.save(temp_handle, 'jpeg')
temp_handle.seek(0) # rewind the file
# save to the thumbnail field
suf = SimpleUploadedFile(os.path.split(self.image.name)[-1],
temp_handle.read(),
content_type='image/jpg')
self.thumb.save('%s_thumbnail.%s' % (self.id, 'jpg'), suf, save=False)
# save the image object
self.image.name = "%s_original.%s" % (self.id, 'jpg')
super(FoodImage, self).save(force_update, force_insert)
def delete(self, using=None, keep_parents=False):
os.remove(os.path.join(settings.MEDIA_ROOT, self.image.name))
os.remove(os.path.join(settings.MEDIA_ROOT, self.thumb.name))
super(FoodImage, self).delete()