def edit_profile(request, username): """ This function renders Edit Profile Page. """ native_user = get_object_or_404(User, username=username) profile = get_object_or_404(UserProfile, user=native_user) # All tags and subscribed tags tags = Tags.objects.all() tags_subscribed = profile.subscribed_tags.all() # Forms avatar_form = AvatarUploadForm() form = UserSignupForm() password_change_form = PasswordChangeForm(user=native_user) tag_form = TagForm() context = { 'form': form, 'profile': profile, 'avatar_form': avatar_form, 'native_user': native_user, 'password_change_form': password_change_form, 'tag_form': tag_form, 'tags': tags, 'tags_subscribed': tags_subscribed, } # A check so that other users cannot visit edit page of native user. if request.user == native_user: return render(request, 'user_profile/edit_profile.html', context) messages.info(request, f"You are not authorised to visit this page.") return HttpResponseRedirect( reverse('User Profile', kwargs={'username': request.user}))
def index(request, tag_filter=None, username=None, liked=None, older=None): """ This function renders Home Page. If tag_filter = None, All posts are fetched otherwise posts from specific tags are fetched. """ check_profile = None flag = False if request.user.is_authenticated: flag = set_profile(request, request.user) check_profile = UserProfile.objects.get(user=request.user) # User signup form, comment form, Post adding form form = UserSignupForm() comment_form = CommentForm() addpostform = PostForm() # Fetching posts as pages, all User Profiles, tags and comments posts = page_maker(request, Post, tag_filter=tag_filter, username=username, liked=liked, older=older) user_profiles = UserProfile.objects.all() tags = Tags.objects.all() comments = Comment.objects.all() # Using overridden Model Manager all(). # Context to be sent to template context = { 'form': form, 'addpostform': addpostform, 'posts': posts, 'tags': tags, 'user_profiles': user_profiles, 'comments': comments, 'comment_form': comment_form, } if check_profile is not None: if not check_profile.is_profile_set: # messages.info(request, f"Set your profile first.") return HttpResponseRedirect( reverse('edit_profile', kwargs={'username': request.user.username})) if username is not None: messages.info(request, f"You are viewing Personalised Feed.") else: messages.info(request, f"You are viewing Public Feed.") return render(request, 'home/index.html', context)
def change_password(request, username): """ This function changes password of a user . It ask for current(old) password. It also keeps user logged in after successful password change. """ native_user = get_object_or_404(User, username=username) if request.user == native_user: if request.method == 'POST': password_change_form = PasswordChangeForm(native_user, request.POST) if password_change_form.is_valid(): user = password_change_form.save() update_session_auth_hash( request, user) # Important! To keep User Logged in. messages.success(request, 'Your password was successfully updated!') return HttpResponseRedirect( reverse('edit_profile', kwargs={'username': username})) else: messages.error(request, f'Something went wrong, try again!') return HttpResponseRedirect( reverse('edit_profile', kwargs={'username': username})) else: password_change_form = PasswordChangeForm(native_user) avatar_form = AvatarUploadForm() form = UserSignupForm() addpostform = PostForm() comments = Comment.objects.all() comment_form = CommentForm() user_profiles = UserProfile.objects.all() context = { 'password_change_form': password_change_form, 'addpostform': addpostform, 'avatar_form': avatar_form, 'form': form, 'comments': comments, 'comment_form': comment_form, 'user_profiles': user_profiles, } return render(request, 'user_profile/edit_profile.html', context) messages.info(request, f"You are not authorised to visit this page.") return HttpResponseRedirect( reverse('User Profile', kwargs={'username': request.user}))
def user_profile(request, username, tag_name=None, liked=None, older=None): """ This functions renders User Profile Page """ service = get_calendar_service(request) # Call the Calendar API # print('Getting list of calendars') timeZone = None calendar_id = None if service: cal_service_found = True calendars_result = service.calendarList().list().execute() calendars = calendars_result.get('items', []) if not calendars: print('No calendars found.') for calendar in calendars: # print(calendar.get('primary'))# prints True only for the primary Calendar of User if calendar.get('primary'): summary = calendar['summary'] calendar_id = calendar['id'] timeZone = calendar['timeZone'] primary = "Primary" if calendar.get('primary') else "" # print(timeZone) # prints Asia/Kolkata print("%s\t%s\t%s" % (summary, calendar_id, primary)) # User whose profile is open native_user = get_object_or_404(User, username=username) profile = get_object_or_404(UserProfile, user=native_user) check_profile = None flag = False if request.user.is_authenticated: check_profile = get_object_or_404(UserProfile, user=request.user) if profile is check_profile: flag = set_profile(request, native_user) check_profile = get_object_or_404(UserProfile, user=request.user) # Users current user is following followed_users = check_profile.followed_users.all() # Users who are following current user followers = check_profile.followers.all() # Posts and avatar of that user native_posts = page_maker(request, Post, native_user, tag_filter=tag_name, liked=liked, older=older) # drafts = page_maker(request, Post, native_user, draft=True) # This also contains scheduled but NOT yet approved event related post drafts = Post.objects.filter(author=native_user).filter(draft=True) # Scheduled event related post which have not been given permission yet. scheduled_posts = Post.objects.filter(author=native_user).filter( draft=True).filter(is_scheduled=True) # Fetching all User profiles, comments, tags, pending posts and # pending posts of native user user_profiles = UserProfile.objects.all() comments = Comment.objects.all() tags = Tags.objects.all() # Filtering All pending posts and pending post of native user which are NOT drafts. pending_posts = Post.objects.filter(verify_status=-1).filter(draft=False) native_pending_posts = pending_posts.filter(author=native_user).filter( draft=False) # Comment form, user signup form, post adding form and password change form comment_form = CommentForm() form = UserSignupForm() addpostform = PostForm() password_change_form = PasswordChangeForm(user=native_user) avatar_form = AvatarUploadForm() read_notif = None unread_notif = None if request.user.is_authenticated: # Read notifications read_notif = request.user.notifications.read() # Unread notifications unread_notif = request.user.notifications.unread() quiz_results = UserQuizResult.objects.all().filter(user=request.user) context = { 'profile': profile, 'avatar_form': avatar_form, 'form': form, 'native_user': native_user, 'native_posts': native_posts, 'addpostform': addpostform, 'password_change_form': password_change_form, 'comments': comments, 'comment_form': comment_form, 'user_profiles': user_profiles, 'tags': tags, 'pending_posts': pending_posts, 'native_pending_posts': native_pending_posts, 'read_notif': read_notif, 'unread_notif': unread_notif, 'drafts': drafts, 'quiz_results': quiz_results, 'scheduled_posts': scheduled_posts, 'followed_users': followed_users, 'followers': followers, } if check_profile is not None: if not profile.is_profile_set: messages.info(request, f"User profile not set") return HttpResponseRedirect(reverse('Index')) if 'cal_service_found' in locals(): new_to_context = { 'cal_service_found': 1, 'calendar_id': calendar_id, 'timeZone': timeZone } context.update(new_to_context) return render(request, 'user_profile/user_profile.html', context)
def avatar_upload(request, username): """ This function uploads/re-uploads profile picture of a user. """ # <<<<<<< HEAD # native_user = get_object_or_404(User, username=username) # if request.user == native_user: # if request.method == 'POST': # avatar_form = AvatarUploadForm(request.POST, request.FILES) # if avatar_form.is_valid(): # user_prof = UserProfile.objects.get(user=native_user) # img = avatar_form.cleaned_data['avatar'] # user_prof.avatar = img # user_prof.save() # messages.success(request, f"Avatar uploaded successfully!") # return HttpResponseRedirect(reverse("edit_profile", kwargs={'username': username})) # else: # avatar_form = AvatarUploadForm() # form = UserSignupForm() # password_change_form = PasswordChangeForm(request.user) # addpostform = PostForm() # comments = Comment.objects.all() # comment_form = CommentForm() # user_profiles = UserProfile.objects.all() # context = { # 'password_change_form': password_change_form, # 'addpostform': addpostform, # 'avatar_form': avatar_form, # 'form': form, # 'comments': comments, # 'comment_form': comment_form, # 'user_profiles': user_profiles, # } # return render(request, 'user_profile/edit_profile.html', context) # messages.info(request, f"You are not authorised to visit this page.") # return HttpResponseRedirect(reverse('User Profile', kwargs={'username': request.user})) # ======= if request.method == 'POST': avatar_form = AvatarUploadForm(request.POST, request.FILES) if avatar_form.is_valid(): user = User.objects.get(username=username) user_prof = UserProfile.objects.get(user=user) if clean_file(request, avatar_form): img = avatar_form.cleaned_data['avatar'] else: return HttpResponseRedirect( reverse("User Profile", kwargs={'username': request.user})) user_prof.avatar = img user_prof.save() messages.success(request, f"Avatar uploaded successfully!") return HttpResponseRedirect( reverse("edit_profile", kwargs={'username': username})) else: return HttpResponse("Please upload an Image File only...") else: avatar_form = AvatarUploadForm() form = UserSignupForm() password_change_form = PasswordChangeForm(request.user) addpostform = PostForm() comments = Comment.objects.all() comment_form = CommentForm() user_profiles = UserProfile.objects.all() context = { 'password_change_form': password_change_form, 'addpostform': addpostform, 'avatar_form': avatar_form, 'form': form, 'comments': comments, 'comment_form': comment_form, 'user_profiles': user_profiles, } return render(request, 'user_profile/edit_profile.html', context)
def user_profile(request, username, tag_name=None, liked=None, older=None): """ This functions renders User Profile Page """ native_user = get_object_or_404(User, username=username) profile = get_object_or_404(UserProfile, user=native_user) check_profile = None flag = False # Users current user is following followed_users = None # Users who are following current user followers = None if request.user.is_authenticated: check_profile = get_object_or_404(UserProfile, user=request.user) # Users current user is following followed_users = check_profile.followed_users.all() # Users who are following current user followers = check_profile.followers.all() if profile is check_profile: flag = set_profile(request, native_user) check_profile = get_object_or_404(UserProfile, user=request.user) # Posts and avatar of that user native_posts = page_maker(request, Post, native_user, tag_filter=tag_name, liked=liked, older=older) # drafts = page_maker(request, Post, native_user, draft=True) # This also contains scheduled but NOT yet approved event related post drafts = Post.objects.filter(author=native_user).filter(draft=True) admin = User.objects.get(username='******') # Scheduled event related post which have not been given permission yet. scheduled_posts = Post.objects.filter(author=admin).filter( draft=True).filter(is_scheduled=True) # Fetching all User profiles, comments, tags, pending posts and # pending posts of native user user_profiles = UserProfile.objects.all() comments = Comment.objects.all() tags = Tags.objects.all() # Filtering All pending posts and pending post of native user which are NOT drafts. pending_posts = Post.objects.filter(verify_status=-1).filter(draft=False) native_pending_posts = pending_posts.filter(author=native_user).filter( draft=False) # Comment form, user signup form, post adding form and password change form comment_form = CommentForm() form = UserSignupForm() addpostform = PostForm() password_change_form = PasswordChangeForm(user=native_user) avatar_form = AvatarUploadForm() read_notif = None unread_notif = None quiz_results = None if request.user.is_authenticated: # Read notifications # read_notif = request.user.notifications.read() read_notif = Notification.objects.filter( recipient=request.user).filter(unread=False) # Unread notifications # unread_notif = request.user.notifications.unread() unread_notif = Notification.objects.filter( recipient=request.user).filter(unread=True) quiz_results = UserQuizResult.objects.all().filter(user=request.user) context = { 'profile': profile, 'avatar_form': avatar_form, 'form': form, 'native_user': native_user, 'native_posts': native_posts, 'addpostform': addpostform, 'password_change_form': password_change_form, 'comments': comments, 'comment_form': comment_form, 'user_profiles': user_profiles, 'tags': tags, 'pending_posts': pending_posts, 'native_pending_posts': native_pending_posts, 'read_notif': read_notif, 'unread_notif': unread_notif, 'drafts': drafts, 'quiz_results': quiz_results, 'scheduled_posts': scheduled_posts, 'followed_users': followed_users, 'followers': followers, } if check_profile is not None: if not profile.is_profile_set: messages.info(request, f"User profile not set") return HttpResponseRedirect(reverse('Index')) return render(request, 'user_profile/user_profile.html', context)
def post_detail(request, slug): """ This function renders Post Detail View. """ # check_profile = None # flag = False # # if request.user.is_authenticated: # check_profile = get_object_or_404(UserProfile, user=request.user) # flag = set_profile(request, request.user) # check_profile = get_object_or_404(UserProfile, user=request.user) # # if check_profile is not None: # if not check_profile.is_profile_set: # messages.info(request, f"User profile not set") # return HttpResponseRedirect(reverse('Index')) # Fetching post(using slug), post author, user profile of author # all user profiles, all comments and all tags. post_qs = Post.objects.filter(slug=slug) if post_qs: post = post_qs.first() else: messages.info(request, f"This post does not exist.") return HttpResponseRedirect(reverse('User Profile', kwargs={'username': request.user.username})) author = post.author author_profile = get_object_or_404(UserProfile, user=author) user_profiles = UserProfile.objects.all() comments = Comment.objects.all() tags = Tags.objects.all() # Comment adding form and user signup form comment_form = CommentForm() form = UserSignupForm() context = { 'post': post, 'author': author, 'author_profile': author_profile, 'comments': comments, 'comment_form': comment_form, 'user_profiles': user_profiles, 'tags': tags, 'form': form, } faculties = User.objects.filter(groups__name='Teacher') # print("Iwas out of everything") if post.draft: # print("I was in drafts.") if post.is_scheduled: # print("Iwas in is scheduled") # If post is scheduled and draft, only admin and teachers can see it if (request.user in faculties) or (request.user.username == 'admin'): return render(request, 'post/post_detail.html', context) else: messages.info(request, f"Sorry, You can't see this yet.") return HttpResponseRedirect(reverse('Index')) elif post.author != request.user: # If someone tries to access Drafts of others through URL. messages.info(request, f"Sorry, You can't see this yet.") return HttpResponseRedirect(reverse('Index')) return render(request, 'post/post_detail.html', context)
def add_comment(request, post_id): """ Function to add comments. Currently NOT IN USE. """ post = Post.objects.get(id=post_id) if request.method == 'POST': comment_form = CommentForm(request.POST) if comment_form.is_valid(): # Content of comment content_data = comment_form.cleaned_data.get('comment_text') parent_obj = None try: # If parent exists parent_id = int(request.POST['parent_id']) except: # If parent doesn't exist parent_id = None # Check if parent_id exists if parent_id: parent_qs = Comment.objects.filter(id=parent_id) # Check if parent_qs is not NULL if parent_qs.exists() and parent_qs.count() == 1: parent_obj = parent_qs.first() # Create new comment new_comment, created = Comment.objects.get_or_create( user=request.user, post=post, comment_text=content_data, parent=parent_obj, ) if created: # Send success message messages.success(request, f'Comment posted!') if parent_obj is None: return redirect(HOME + '#comment-' + str(post.pk) + '-' + str(new_comment.pk)) else: return redirect(HOME + '#comment-' + str(post.pk) + '-' + str(parent_obj.pk)) # return HttpResponseRedirect(new_comment.comment_text.get_absolute_url()) else: messages.error(request, f"Not created!") return redirect(HOME) # else: # messages.error(request, f"Please write some comment!") # return redirect(HOME) else: comment_form = CommentForm() form = UserSignupForm() add_post_form = PostForm() posts = post_view.page_maker(request, Post) user_profiles = UserProfile.objects.all() tags = Tags.objects.all() comments = Comment.objects.all() context = { 'comment_form': comment_form, 'form': form, 'addpostform': add_post_form, 'posts': posts, 'tags': tags, 'user_profiles': user_profiles, 'comments': comments, } return render(request, 'home/index.html', context)