class Food(models.Model): name = models.CharField(max_length=50) ratings = Ratings() def __unicode__(self): return self.name
class Beverage(models.Model): name = models.CharField(max_length=50) ratings = Ratings(BeverageRating) def __unicode__(self): return self.name
class Snippet(models.Model): title = models.CharField(max_length=255) language = models.ForeignKey(Language) author = models.ForeignKey(User) description = models.TextField() description_html = models.TextField(editable=False) code = models.TextField() highlighted_code = models.TextField(editable=False) version = models.CharField(max_length=5, choices=VERSIONS, default='0') pub_date = models.DateTimeField(auto_now_add=True) updated_date = models.DateTimeField(auto_now=True) bookmark_count = models.IntegerField(default=0) # denormalized count rating_score = models.IntegerField(default=0) # denormaliazed score ratings = Ratings() tags = TaggableManager() objects = SnippetManager() class Meta: ordering = ('-pub_date',) def __str__(self): return self.title def save(self, *args, **kwargs): self.description_html = markdown(self.description, safe_mode="escape") self.highlighted_code = self.highlight() super(Snippet, self).save(*args, **kwargs) @permalink def get_absolute_url(self): return ('cab_snippet_detail', (), {'snippet_id': self.id}) def highlight(self): return highlight(self.code, self.language.get_lexer(), formatters.HtmlFormatter(linenos=True)) def get_tagstring(self): return ", ".join([t.name for t in self.tags.order_by('name').all()]) def get_version(self): return dict(VERSIONS)[self.version] def update_rating(self): self.rating_score = self.ratings.cumulative_score() or 0 self.save() def update_bookmark_count(self): self.bookmark_count = self.bookmarks.count() or 0 self.save()
class Snippet(models.Model): title = models.CharField(max_length=255) language = models.ForeignKey(Language, on_delete=models.CASCADE) author = models.ForeignKey(User, on_delete=models.CASCADE) description = models.TextField() description_html = models.TextField(editable=False) code = models.TextField() highlighted_code = models.TextField(editable=False) version = models.CharField(max_length=5, choices=VERSIONS, default='0.0') pub_date = models.DateTimeField(auto_now_add=True) updated_date = models.DateTimeField(auto_now=True) bookmark_count = models.IntegerField(default=0) # denormalized count rating_score = models.IntegerField(default=0) # denormalized score ratings = Ratings() tags = TaggableManager() objects = SnippetManager() class Meta: ordering = ('-pub_date', ) def __str__(self): return self.title def save(self, *args, **kwargs): self.description_html = sanitize_markdown(self.description) self.highlighted_code = self.highlight() super(Snippet, self).save(*args, **kwargs) def get_absolute_url(self): return reverse('cab_snippet_detail', kwargs={'snippet_id': self.id}) def highlight(self): return highlight(self.code, self.language.get_lexer(), formatters.HtmlFormatter(linenos=True)) def get_tagstring(self): return ", ".join([t.name for t in self.tags.order_by('name').all()]) def get_version(self): return dict(VERSIONS)[self.version] def update_rating(self): self.rating_score = self.ratings.cumulative_score() or 0 self.save() def update_bookmark_count(self): self.bookmark_count = self.bookmarks.count() or 0 self.save() def mark_as_inappropiate(self): snippet_flag = SnippetFlag(snippet=self, user=self.author, flag=SnippetFlag.FLAG_INAPPROPRIATE) snippet_flag.save() def mark_as_spam(self): snippet_flag = SnippetFlag(snippet=self, user=self.author, flag=SnippetFlag.FLAG_SPAM) snippet_flag.save()
class ThreadedComment(Comment): title = models.TextField(_('Title'), blank=True) parent = models.ForeignKey('self', null=True, blank=True, default=None, related_name='children', verbose_name=_('Parent')) last_child = models.ForeignKey('self', null=True, blank=True, on_delete=models.SET_NULL, verbose_name=_('Last child')) tree_path = models.CharField(_('Tree path'), max_length=500, editable=False) objects = CommentManager() ratings = Ratings() @property def depth(self): return len(self.tree_path.split(PATH_SEPARATOR)) @property def root_id(self): return int(self.tree_path.split(PATH_SEPARATOR)[0]) @property def root_path(self): return ThreadedComment.objects.filter( pk__in=self.tree_path.split(PATH_SEPARATOR)[:-1]) @transaction_atomic def save(self, *args, **kwargs): skip_tree_path = kwargs.pop('skip_tree_path', False) super(ThreadedComment, self).save(*args, **kwargs) if skip_tree_path: return None tree_path = str(self.pk).zfill(PATH_DIGITS) if self.parent: tree_path = PATH_SEPARATOR.join((self.parent.tree_path, tree_path)) self.parent.last_child = self ThreadedComment.objects.filter(pk=self.parent_id).update( last_child=self.id) self.tree_path = tree_path ThreadedComment.objects.filter(pk=self.pk).update( tree_path=self.tree_path) def delete(self, *args, **kwargs): # Fix last child on deletion. if self.parent_id: try: prev_child_id = ThreadedComment.objects \ .filter(parent=self.parent_id) \ .exclude(pk=self.pk) \ .order_by('-submit_date') \ .values_list('pk', flat=True)[0] except IndexError: prev_child_id = None ThreadedComment.objects.filter(pk=self.parent_id).update( last_child=prev_child_id) super(ThreadedComment, self).delete(*args, **kwargs) class Meta(object): ordering = ('tree_path', ) db_table = 'threadedcomments_comment' verbose_name = _('Threaded comment') verbose_name_plural = _('Threaded comments')
def index(request): if request.method == "POST": selected_movie = request.POST.get("Movies") rating = request.POST.get("star") r = Ratings(movie_name=selected_movie, ratings=rating) r.save() avg_rating = Ratings.objects.filter( movie_name=selected_movie).aggregate(Avg('ratings')) if request.method == "GET": avg_rating = "" text = "<body style=background-color:white;><h1>Hello, world. Welcome to the Movie Ratings Page</h1>" \ "<!DOCTYPE html>" \ "<html>" \ "<body>" \ "<p><b>List of Movies </b></p>" \ "<form action='/ratings/' method='POST'>" \ "<select name='Movies'>" \ "<option value='baasha'>Baasha</option>" \ "<option value='padayappa'>Padayappa</option>" \ "<option value='enthiran'>Enthiran</option>" \ "<option value='darbar'>Darbar</option>" \ "</select>" \ "<br><br>" \ "<p><b>User Rating</b><p>" \ "<div class='rating'>" \ "<input id='star5' name='star' type='radio' value='5' class='radio-btn hide' />" \ "<label for='star5'' >☆</label>" \ "<input id='star4' name='star' type='radio' value='4' class='radio-btn hide' />" \ "<label for='star4' >☆</label>" \ "<input id='star3' name='star' type='radio' value='3' class='radio-btn hide' />" \ "<label for='star3' >☆</label>" \ "<input id='star2' name='star' type='radio' value='2' class='radio-btn hide' />" \ "<label for='star2' >☆</label>" \ "<input id='star1' name='star' type='radio' value='1' class='radio-btn hide' />" \ "<label for='star1' >☆</label>" \ "</div>" \ "</div>" \ "<style>" \ ".txt-center {" \ "text-align: center;" \ "}" \ ".hide {" \ "display: none;" \ "}" \ ".clear {" \ "float: none;" \ "clear: both;" \ "}" \ ".rating {" \ "width: 90px;" \ "unicode-bidi: bidi-override;" \ "direction: rtl;" \ "text-align: center;" \ "position: relative;" \ "}" \ ".rating > label {" \ "float: right;" \ "display: inline;" \ "padding: 0;" \ "margin: 0;" \ "position: relative;" \ "width: 1.1em;" \ "cursor: pointer;" \ "color: #000;" \ "}" \ ".rating > label:hover," \ ".rating > label:hover ~ label," \ ".rating > input.radio-btn:checked ~ label {" \ "color: yellow;" \ "}" \ ".rating > label:hover:before," \ ".rating > label:hover ~ label:before," \ ".rating > input.radio-btn:checked ~ label:before," \ ".rating > input.radio-btn:checked ~ label:before {" \ "position: absolute;" \ "left: 0;" \ "color: yellow;" \ "}" \ "</style>" \ "<br>" \ "</br>" \ "<p><b>Avg Rating </b></p>" \ +str(avg_rating)+ \ "<br></br>" \ "<input type='submit'>" \ "</form>" \ "</body>" \ "</html>" return HttpResponse(text)
def add_rating(request): if request.method == 'POST': score = int(request.POST['score']) comment = request.POST['comment'] user_id = request.POST['user_id'] user_name = request.POST['user_name'] try: realtor_id = request.POST['realtor_id'] path = 'realtors' rating_type = 1 rating_id = realtor_id except: listing_id = request.POST['listing_id'] path = 'listings' rating_type = 2 rating_id = listing_id if (not isinstance(score, int)) or (score < 1 or score > 5): messages.error(request, 'You entered an invalid score!') return redirect('/' + path + '/' + rating_id) if request.user.is_authenticated: user_id = request.user.id if rating_type == 1: has_rating = Ratings.objects.all().filter(user_id=user_id, realtor_id=rating_id, type=rating_type, is_published=True) if has_rating: messages.error(request, 'You have already rated this realtor!') return redirect('/' + path + '/' + rating_id) else: has_rating = Ratings.objects.all().filter(user_id=user_id, listing_id=rating_id, type=rating_type, is_published=True) if has_rating: messages.error(request, 'You have already rated this house!') return redirect('/' + path + '/' + rating_id) else: messages.error(request, 'Please login') return redirect('login') if rating_type == 1: rating = Ratings(score=score, user_id=user_id, user_name=user_name, realtor_id=rating_id, content=comment, type=rating_type, is_published=True) else: rating = Ratings(score=score, user_id=user_id, user_name=user_name, listing_id=rating_id, content=comment, type=rating_type, is_published=True) rating.save() return redirect('/' + path + '/' + rating_id)