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 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 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 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)