예제 #1
0
def follow_person(request, username, url):
    authenticated_user = get_authenticated_user(request)
    person = Person.objects.get(username=username)

    if person in authenticated_user.following.all():
        authenticated_user.following.remove(person)
        authenticated_user.len_following = int(
            authenticated_user.len_following) - 1

        person.followers.remove(authenticated_user)
        person.len_followers = int(person.len_followers) - 1

    else:
        authenticated_user.following.add(person)
        authenticated_user.len_following = int(
            authenticated_user.len_following) + 1

        person.followers.add(authenticated_user)
        person.len_followers = int(person.len_followers) + 1

        notif = Notification(
            givver=person,
            message=
            f'<a href="/user/{authenticated_user.username}/">{authenticated_user.name}</a> از حالا شما را دنبال می‌کند',
            notif_type='follow')
        notif.save()

    person.save()
    authenticated_user.save()

    return HttpResponseRedirect(url.replace('%2F', '/'))
예제 #2
0
def post_like(request, username, post_id):
    authenticated_user = get_authenticated_user(request)
    post = Post.objects.get(id=post_id)

    response_data = {}

    if request.POST.get('action') == 'post':
        if post in authenticated_user.liked_posts.all():
            post.likes = int(post.likes) - 1
            authenticated_user.liked_posts.remove(post)
            response_data[
                'like_btn'] = f'<button type="submit" class="btn btn-light border-gray"><i class="far fa-heart"></i> {post.likes}</button>'

        else:
            post.likes = int(post.likes) + 1
            authenticated_user.liked_posts.add(post)
            response_data[
                'like_btn'] = f'<button type="submit" class="btn btn-light text-danger border-gray"><i class="fas fa-heart"></i> {post.likes}</button>'

            notif = Notification(
                givver=post.author,
                message=
                '<a href="/user/{0}/">{1}</a> مطلب «<a href="/user/{2}/post/{3}/">{4}</a>» شما را لایک کرد'
                .format(authenticated_user.username, authenticated_user.name,
                        post.author.username, post.id, post.title),
                notif_type='like')
            notif.save()

        authenticated_user.save()
        post.save()

        return JsonResponse(response_data)

    return HttpResponseRedirect('/user/' + username + '/post/' + str(post.id))
예제 #3
0
def index(request):
    authenticated_user = get_authenticated_user(request)

    # Load all posts order by there publish time
    posts = Post.objects.all().order_by('-publish_time')
    paginate = Paginator(posts, 5)  # Paginate by 5

    # Load banner items
    try:
        all_ads = Ad.objects.filter(type='بنر')
        for ad in all_ads:
            if ad.end_date <= jdatetime.datetime.today():
                ad.delete()

        ad1, ad2, ad3 = sample(list(all_ads), 3)

    except:
        ad1, ad2, ad3 = None, None, None

    context = {
        'authenticated_user': authenticated_user,
        'new_notifications': get_new_notifications(authenticated_user),
        'posts': paginate.page(1),
        'ad1': ad1,
        'ad2': ad2,
        'ad3': ad3,
    }

    # Show 'index.html' to user
    return render(request, 'index.html', context)
예제 #4
0
def add_comment(request, username, slug):
    authenticated_user = get_authenticated_user(request)
    person = get_object_or_404(Person, username=username)  # Get the Person
    post = get_object_or_404(Post, author=person, slug=slug)  # Get the Post

    if request.method == 'POST':
        form = CommentForm(request.POST)  # Get form

        if form.is_valid():
            text = form.cleaned_data['textInput']  # Read body

            # Create a Comment model with form data
            comment = Comment(
                author=authenticated_user,
                text=text,
            )
            comment.save()  # Save it

            post.comments.add(comment)
            post.len_comments = int(post.len_comments) + 1
            post.save()

            notif = Notification(
                receiver=post.author,
                message=
                f'<a href="/user/{authenticated_user.username}/">{authenticated_user.name}</a> نظری روی مطلب «<a href="/user/{username}/post/{slug}/">{post.title}</a>» شما ارسال کرد',
                notif_type='comment')
            notif.save()

    # Redirect user to 'person:post:detail' url
    return HttpResponseRedirect('/user/' + username + '/post/' + slug)
예제 #5
0
def like(request, username, slug):
    authenticated_user = get_authenticated_user(request)
    person = get_object_or_404(Person, username=username)  # Get the Person
    post = get_object_or_404(Post, author=person, slug=slug)  # Get the Post

    # If user has liked post before
    if post in authenticated_user.liked_posts.all():
        post.likes.remove(authenticated_user)
        post.len_likes = int(post.len_likes) - 1
        authenticated_user.liked_posts.remove(post)
        responseText = f'<button class="btn btn-light me-2" type="button" onclick="likePost(\'{post.author.username}\', \'{post.slug}\', \'{post.id}\')"><i class="far fa-heart"></i> <span class="fa-num">{post.len_likes}</span></button>'

    # If user has not liked post before
    else:
        post.likes.add(authenticated_user)
        post.len_likes = int(post.len_likes) + 1
        authenticated_user.liked_posts.add(post)
        responseText = f'<button class="btn btn-outline-danger me-2" type="button" onclick="likePost(\'{post.author.username}\', \'{post.slug}\', \'{post.id}\')"><i class="fas fa-heart"></i> <span class="fa-num">{post.len_likes}</span></button>'

        notif = Notification(
            receiver=post.author,
            message=
            f'<a href="/user/{authenticated_user.username}/">{authenticated_user.name}</a> مطلب «<a href="/user/{username}/post/{slug}/">{post.title}</a>» شما را پسند کرد',
            notif_type='like')
        notif.save()

    authenticated_user.save()
    post.save()

    # Response the data
    return HttpResponse(responseText)
예제 #6
0
def reply_comment(request, username, slug, comment_id):
    authenticated_user = get_authenticated_user(request)
    comment = get_object_or_404(Comment, id=comment_id)  # Get the Comment

    if request.method == 'POST':
        form = CommentForm(request.POST)  # Get form

        if form.is_valid():
            text = form.cleaned_data['textInput']  # Read body

            # Create a Comment model with form data
            new_comment = Comment(
                author=authenticated_user,
                text=text,
            )
            new_comment.save()  # Save it

            # Add new comment to comment replys
            comment.replys.add(new_comment)

            notif = Notification(
                receiver=comment.author,
                message=
                f'<a href="/user/{authenticated_user.username}/">{authenticated_user.name}</a> پاسخی روی <a href="/user/{username}/post/{slug}/#comment_{comment.id}">نظر</a> شما ارسال کرد',
                notif_type='reply')
            notif.save()

    # Redirect user to 'person:post:detail' url
    return HttpResponseRedirect('/user/' + username + '/post/' + slug)
예제 #7
0
def edit_comment(request, username, slug, comment_id):
    authenticated_user = get_authenticated_user(request)
    person = get_object_or_404(Person, username=username)  # Get the Person
    post = get_object_or_404(Post, author=person, slug=slug)  # Get the Post
    comment = get_object_or_404(post.comments,
                                id=comment_id)  # Get the Comment

    if authenticated_user == comment.author:
        if request.method == 'POST':
            form = CommentForm(request.POST)  # Get form

            if form.is_valid():
                text = form.cleaned_data['textInput']  # Read body

                # Replace comment text with new one
                comment.text = text
                comment.save()  # Save it

        # Redirect user to 'person:post:detail' url
        return HttpResponseRedirect('/user/' + username + '/post/' + slug)

    # If registered user not comment author
    context = {
        'authenticated_user': authenticated_user,
        'new_notifications': get_new_notifications(authenticated_user),
    }

    # Show 'forbidden.html' to user
    return render(request, 'forbidden.html', context)
예제 #8
0
def detail(request, username, slug):
    authenticated_user = get_authenticated_user(request)
    person = get_object_or_404(Person, username=username)  # Get the Person
    post = get_object_or_404(Post, author=person, slug=slug)  # Get the Post

    # Add to post view
    post.views = int(post.views) + 1
    post.save()

    # Add post to user's viewed posts
    if authenticated_user is not None:
        authenticated_user.viewed_posts.add(post)
        authenticated_user.save()

    context = {
        'authenticated_user': authenticated_user,
        'new_notifications': get_new_notifications(authenticated_user),
        'post': post,
        'person': person,
        'post_body': translate_to_html(post.body),
        'ads_list': ads_list(),
    }

    # Show 'user/post/detail.html' to user
    return render(request, 'user/post/detail.html', context)
예제 #9
0
def edit_rezome(request):
    authenticated_user = get_authenticated_user(request)

    if request.method == 'POST':
        form = RezomeForm(request.POST)

        if form.is_valid():
            rezome = form.cleaned_data['rezome']
            rezome = rezome.replace('<', '&lt;').replace('>', '&gt;').replace(
                '\n', '<br/>')  # Clean the rezome and recognize line breaks

            authenticated_user.rezome = rezome
            authenticated_user.save()

            return HttpResponseRedirect('/user/' +
                                        authenticated_user.username +
                                        '/rezome/')

    else:
        form = RezomeForm(
            initial={
                'rezome':
                authenticated_user.rezome.replace('<br/>', '\n').replace(
                    '&lt;', '<').replace('&gt;', '>')
            })

    context = {
        'authenticated_user': authenticated_user,
        'new_notifications': get_new_notifications(authenticated_user),
        'form': form,
    }

    return render(request, 'person/rezome/edit.html', context)
