def dispatch(self, request, *args, **kwargs): if request.user.is_authenticated() and request.user.pk == int(kwargs['user_id']): self.user = request.user else: self.user = get_object_or_404(User, pk=kwargs['user_id']) self.wall_post_form = WallPostForm(request.POST or None) return super(UserProfileView, self).dispatch(request, *args, **kwargs)
def post(self, request, *args , **kwargs): #context = self.get_context_data(**kwargs) now = datetime.utcnow().replace(tzinfo=utc) if request.method == 'POST': self.form = WallPostForm(request.POST) if self.form.is_valid(): message = self.form.cleaned_data['message'] WallPost.objects.create(message=message, poster=request.user, receiver=User.objects.get(pk=self.get_object().id), pub_date=now) else: self.form = WallPostForm() return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
class UserProfileView(TemplateView): template_name = 'users/profile.html' def dispatch(self, request, *args, **kwargs): if request.user.is_authenticated() and request.user.pk == int(kwargs['user_id']): self.user = request.user else: self.user = get_object_or_404(User, pk=kwargs['user_id']) self.wall_post_form = WallPostForm(request.POST or None) return super(UserProfileView, self).dispatch(request, *args, **kwargs) def get_wall_posts(self): paginator = Paginator(self.user.wall_posts.select_related('author'), 10) page = self.request.GET.get('page') try: posts_on_wall = paginator.page(page) except PageNotAnInteger: posts_on_wall = paginator.page(1) except EmptyPage: posts_on_wall = paginator.page(paginator.num_pages) return posts_on_wall def get_context_data(self, **kwargs): context = super(UserProfileView, self).get_context_data(**kwargs) context['profile_user'] = self.user context['posts_on_wall'] = self.get_wall_posts() context['wall_post_form'] = self.wall_post_form if self.request.user != self.user: context['is_my_friend'] = User.friendship.are_friends(self.request.user, self.user) return context def post(self, request, *args, **kwargs): if self.wall_post_form.is_valid(): post = self.wall_post_form.save(commit=False) post.user = self.user post.author = request.user post.save() messages.success(request, _(u'Повідомлення успішно опубліковано')) return redirect(request.path) return self.get(request, *args, **kwargs)
class UserView(generic.DetailView): model = User template_name = 'users/user_profile.html' @method_decorator(login_required) def dispatch(self, *args, **kwargs): return super(UserView, self).dispatch(*args, **kwargs) # This method processes the post data. def post(self, request, *args , **kwargs): #context = self.get_context_data(**kwargs) now = datetime.utcnow().replace(tzinfo=utc) if request.method == 'POST': self.form = WallPostForm(request.POST) if self.form.is_valid(): message = self.form.cleaned_data['message'] WallPost.objects.create(message=message, poster=request.user, receiver=User.objects.get(pk=self.get_object().id), pub_date=now) else: self.form = WallPostForm() return HttpResponseRedirect(request.META.get('HTTP_REFERER')) def get_context_data(self, **kwargs): # Call base implementation to get a context context = super(UserView, self).get_context_data(**kwargs) context['all_wall_posts'] = WallPost.objects.filter(receiver=User.objects.get(pk=self.object.id)) # Generate a list of mutual friends. Think this is ineficient. self.mutual_friends = [] for f in self.object.friends.all(): if f in User.objects.get(pk=self.request.user.id).friends.all(): self.mutual_friends.append(f) context['friends'] = self.object.friends.all() context['mutual_friends'] = self.mutual_friends context['number_of_friends'] = len(self.object.friends.all()) context['this_user'] = self.object context['friend_request_exists'] = Request.objects.filter( requester=User.objects.get(pk=self.request.user.id), requestee=User.objects.get(pk=self.object.id)).exists() context['logged_in_user'] = self.request.user context['logged_in_as'] = self.request.user.get_full_name() self.form = WallPostForm() context['form'] = self.form return context
def get_context_data(self, **kwargs): # Call base implementation to get a context context = super(UserView, self).get_context_data(**kwargs) context['all_wall_posts'] = WallPost.objects.filter(receiver=User.objects.get(pk=self.object.id)) # Generate a list of mutual friends. Think this is ineficient. self.mutual_friends = [] for f in self.object.friends.all(): if f in User.objects.get(pk=self.request.user.id).friends.all(): self.mutual_friends.append(f) context['friends'] = self.object.friends.all() context['mutual_friends'] = self.mutual_friends context['number_of_friends'] = len(self.object.friends.all()) context['this_user'] = self.object context['friend_request_exists'] = Request.objects.filter( requester=User.objects.get(pk=self.request.user.id), requestee=User.objects.get(pk=self.object.id)).exists() context['logged_in_user'] = self.request.user context['logged_in_as'] = self.request.user.get_full_name() self.form = WallPostForm() context['form'] = self.form return context