def favorite(request, response): id = param(request, "id", int) book = _get_book(id) try: request.user.book_favorite_set.create(book=book) except: raise AjaxError("你已经收藏过了。")
def favorite(request, response): id = param(request, 'id', int) book = _get_book(id) try: request.user.book_favorite_set.create(book=book) except: raise AjaxError('你已经收藏过了。')
def set_rate(request, response): id = param(request, 'id', int) rate = param(request, 'rate', int) if rate == 0 or rate > BOOK_RATE_MAX or rate < BOOK_RATE_MIN: raise AjaxError(u'评分不正确') book = _get_book(id) if book.check_rated(request.user): raise AjaxError(u'你已经给此小说评过分了') book.book_rate_set.create(user=request.user, rate=rate) # 记录已评分 # 计算帖子平均分值 #sum = book.book_rate_set.aggregate(models.Sum('rate'))[ 'rate__sum' ] or 0 #count = book.rate_count + 1 Book.objects.filter(pk=book.pk).update(rate_score=models.F('rate_score')+rate, rate_count=models.F('rate_count')+1)
def set_rate(request, response): id = param(request, 'id', int) rate = param(request, 'rate', int) if rate == 0 or rate > BOOK_RATE_MAX or rate < BOOK_RATE_MIN: raise AjaxError(u'评分不正确') book = _get_book(id) if book.check_rated(request.user): raise AjaxError(u'你已经给此小说评过分了') book.book_rate_set.create(user=request.user, rate=rate) # 记录已评分 # 计算帖子平均分值 #sum = book.book_rate_set.aggregate(models.Sum('rate'))[ 'rate__sum' ] or 0 #count = book.rate_count + 1 Book.objects.filter(pk=book.pk).update( rate_score=models.F('rate_score') + rate, rate_count=models.F('rate_count') + 1)
def validate_username_uniqueness(request, response): username = param(request, "username", unicode) try: _validate_username(username) _validate_username_uniqueness(username) return {"message": u"用户名可以使用", "valid": True} except ValidationError, e: return {"message": u",".join(e.messages), "valid": False}
def validate_username_uniqueness(request, response): username = param(request, 'username', unicode) try: _validate_username(username) _validate_username_uniqueness(username) return {'message':u'用户名可以使用', 'valid':True} except ValidationError, e: return {'message':u','.join(e.messages), 'valid':False}
def save_point(request, response): chapter_id = param(request, "chapter_id", int) try: chapter = Chapter.objects.select_related("book").get(pk=chapter_id) except: raise AjaxError(u"Chapter 不存在") point, is_create = ReadPoint.objects.get_or_create(user=request.user, book=chapter.book) point.chapter = chapter point.save()
def save_point(request, response): chapter_id = param(request, 'chapter_id', int) try: chapter = Chapter.objects.select_related('book').get(pk=chapter_id) except: raise AjaxError(u'Chapter 不存在') point, is_create = ReadPoint.objects.get_or_create(user=request.user, book=chapter.book) point.chapter = chapter point.save()
def delete_point(request, response): id = param(request, "id", int) request.user.book_readpoint_set.filter(id=id).delete()
def stat(request, response): id = param(request, "id", int) Book.objects.filter(pk=id).update(views=models.F("views") + 1)
def delete_favorite(request, response): id = param(request, 'id', int) request.user.book_favorite_set.filter(id=id).delete()
def delete_chapter(request, response): id = param(request, 'id', int) Chapter.objects.filter(id=id).delete()
def delete_point(request, response): id = param(request, 'id', int) request.user.book_readpoint_set.filter(id=id).delete()
def search_book(request, response): title = param(request, 'title', unicode) if title: s = Book.search.query('@title %s' % title).set_options(passages=True, mode='SPH_MATCH_EXTENDED2').all()[0:10] if s: return [{'id':v.id, 'title':v.sphinx['passages']['title'], 'author':v.author} for v in s]
def search_book(request, response): title = param(request, "title", unicode) if title: s = Book.search.query("@title %s" % title).set_options(passages=True, mode="SPH_MATCH_EXTENDED2").all()[0:10] if s: return [{"id": v.id, "title": v.sphinx["passages"]["title"], "author": v.author} for v in s]
def stat(request, response): id = param(request, 'id', int) Book.objects.filter(pk=id).update(views=models.F('views') + 1)
def delete_chapter(request, response): id = param(request, "id", int) Chapter.objects.filter(id=id).delete()
def delete_favorite(request, response): id = param(request, "id", int) request.user.book_favorite_set.filter(id=id).delete()