예제 #10
0
def posts(request, page):
    authenticated_user = get_authenticated_user(request)

    # Load all posts order by there publish time
    posts = Post.objects.all().order_by('-publish_time')
    paginate = Paginator(posts, 3)  # Paginate by 3

    # Load banner
    try:
        ad = Ad.objects.filter(type='صفحه اول')
        ad = choice(ad)

        while ad.available_views == '0':
            ad.delete()
            ad = choice(Ad.objects.filter(type='صفحه اول'))

        ad.available_views = int(ad.available_views) - 1
        ad.save()

    except:
        ad = None

    context = {
        'authenticated_user': authenticated_user,
        'new_notifications': get_new_notifications(authenticated_user),
        'posts': paginate.page(page),
        'ad': ad,
    }

    # Show "index" page template to user
    return render(request, 'index/index.html', context)
예제 #11
0
def edit_post(request, post_id):
    authenticated_user = get_authenticated_user(request)
    post = Post.objects.get(id=post_id)  # Get the Post

    # If registered user is post author
    if post.author.username == authenticated_user.username:
        # If form method == POST
        if request.method == 'POST':
            form = PostForm(request.POST)  # Get form

            if form.is_valid():
                title = form.cleaned_data['title']  # Read title
                body = form.cleaned_data['body']  # Read body
                cover = form.cleaned_data['cover']  # Read cover
                short_description = form.cleaned_data[
                    'short_description']  # Read short description
                category = form.cleaned_data['category']  # Read category

                # Give new data to post
                post.title = title
                post.body = body
                post.cover = cover
                post.short_description = short_description
                post.category = category
                post.save()  # Save it

                return HttpResponseRedirect('/user/' + post.author.username +
                                            '/post/' + str(post.id))

        # If form method == GET
        else:
            # Give form to user
            form = PostForm(
                initial={
                    'title': post.title,
                    'body': post.body,
                    'cover': post.cover,
                    'short_description': post.short_description,
                    'category': post.category,
                })

        context = {
            'authenticated_user': authenticated_user,
            'new_notifications': get_new_notifications(authenticated_user),
            'form': form,
            'post': post,
        }

        return render(request, 'post/edit.html', context)

    context = {
        'authenticated_user': authenticated_user,
        'new_notifications': get_new_notifications(authenticated_user),
    }

    return render(request, 'pages/forbidden.html', context)
예제 #12
0
def support(request):
    authenticated_user = get_authenticated_user(request)

    context = {
        'authenticated_user': authenticated_user,
        'new_notifications': get_new_notifications(authenticated_user),
    }

    # Show "supprot" page template to user
    return render(request, 'pages/support.html', context)
예제 #13
0
def page_not_found_view(request, exception=None):
    authenticated_user = get_authenticated_user(request)

    context = {
        'authenticated_user': authenticated_user,
        'new_notifications': get_new_notifications(authenticated_user),
    }

    # Show 'admin/404.html' to user
    return render(request, 'admin/404.html', context)
예제 #14
0
def wellcome(request):
    authenticated_user = get_authenticated_user(request)

    context = {
        'authenticated_user': authenticated_user,
        'new_notifications': get_new_notifications(authenticated_user),
    }

    # Show "wellcome" page template to user
    return render(request, 'pages/wellcome.html', context)
예제 #15
0
def terms(request):
    authenticated_user = get_authenticated_user(request)

    context = {
        'authenticated_user': authenticated_user,
        'new_notifications': get_new_notifications(authenticated_user),
    }

    # Show 'terms.html' to user
    return render(request, 'terms.html', context)
예제 #16
0
def edit(request, username, slug):
    authenticated_user = get_authenticated_user(request)
    person = get_object_or_404(Person, username=username)  # Get the Person
    post = get_object_or_404(Post, author=person, slug=slug)  # Get the Post

    # If registered user is post author
    if authenticated_user == post.author:
        if request.method == 'POST':
            form = PostForm(request.POST)  # Get form

            if form.is_valid():
                title = form.cleaned_data['titleInput']  # Read title
                body = form.cleaned_data['bodyInput']  # Read body
                cover = form.cleaned_data['coverInput']  # Read cover
                short_description = form.cleaned_data[
                    'shortDescriptionInput']  # Read short_description

                # Give new data to post
                post.title = title
                post.body = body
                post.cover = cover
                post.short_description = short_description
                post.save()  # Save it

                # Redirect user to 'person:post:detail' url
                return HttpResponseRedirect('/user/' + username + '/post/' +
                                            slug)

        # If request.method == 'GET'
        else:
            form = PostForm(
                initial={
                    'titleInput': post.title,
                    'bodyInput': post.body,
                    'coverInput': post.cover,
                    'shortDescriptionInput': post.short_description,
                })

        context = {
            'authenticated_user': authenticated_user,
            'new_notifications': get_new_notifications(authenticated_user),
            'form': form,
            'post': post,
        }

        # Show 'user/post/edit.html' to user
        return render(request, 'user/post/edit.html', context)

    context = {
        'authenticated_user': authenticated_user,
        'new_notifications': get_new_notifications(authenticated_user),
    }

    return render(request, 'forbidden.html', context)
예제 #17
0
def rezome_detail(request, username):
    authenticated_user = get_authenticated_user(request)
    person = Person.objects.get(username=username)

    context = {
        'authenticated_user': authenticated_user,
        'new_notifications': get_new_notifications(authenticated_user),
        'person': person,
    }

    return render(request, 'person/rezome/detail.html', context)
예제 #18
0
def friends(request, page):
    authenticated_user = get_authenticated_user(request)

    paginate = Paginator(authenticated_user.following.all(), 3)

    context = {
        'authenticated_user': authenticated_user,
        'new_notifications': get_new_notifications(authenticated_user),
        'persons': paginate.page(page),
        'current_url': str('/user/friends/%3Fpage=1/').replace('/', '%2F'),
    }

    return render(request, 'person/friends/list.html', context)
예제 #19
0
def all_persons(request, page):
    authenticated_user = get_authenticated_user(request)
    persons = Person.objects.all()  # Get all persons

    paginate = Paginator(persons, 3)

    context = {
        'authenticated_user': authenticated_user,
        'new_notifications': get_new_notifications(authenticated_user),
        'persons': paginate.page(page),
        'current_url': str('/user/all/%3Fpage=1/').replace('/', '%2F'),
    }

    return render(request, 'person/list.html', context)
예제 #20
0
def all_notifications(request, page):
    authenticated_user = get_authenticated_user(request)
    notifications = Notification.objects.filter(
        givver=authenticated_user).order_by('-id')

    paginate = Paginator(notifications, 3)

    context = {
        'authenticated_user': authenticated_user,
        'new_notifications': get_new_notifications(authenticated_user),
        'notifications': paginate.page(page),
    }

    return render(request, 'person/notifications/list.html', context)
예제 #21
0
def users(request, page):
    authenticated_user = get_authenticated_user(request)

    # Load all posts order by there publish time
    users = Person.objects.all().order_by('-join_time')
    paginate = Paginator(users, 5)  # Paginate by 5

    context = {
        'authenticated_user': authenticated_user,
        'new_notifications': get_new_notifications(authenticated_user),
        'users': paginate.page(page),
    }

    # Show 'users.html' to user
    return render(request, 'users.html', context)
예제 #22
0
def all_posts(request, username, page):
    authenticated_user = get_authenticated_user(request)
    person = Person.objects.get(username=username)  # Get the Person
    posts = Post.objects.filter(author=person).order_by(
        '-publish_time')  # Get the Posts

    paginate = Paginator(posts, 3)

    context = {
        'authenticated_user': authenticated_user,
        'new_notifications': get_new_notifications(authenticated_user),
        'posts': paginate.page(page),
        'person': person,
    }

    return render(request, 'person/profile/detail.html', context)
예제 #23
0
def new_notifications(request):
    authenticated_user = get_authenticated_user(request)
    notifications = Notification.objects.filter(givver=authenticated_user,
                                                done=False).order_by('-id')

    for notification in notifications:
        notification.done = True
        notification.save()

    context = {
        'authenticated_user': authenticated_user,
        'new_notifications': get_new_notifications(authenticated_user),
        'notifications': notifications,
    }

    return render(request, 'person/notifications/new.html', context)
예제 #24
0
def cloud_index(request):
    authenticated_user = get_authenticated_user(request)
    cloud = Cloud.objects.get(owner=authenticated_user)

    # Get user's cloud uploaded files
    files = File.objects.filter(cloud=cloud).order_by('-id')

    context = {
        'authenticated_user': authenticated_user,
        'new_notifications': get_new_notifications(authenticated_user),
        'cloud': cloud,
        'files': files,
    }

    # Show "cloud index" page template to user
    return render(request, 'cloud/cloud_index.html', context)
