Exemplo n.º 1
0
def detail(request, slug):
    book = get_object_or_404(Book, slug=slug)
    reviews = Review.objects.filter(book=book).order_by('-likes')
    paginator = Paginator(reviews, 10)
    page = request.GET.get('page')
    if request.method == 'POST':
        review_form = ReviewForm(data=request.POST)
        if review_form.is_valid():
            new_review = review_form.save(commit=False)
            new_review.book = book
            new_review.user = request.user
            new_review.save()
            messages.success(request, 'Review added successfully')
            create_action(request.user, 'wrote a review', new_review)
            review_form = ReviewForm()
    else:
        review_form = ReviewForm()
        try:
            reviews = paginator.page(page)
        except PageNotAnInteger:
            reviews = paginator.page(1)
        except EmptyPage:
            if request.is_ajax():
                return HttpResponse('')
            reviews = paginator.page(paginator.num_pages)
        if request.is_ajax():
            return render(request, 'books/detail.html', {'reviews': reviews})

    return render(request, 'books/detail.html', {
        'book': book,
        'reviews': reviews,
        'review_form': review_form,
    })
Exemplo n.º 2
0
def post_write(request):
    """
    View for writing a Post.
    """
    if request.method == 'POST':
        # form is sent
        form = PostForm(data=request.POST, files=request.FILES)
        if form.is_valid():
            # form data is valid
            post = form.save(commit=False)
            post.user = request.user
            post.save()
            messages.success(request, 'Post written successfully')
            create_action(request.user, 'wrote a post', post)
            # redirect to new created item detail view
            return redirect(post.get_absolute_url())
    else:
        # build form with data provided by the bookmarklet via GET
        form = PostForm()

    return render(request, 'posts/write.html', {
        'form': form,
    })
Exemplo n.º 3
0
def user_follow(request):
    profile_name = request.POST['username']
    action = request.POST['action']
    if profile_name and action:
        try:
            profile_to_follow = Profile.objects.get(user=User.objects.get(
                username=profile_name))
            if action == 'follow':
                Follow.objects.get_or_create(
                    profile_from=Profile.objects.get(user=request.user),
                    profile_to=profile_to_follow)
                create_action(Profile.objects.get(user=request.user),
                              'is following', profile_to_follow)
            else:
                Follow.objects.filter(
                    profile_from=Profile.objects.get(user=request.user),
                    profile_to=profile_to_follow).delete()

            return JsonResponse({'status': 'ok'})

        except Profile.DoesNotExist:
            return JsonResponse({'status': 'error'})
    return JsonResponse({'status': 'error'})
Exemplo n.º 4
0
def book_create(request):
    """
    View for creating a Book.
    """
    if request.method == 'POST':
        # form is sent
        form = BookForm(data=request.POST, files=request.FILES)
        if form.is_valid():
            # form data is valid
            book = form.save(commit=False)
            book.user = request.user
            book.save()
            form.save_m2m()
            messages.success(request, 'Book added successfully')
            create_action(request.user, 'added a book', book)
            # redirect to new created item detail view
            return redirect(book.get_absolute_url())
    else:
        # build form with data provided by the bookmarklet via GET
        form = BookForm()

    return render(request, 'books/create.html', {
        'form': form,
    })
Exemplo n.º 5
0
 def create_clap_action(self, bundle_to_clap):
     create_action(Profile.objects.get(user=self.request.user),
                   'clapped the bundle', bundle_to_clap)
Exemplo n.º 6
0
 def create_comment_action(self, bundle_to_comment):
     create_action(Profile.objects.get(user=self.request.user),
                   'commented on bundle', bundle_to_comment)
Exemplo n.º 7
0
 def create_fork_action(self, bundle_from):
     create_action(Profile.objects.get(user=self.request.user),
                   'forked bundle', bundle_from)
Exemplo n.º 8
0
 def create_published_action(self):
     if self.object.status == 'Publish':
         create_action(Profile.objects.get(user=self.request.user),
                       'created a new bundle', self.object)