Advertisement
Guest User

Untitled

a guest
Nov 29th, 2015
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.66 KB | None | 0 0
  1. class Cafe(models.Model):
  2.     name = models.CharField(max_length=100)
  3.     owner = models.ForeignKey(User)
  4.     description = models.TextField()
  5.     adress = models.TextField()
  6.     image = models.ImageField()
  7.     registered_at = models.DateField()
  8.     last_updated = models.DateField()
  9.     rating = models.FloatField(default=0)
  10.    
  11.     def calc_rating(self):
  12.         comments = CommentCafe.objects.filter(cafe_id = self.id)
  13.         rating = (0.0+sum([obj.rating for obj in comments]))/len(comments)
  14.         rating = round(rating, 2)
  15.         x = 0.25 # value for round rating
  16.         rating = round(rating * (1 / x)) / (1 / x)
  17.         self.rating = rating
  18.         self.save()
  19.    
  20.     def __unicode__(self):
  21.         return "%s ( %s... )" % (self.name, self.description[:100])
  22.    
  23.    
  24. class Product(models.Model):
  25.     cafe = models.ForeignKey(Cafe)
  26.     name = models.CharField(max_length=70)
  27.     description = models.TextField()
  28.     image = models.ImageField()
  29.     last_updated = models.DateField()
  30.    
  31.     def __unicode__(self):
  32.         return "%s ( %s... )" % (self.name, self.description[:50])
  33.    
  34.    
  35. class Comment(models.Model):
  36.     class Meta:
  37.         abstract = True
  38.  
  39.     user = models.ForeignKey(User)
  40.     rating = models.PositiveSmallIntegerField()
  41.     text = models.CharField(max_length=250)
  42.     created_at = models.DateTimeField()
  43.    
  44.     def __unicode__(self):
  45.         return "%s (%d) : %s" % (self.user.first_name, self.rating,
  46.                                 self.text[:50])
  47.    
  48.    
  49. class CommentCafe(Comment):
  50.     cafe = models.ForeignKey(Cafe)
  51.  
  52.  
  53. class CommentProduct(Comment):
  54.     product = models.ForeignKey(Product)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement