Source code for notes.models

from random import choices
from typing import Optional
from django.db import models
from core.models import Addition
from django.contrib.auth.models import AbstractUser
from django.core.validators import MaxValueValidator, MinValueValidator,FileExtensionValidator
from django.core.exceptions import ValidationError
from django.dispatch import receiver
from django.db.models.signals import (post_save)
from django.core.mail import send_mail
from django.contrib import messages
from django.utils.timezone import localtime, now
import datetime
from datetime import datetime, timedelta,date
from core.models import Style,Country,Account,User
#import magic # type: ignore
from core.functions import get_account_id,get_account_name 

""" Two Models are used by Notes: 
    1. Notes: This model is used to store the tasting notes for a wine. 
    2. NotesMedia: This model is used to store the media files associated with the tasting notes. 
""" 
pdf_validator = FileExtensionValidator(['pdf'])
img_validator = FileExtensionValidator(['png','jpg','jpeg',])


#def validate_file_mimetype(file):
#    accept=['image/png','image/jpeg','application/pdf']
#    file_mime_type = magic.from_buffer(file.read(1024),mime=True)
#    print(file_mime_type)
#    if file_mime_type not in accept:
#        raise ValidationError("Unsupported filetype ") 

[docs] class Notes (models.Model): TASTESCORES = [ ('Pending','Pending'), ('Bronze','Bronze'), ('Silver','Silver'), ('Gold', 'Gold'), ('Platinum','Platinum') ] FRUITYCHOICES = [ ('Lemon','Lemon'), ('Grapefruit','Grapefruit'), ('Orange','Orange'), ('Pineapple','Pineapple'), ('Banana','Banana'), ('Lychee','Lychee'), ('Melon','Melon'), ('Muscat','Muscat'), ('Apple','Apple'), ('Pear','Pear'), ('Quince','Quince'), ('Strawberry','Strawberry'), ('Raspberry','Raspberry'), ('Redcurrant','Recurrant'), ('Blackcurrant','Blackcurrant'), ('Blueberry','Blueberry'), ('Blackberry','Blackberry'), ('Cherry','Cherry'), ('Apricot','Apricot'), ('Peach','Peach'), ('Almond','Almond'), ('Prune','Prune'), ('Walnut','Walnut'), ('-','-'), ] VEGETALCHOICES = [ ('Green Pepper','Green Pepper'), ('Mushroom','Mushroom'), ('Truffle','Truffle'), ('Wine Lees','Wine Lees'), ('Cedar','Cedar'), ('Pine','Pine'), ('Liquorice','Liquorice'), ('Blackcurrant bud','Blackcurrant bud'), ('Cut Hay','Cut Hay'), ('Thyme','Thyme'), ('Vanilla','Vanilla'), ('Cinamon','Cinamon'), ('Clove','Clove'), ('Pepper','Pepper'), ('Saffron','Saffron'), ('-','-'), ] FLORALCHOICES = [ ('Violet','Violet'), ('Rose','Rose'), ('Acacia','Acacia'), ('Hawthorn','Hawthorn'), ('Roasted Hazlenut','Roasted Hazlenut'), ('Honey','Honey'), ('Linden','Linden'), ('-','-'), ] ROASTEDCHOICES = [ ('Leather','Leather'), ('Musk','Musk'), ('Butter','Butter'), ('Toast','Toast'), ('Roasted Almond','Roasted Almond'), ('Caramel','Caramel'), ('Coffee','Coffee'), ('Smoke','Smoke'), ('Dark Chocolate','Dark Chocolate'), ('-','-'), ] NOTETYPES = [ ('Cellar', 'Cellar'), ('General', 'General'), ('Tasting', 'Non cellar Tasting'), ] key = models.CharField(max_length=100, default='',blank=True) addition = models.ForeignKey(Addition,null=True,blank=True,default='',on_delete=models.DO_NOTHING) type = models.CharField(max_length=8,default='G',choices=NOTETYPES) title = models.CharField(max_length=75, default='') style = models.ForeignKey(Style,blank=True,null=True,on_delete=models.DO_NOTHING) country = models.ForeignKey(Country,blank=True,null=True,on_delete=models.DO_NOTHING) year = models.CharField(max_length=8,default='',blank=True) notes = models.TextField() fruit_description = models.CharField(max_length=30,choices=FRUITYCHOICES,default='-') vegetal_description = models.CharField(max_length=30,choices=VEGETALCHOICES,default='-') floral_description = models.CharField(max_length=30,choices=FLORALCHOICES,default='-') roasted_description = models.CharField(max_length=30,choices=ROASTEDCHOICES,default='-') nosescore = models.CharField(max_length=30,choices=TASTESCORES,default='Pending') appearancescore = models.CharField(max_length=30,choices=TASTESCORES,default='Pending') tastescore = models.CharField(max_length=30,choices=TASTESCORES,default='Pending') overallscore = models.CharField(max_length=30,choices=TASTESCORES,default='Pending') created = models.DateTimeField(auto_now_add=True) last_update = models.DateTimeField(auto_now=True) account = models.ForeignKey(Account,default='1',on_delete=models.DO_NOTHING) #image = models.ImageField(upload_to='images',null=True,blank=True,validators=[img_validator,validate_file_mimetype]) image = models.ImageField(upload_to='images',null=True,blank=True,validators=[img_validator]) #@property #def total_score(self): # sum_of_scores = int(self.nosescore) + int(self.appearancescore)+ int(self.tastescore)+ int(self.overallscore) # return sum_of_scores @property def nosevalue(self): return value_convert(self.nosescore) @property def appearancevalue(self): return value_convert(self.appearancescore) @property def tastevalue(self): return value_convert(self.tastescore) @property def overallvalue(self): return value_convert(self.overallscore) @property def totalvalue(self): return (value_convert(self.overallscore)+ value_convert(self.nosescore) + value_convert(self.appearancescore) + value_convert(self.tastescore) ) class Meta: ordering = ["created"] def __str__(self): return self.title
[docs] def delete(self): # wont work for queryset deletion only for single items self.image.delete() super().delete()
[docs] def upload_path(instance,filename): return "user_{0}/{1}".format(instance.user.id, filename)
[docs] class NotesMedia(models.Model): note = models.ForeignKey(Notes,on_delete=models.CASCADE,related_name='note_key') #file = models.FileField(upload_to=upload_path,validators=[pdf_validator,validate_file_mimetype]) file = models.FileField(upload_to=upload_path,validators=[pdf_validator]) name = models.CharField(max_length=30) user = models.ForeignKey(User,on_delete=models.CASCADE) def __str__(self): return self.name
[docs] def delete(self): # wont work for queryset deletion only for single items self.image.delete() super().delete()
[docs] def value_convert(score): if score == 'Pending': return 0 if score == 'Bronze': return 1 if score == 'Silver': return 3 if score == 'Gold': return 5 if score == 'Platinum': return 9