def index(request): tweets = None users = None notifications = None if request.method == 'POST': form = TweetForm(request.POST) if form.is_valid(): tweet = form.save(commit=False) tweet.created_by = TwitterUser.objects.get(id=request.user.id) tweet.save() notify(tweet) try: following = list(request.user.following.all()) users = TwitterUser.objects.all() following.append(request.user) notifications = Notification.objects.filter(recipient=request.user) tweets = Tweet.objects \ .filter(created_by__in=following) \ .order_by('-created_at') except Exception: pass return render(request, 'index/index.html', { 'form': TweetForm(), 'tweets': tweets, 'users': users, 'notifications': notifications })
def profiles(request, username): if request.user.is_authenticated: user = User.objects.get(username=username) if request.method == "POST": form = TweetForm(data=request.POST) if form.is_valid() tweet = form.save(commit=False) tweet.user = request.user tweet.save() redirecturl = request.POST.get("redirect", "/") return redirect(redirecturl) else: form = TweetForm() return render(request, "profile.html", {"user": user, "form": form}) else: return redirect("/") def follows(request, username): user = User.objects.get(username=username) tweeterprofiles = user.tweeterprofile.follows return render(request, "users.html", {"title": "Follows", "tweeterprofiles": tweeterprofiles}) def followers(request, username): user = User.objects.get(username=username) tweeterprofiles = user.tweeterprofile.followed_by return render(request, "user.html", {"title": "Followers", "tweeterprofiles": tweeterprofiles})
def profile(request, username): if request.user.is_authenticated: user = User.objects.get(username=username) if request.method == 'POST': if 'tweetform' in request.POST: form = TweetForm(data=request.POST) infoform = PersonalInfoForm() if form.is_valid(): tweet = form.save(commit=False) tweet.user = request.user tweet.save() redirecturl = request.POST.get('redirect', '/') return redirect(redirecturl) else: infoform = PersonalInfoForm(request.POST, request.FILES) form = TweetForm() if infoform.is_valid(): profile = request.user.twitterprofile if infoform.cleaned_data.get('profile_picture'): photo = infoform.cleaned_data.get('profile_picture') x = infoform.cleaned_data.get('p_x') y = infoform.cleaned_data.get('p_y') w = infoform.cleaned_data.get('p_width') h = infoform.cleaned_data.get('p_height') profile.crop_profile_picture(photo, x, y, w, h) if infoform.cleaned_data.get('banner_picture'): photo = infoform.cleaned_data.get('banner_picture') x = infoform.cleaned_data.get('b_x') y = infoform.cleaned_data.get('b_y') w = infoform.cleaned_data.get('b_width') h = infoform.cleaned_data.get('b_height') profile.crop_banner_picture(photo, x, y, w, h) profile.username = infoform.cleaned_data.get('username') profile.biography = infoform.cleaned_data.get('biography') profile.save() redirecturl = request.POST.get('redirect', '/') return redirect(redirecturl) else: form = TweetForm() infoform = PersonalInfoForm() return render(request, 'profile.html', { 'form': form, 'infoform': infoform, 'user': user }) else: return redirect('/')
def post(self, request): form = TweetForm(request.POST) if form.is_valid(): tweet = form.save(commit=False) tweet.user = request.user tweet.save() messages.success(request, "Added") return redirect('homepage') messages.warning(request, "Error") return redirect("homepage")
def post_tweet(request, message=None, *args, **kwargs): """ post tweet and re-direct user to the main page """ tweet_content = request.POST['content'] tweet_form = TweetForm({'content': tweet_content}) if tweet_form.is_valid(): if is_dirty(content=tweet_content): messages.add_message(request, messages.ERROR, 'Shame on you') #save tweet tweet = tweet_form.save(commit=False) tweet.created_by = request.user tweet.save() else: brand_corrected = brand_correct_tweet(content=request.POST['content']) brand_corrected_tweet = brand_corrected[0] brand_corrected_flag = brand_corrected[1] if brand_corrected_flag: request.session.__setitem__('brand_corrected_tweet', brand_corrected_tweet) request.session.__setitem__('content', request.POST['content']) return HttpResponseRedirect(reverse('index')) else: # post tweet to twitter post_status = post_to_twitter(content=request.POST['content']) if post_status['success']: messages.add_message(request, messages.SUCCESS, post_status['message']) # save tweet tweet = tweet_form.save(commit=False) tweet.created_by = request.user tweet.tweet_id = post_status['id'] if not is_dirty(content=tweet_content): tweet.is_dirty = True tweet.save() else: messages.add_message(request, messages.ERROR, post_status['message']) return HttpResponseRedirect(reverse('index'))
def create(request): if request.POST: form = TweetForm(request.POST) if form.is_valid(): a = form.save() messages.add_message(request , messages.SUCCESS , 'Your tweet was added') return HttpResponseRedirect('/tweets/all') else: form = TweetForm() args = {} args.update(csrf(request)) args['form'] = form return render_to_response('create_tweet.html', args)
def create(request): if request.POST: form = TweetForm(request.POST) if form.is_valid(): a = form.save() messages.add_message(request, messages.SUCCESS, 'Your tweet was added') return HttpResponseRedirect('/tweets/all') else: form = TweetForm() args = {} args.update(csrf(request)) args['form'] = form return render_to_response('create_tweet.html', args)
def homeview(request): if request.method == 'POST': tweet_form = TweetForm(request.POST, request.FILES) if tweet_form.is_valid(): t = tweet_form.save(commit=False) t.user = request.user t.save() hashtagArray = re.findall(r'#(\w+)', t.tweet) for ht in hashtagArray: t.tags.add(ht) t.save() elif 'retweet' in request.POST: id = request.POST['retweet'] rt = Tweet.objects.get(id=id) retweet = Retweet(user=request.user, owner=rt.user, tweet=rt.tweet, image=rt.image, tags=rt.tags, date=rt.date) retweet.save() else: tweet_form = TweetForm() tweets = Tweet.objects.filter(user=request.user) tags = Tag.objects.all() friend, created = Friend.objects.get_or_create(owner=request.user) users = friend.users.all() friends_tweets = [] for fr in users: tweets = tweets | Tweet.objects.filter(user=fr) profile = UserProfile.objects.get(user=request.user) users = User.objects.exclude(username=request.user) retweets = Retweet.objects.filter(user=request.user) return render( request, 'tweet/homeview.html', { 'username': request.user, 'tweets': tweets, 'retweets': retweets, 'form': tweet_form, 'tags': tags, 'profile': profile, 'users': users })
def profile(request, username): if request.user.is_authenticated: user = User.objects.get(username=username) if request.method == 'POST': form = TweetForm(data=request.POST) if form.is_valid(): tweet = form.save(commit=False) tweet.user = request.user tweet.save() redirecturl = request.POST.get('redirect', '/') return redirect(redirecturl) else: form = TweetForm() return render(request, 'profile.html', {'form': form, 'user': user}) else: return redirect('/')
def compose(request): html = 'compose_tweet_form.html' if request.method == 'POST': form = TweetForm(request.POST) if form.is_valid(): tweet = form.save(commit=False) tweet.tweet_author = MyUser.objects.get(id=request.user.id) tweet.save() notify_user(tweet) return HttpResponseRedirect(reverse('homepage')) notifications = Notification.objects.filter(receiving_user=request.user) form = TweetForm() return render(request, html, { 'form': form, 'notifications': notifications })
def compose(request): tweet_count = 0 user = None follow = False unread_count = 0 user = get_object_or_404(TwitterUser, pk=request.user.id) followings = FollowModel.objects.filter(follower=user) followers = FollowModel.objects.filter(followed=user) followings_count = followings.count() followers_count = followers.count() try: unread_notifications = Notification.objects.filter( receiver=user, read=False) unread_count = unread_notifications.count() tweet_count = Tweet.objects.filter(user=user).count() except ObjectDoesNotExist: tweet_count = 0 form = TweetForm() if request.method == 'POST': form = TweetForm(request.POST) if form.is_valid(): tweet = form.save(commit=False) tweet.user = request.user tweet.save() mentions = tweet.parse_mentions() print(mentions) if mentions: for mention in mentions: notification_instance = Notification( sender=request.user, receiver=mention, related=tweet) notification_instance.save() return redirect('home') return render(request, 'compose.html', {'form': form, 'tweet_count': tweet_count, 'user': user, 'follow': follow, 'followings_count': followings_count, 'followers_count': followers_count, 'unread_count': unread_count})
def reply(request, user, tweet): if request.user.is_authenticated: if request.method == 'POST': form = TweetForm(data=request.POST) if form.is_valid(): parent_id = form.cleaned_data.get('parent_id') tweet_id = form.cleaned_data.get('tweet_id') parent = Tweet.objects.get(user_id=parent_id, id=tweet_id) tweet = form.save(commit=False) tweet.parent = parent tweet.user = request.user tweet.body = form.cleaned_data.get('body') tweet.save() # redirecturl = request.POST.get('redirect', '/') return redirect('/' + tweet.user.username + '/status/' + str(tweet.id)) else: tweet = Tweet.objects.get(user_id=user, id=tweet) ctx = { 'tweet_screenname': tweet.user.twitterprofile.username, 'tweet_username': tweet.user.username, 'tweet_profilepicture': tweet.user.twitterprofile.profile_picture.url, 'tweet_date': time_difference(tweet.created_at), 'tweet_text': tweet.body, 'tweet_id': tweet.id, 'user_id': user } return HttpResponse(json.dumps(ctx), content_type='application/json') else: return redirect('/')