예제 #25
0
def create(request):
    authenticated_user = get_authenticated_user(request)

    # If form method == POST
    if request.method == 'POST':
        form = PostForm(request.POST)  # Get form

        if form.is_valid():
            title = form.cleaned_data['titleInput']  # Read title
            body = form.cleaned_data['bodyInput']  # Read body
            cover = form.cleaned_data['coverInput']  # Read cover
            short_description = form.cleaned_data[
                'shortDescriptionInput']  # Read short description

            # Create a Post model with form data
            post = Post(
                title=title,
                body=body,
                author=authenticated_user,
                cover=cover,
                short_description=short_description,
            )
            post.save()  # Save it

            post.slug = post_slug_generator(post)
            post.save()

            # Redirect user to 'person:post:detail' url
            return HttpResponseRedirect('/user/' + post.author.username +
                                        '/post/' + post.slug)

    # If form method == GET
    else:
        form = PostForm()  # Give form to user

    context = {
        'authenticated_user': authenticated_user,
        'new_notifications': get_new_notifications(authenticated_user),
        'form': form,
    }

    # Show 'create/post.html' to user
    return render(request, 'create/post.html', context)
예제 #26
0
def friends_posts(request, page):
    authenticated_user = get_authenticated_user(request)

    posts = []
    all_posts = Post.objects.all().order_by('-publish_time')
    for post in all_posts:
        if post.author in authenticated_user.following.all():
            post.body = post.body.replace('<br/>', ' ')
            posts.append(post)

    paginate = Paginator(posts, 3)

    context = {
        'authenticated_user': authenticated_user,
        'new_notifications': get_new_notifications(authenticated_user),
        'posts': paginate.page(page),
    }

    return render(request, 'person/friends/posts.html', context)
예제 #27
0
def followings(request, username, page):
    authenticated_user = get_authenticated_user(request)
    person = Person.objects.get(username=username)

    following_available = False
    if len(person.following.all()) > 0:
        following_available = True

    paginate = Paginator(person.following.all(), 3)

    context = {
        'authenticated_user': authenticated_user,
        'new_notifications': get_new_notifications(authenticated_user),
        'persons': paginate.page(page),
        'person': person,
        'current_url': str('/user/friends/%3Fpage=1/').replace('/', '%2F'),
        'following_available': following_available,
    }

    return render(request, 'person/followings.html', context)
예제 #28
0
def delete_post_comment(request, username, post_id, comment_id):
    authenticated_user = get_authenticated_user(request)
    comment = get_object_or_404(Comment, id=comment_id)  # Get the Comment
    post = get_object_or_404(Post, id=post_id)  # Get the Post

    # If registered user is comment author or post author
    if comment.author.username == authenticated_user or post.author.username == authenticated_user:
        comment.delete()  # Delete it

        post.comments = int(post.comments) - 1
        post.save()

        return HttpResponseRedirect('/user/' + username + '/post/' +
                                    str(post.id))

    context = {
        'authenticated_user': authenticated_user,
        'new_notifications': get_new_notifications(authenticated_user),
    }

    return render(request, 'pages/forbidden.html', context)
예제 #29
0
def delete_post(request, username, post_id):
    authenticated_user = get_authenticated_user(request)
    person = Person.objects.get(username=username)
    post = get_object_or_404(Post, author=person, id=post_id)  # Get the Post

    # If registered user is post author
    if post.author.username == request.user.username:
        # Delete post comments
        for comment in Comment.objects.filter(place=post):
            comment.delete()

        post.delete()  # Delete it

        return HttpResponseRedirect('/' + 'user/' + username)

    # If registered user not post author
    context = {
        'authenticated_user': authenticated_user,
        'new_notifications': get_new_notifications(authenticated_user),
    }

    return render(request, 'pages/forbidden.html', context)
예제 #30
0
def profile_detail(request, username):
    authenticated_user = get_authenticated_user(request)
    person = get_object_or_404(Person, username=username)  # Get Person
    posts = Post.objects.filter(author=person).order_by(
        '-publish_time')  # Get the Posts

    paginate = Paginator(posts, 3)

    skills_availability = False
    if len(person.skills.all()) > 0:
        skills_availability = True

    context = {
        'authenticated_user': authenticated_user,
        'new_notifications': get_new_notifications(authenticated_user),
        'person': person,
        'posts': paginate.page(1),
        'skills_availability': skills_availability,
        'current_url': str(request.path).replace('/', '%2F'),
    }

    return render(request, 'person/profile/detail.html', context)