Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Cafe(models.Model):
- name = models.CharField(max_length=100)
- owner = models.ForeignKey(User)
- description = models.TextField()
- adress = models.TextField()
- image = models.ImageField()
- registered_at = models.DateField()
- last_updated = models.DateField()
- rating = models.FloatField(default=0)
- def calc_rating(self):
- comments = CommentCafe.objects.filter(cafe_id = self.id)
- rating = (0.0+sum([obj.rating for obj in comments]))/len(comments)
- rating = round(rating, 2)
- x = 0.25 # value for round rating
- rating = round(rating * (1 / x)) / (1 / x)
- self.rating = rating
- self.save()
- def __unicode__(self):
- return "%s ( %s... )" % (self.name, self.description[:100])
- class Product(models.Model):
- cafe = models.ForeignKey(Cafe)
- name = models.CharField(max_length=70)
- description = models.TextField()
- image = models.ImageField()
- last_updated = models.DateField()
- def __unicode__(self):
- return "%s ( %s... )" % (self.name, self.description[:50])
- class Comment(models.Model):
- class Meta:
- abstract = True
- user = models.ForeignKey(User)
- rating = models.PositiveSmallIntegerField()
- text = models.CharField(max_length=250)
- created_at = models.DateTimeField()
- def __unicode__(self):
- return "%s (%d) : %s" % (self.user.first_name, self.rating,
- self.text[:50])
- class CommentCafe(Comment):
- cafe = models.ForeignKey(Cafe)
- class CommentProduct(Comment):
- product = models.ForeignKey(Product)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement