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)
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)
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)
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)
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)
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('<', '<').replace('>', '>').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( '<', '<').replace('>', '>') }) context = { 'authenticated_user': authenticated_user, 'new_notifications': get_new_notifications(authenticated_user), 'form': form, } return render(request, 'person/rezome/edit.html', context)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
def new_post(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['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 # Create a Post model with form data post = Post(title=title, body=body, author=authenticated_user, cover=cover, short_description=short_description, category=category) post.save() # Save it return HttpResponseRedirect('/user/' + post.author.username + '/post/' + str(post.id)) # 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, } return render(request, 'post/write.html', context)
def delete_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: delete_comment_completely(comment) post.len_comments = int(post.len_comments) - 1 post.save() # 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)
def signup(request): authenticated_user = get_authenticated_user(request) if authenticated_user is not None: return HttpResponseRedirect('/') # If form method == POST if request.method == 'POST': form = SignUpForm(request.POST) # Get Form # If form valid if form.is_valid(): username = form.cleaned_data['username'] # Read username # Create a user with form data user = form.save(commit=False) user.backend = 'django.contrib.auth.backends.ModelBackend' user.save() # Login current user login(request, user) # Redirect user to "wellcome" page return HttpResponseRedirect('/wellcome/') # If form method == GET else: form = SignUpForm() # Give form to user context = { 'authenticated_user': authenticated_user, 'new_notifications': get_new_notifications(authenticated_user), 'form': form, } # Show "signup" page template to user return render(request, 'registration/signup.html', context)
def delete(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: # Delete post comments for comment in post.comments.all(): delete_comment_completely(comment) post.delete() # Delete post # Redirect user to 'person:profile' url return HttpResponseRedirect('/user/' + username) # If registered user not post author context = { 'authenticated_user': authenticated_user, 'new_notifications': get_new_notifications(authenticated_user), } # Show 'forbidden.html' to user return render(request, 'forbidden.html', context)
def post_detail(request, username, post_id): authenticated_user = get_authenticated_user(request) person = Person.objects.get(username=username) # Get the Person post = get_object_or_404(Post, author=person, id=post_id) # Get the Post comments = Comment.objects.filter(place=post).order_by( 'id') # Get the Comments len_comments = len(comments) # Get length of comments post.views = int(post.views) + 1 post.save() if authenticated_user is not None: authenticated_user.viewed_posts.add(post) authenticated_user.save() # For comment # If form method == POST if request.method == 'POST': form = CommentForm(request.POST) # Get form if form.is_valid(): mode = form.cleaned_data['mode'] # Read mode text = form.cleaned_data['text'] # Read body text = text.replace('<', '<').replace('>', '>').replace( '\n', '<br/>') # Clean the body and recognize line breaks # If mode == comment if mode == 'comment': # Create a Comment model with form data comment = Comment(place=post, author=authenticated_user, text=text) comment.save() # Save it post.comments = int(post.comments) + 1 post.save() notif = Notification( givver=post.author, message= f'<a href="/user/{authenticated_user.username}/">{authenticated_user.name}</a> نظری روی مطلب «<a href="/user/{post.author.username}/post/{post.id}/">{post.title}</a>» شما ارسال کرد', notif_type='comment') notif.save() else: replay = Comment(author=authenticated_user, text=text) replay.save() comment = Comment.objects.get(id=mode) comment.replays.add(replay) comment.save() post.comments = int(post.comments) + 1 post.save() notif = Notification( givver=comment.author, message= f'<a href="/user/{authenticated_user.username}/">{authenticated_user.name}</a> پاسخی به <a href="/user/{post.author.username}/post/{post.id}/#comments">نظر</a> شما داد', notif_type='replay') notif.save() notif2 = Notification( givver=post.author, message= f'<a href="/user/{authenticated_user.username}/">{authenticated_user.name}</a> نظری روی مطلب «<a href="/user/{post.author.username}/post/{post.id}/">{post.title}</a>» شما ارسال کرد', notif_type='comment') notif2.save() return HttpResponseRedirect('/user/' + post.author.username + '/post/' + str(post.id)) # If form method == GET else: form = CommentForm() # Give form to user context = { 'authenticated_user': authenticated_user, 'new_notifications': get_new_notifications(authenticated_user), 'post': post, 'post_body': translate_to_html(post.body), 'comments': comments, 'len_comments': len_comments, 'form': form, 'ads_list': ads_list(), 'current_url': str(request.path).replace('/', '%2F'), } return render(request, 'post/detail.html', context)