def create(self, ref_type, ref_pk, author): if self._instance is not None: message = "Failed to create comment: this form is bound to an existing comment." raise ProgrammingError(message) if not self.is_valid(): raise InvalidFormError(self.errors) instance = Comment.create( ref_type=ref_type, ref_pk=ref_pk, author=author, **self.cleaned_data ) instance.save() return instance
def get_page(self): # Find newly added restos (randomly select 4 out of 10). newly_added_restos = list(Resto.find(order_by="-update_date", limit=10)) random.shuffle(newly_added_restos) newly_added_restos = newly_added_restos[:4] # Find newly commented restos (a list of 2-tuple with resto and comment). new_comments = Comment.find_recent(ref_type=Resto.__name__, limit=4) newly_commented_restos = [] for comment in new_comments: resto = Resto.get_unique(id=int(comment.ref_pk)) if resto: newly_commented_restos.append((resto, comment)) # Render the response. data = { "categories": Resto.CATEGORIES, "newly_added_restos": newly_added_restos, "newly_commented_restos": newly_commented_restos, } data = self.update_data(data) return render_to_response(self.get_page_template(), data, RequestContext(self.request))
def update_data(self, data): comments = Comment.find(ref_type=self.ref_type, ref_pk=self.ref_pk) data["comments"] = comments return super(ViewComments, self).update_data(data)