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 new_tweet_view(request): notification_count = notification_new_count(request) if request.method == "POST": form = TweetForm(request.POST) if form.is_valid(): data = form.cleaned_data new_tweet = Tweet.objects.create( body=data.get("body"), author=request.user, ) mentions = re.findall(r"@(\w+)", data.get("body")) if mentions: for mention in mentions: matched_user = TwitterUser.objects.get(username=mention) if matched_user: Notification.objects.create( n_receiver=matched_user, tracked_tweet=new_tweet, ) return HttpResponseRedirect(reverse("homepage")) form = TweetForm() return render( request, "new_tweet_view.html", { "form": form, "notification_count": notification_count, }, )
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 post_tweet_view(request): if request.method == 'POST': form = TweetForm(request.POST) if form.is_valid(): data = form.cleaned_data new_tweet = Tweet.objects.create(body=data.get('body'), time_posted=datetime.now(), tweeter=request.user) mentions = re.findall(r'@(\w+)', data.get('body')) if mentions: users = TwitterUser.objects.all() for mention in mentions: if TwitterUser.objects.get(username=mention): Notification.objects.create( notifying_tweet=new_tweet, user_notified=TwitterUser.objects.get( username=mention)) if new_tweet: tweeter = request.user tweeter.num_of_tweets += 1 tweeter.save() return HttpResponseRedirect(reverse('homepage')) form = TweetForm() return render(request, 'generic_form.html', {'form': form})
def tweet_view(request): if not request.user.is_authenticated: return HttpResponseRedirect(reverse('home')) form = TweetForm() usernames = [user.username for user in TwitterUser.objects.all()] if request.method == "POST": form = TweetForm(request.POST) if form.is_valid(): data = form.cleaned_data message = data.get("message") word_list = message.split() users = { w for word in word_list if word.startswith("@") and ( w := word[1:]) in usernames and w != request.user.username } author = request.user if users: tweet = Tweet.objects.create(message=message, author=author) for user in users: assigned_user = TwitterUser.objects.get(username=user) Notification.objects.create( username_assigned=assigned_user, tweet=tweet) else: Tweet.objects.create(message=message, author=author) return HttpResponseRedirect(reverse("home")) return render(request, "tweet.html", {"form": form})
def create_tweet(request): context = {} notify = Notification.objects.filter(reciever=request.user, read=False).count() form = TweetForm(request.POST) if form.is_valid(): data = form.cleaned_data new_data = Tweet.objects.create(user=request.user, body=data['body']) # __author__ = "Recieved help from my facilitator Elizabeth" notifications = re.findall(r'@(\S+)', data['body']) for string in notifications: user = TwitterUser.objects.filter(username=string).first() if user: Notification.objects.create(read=False, content_type=new_data, reciever=user) print(notifications) return HttpResponseRedirect( reverse('tweet:tweet_details', args=[new_data.id])) form = TweetForm() context.update({ 'form': form, 'heading_three': 'Tell everybody what you\'re up to! What\'s new? What\'s changed?', 'notify': notify }) return render(request, 'forms/generic.html', context)
def tweetform_view(request): html = "tweetform.html" if request.method == "POST": form = TweetForm(request.POST) if form.is_valid(): data = form.cleaned_data newTweet = Tweet(tweetbody=data['tweetbody'], date_filed=data['date_filed'], author=request.user) newTweet.save() # This newTweet['tweetbody'] is reaching into db # if '@' in newTweet['tweetbody']: pattern = re.search("@[\w\d]+", newTweet.tweetbody) if pattern is not None: pattern = pattern.group(0)[1:] target_user = TwitterUser.objects.get(username=pattern) Notification.objects.create( target_user=target_user, tweet=newTweet, ) baseProfile = TwitterUser.objects.get(username=request.user) newTweet.twittuser.add(baseProfile) return HttpResponseRedirect(reverse("homepage")) form = TweetForm() return render(request, html, {"form": form})
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(self, request, username): form = TweetForm(request.POST) if form.is_valid(): user = User.objects.get(username=username) tweet = Tweet(text=form.cleaned_data['text'], user=user, country=form.cleaned_data['country']) tweet.save() words = form.cleaned_data['text'].split(" ") for word in words: if word[0] == "#": hashtag, created = HashTag.objects.get_or_create(name=word[1:]) hashtag.tweet.add(tweet) return HttpResponseRedirect('/user/'+username)
def add_tweet_view(request): number_tweets = len(Tweet.objects.filter(twitter_user=request.user.id)) user_notifications = Notification.objects.filter( tweeted_user=request.user) if len(user_notifications) > 0: notification_tweet = user_notifications else: notification_tweet = '' number_notifications = len(notification_tweet) if Relationship.objects.filter(from_person=request.user): number_following = len(Relationship.objects.filter( from_person=request.user)) else: number_following = 0 if request.method == "POST": form = TweetForm(request.POST) if form.is_valid(): data = form.cleaned_data new_tweet = Tweet.objects.create( body=data.get('body'), twitter_user=request.user ) if '@' in new_tweet.body: # extract the username split_body = new_tweet.body.split() extracted_username = '' for word in split_body: if word.startswith('@'): extracted_username = word.replace('@', '') notification_user = TwitterUser.objects.filter( username=extracted_username).first() Notification.objects.create( tweeted_user=notification_user, notification_tweets_id=new_tweet.id ) return HttpResponseRedirect( reverse("tweetview", args=[new_tweet.id]) ) form = TweetForm() return render( request, "add_tweet.html", {"form": form, "profile_user": request.user, "number_notifications": number_notifications, "number_tweets": number_tweets, "number_following": number_following} )
def add_tweet_view(request): if request.method == "POST": form = TweetForm(request.POST) if form.is_valid(): data = form.cleaned_data tweet = Tweet.objects.create( text=data.get('text'), user=request.user, ) return HttpResponseRedirect(reverse("index")) form = TweetForm() return render(request, "generic_form.html", {"form": form})
def post(self, request, username): form = TweetForm(self.request.POST) if form.is_valid(): user = User.objects.get(username=username) tweet = Tweet(text=form.cleaned_data['text'], user=user, country=form.cleaned_data['country']) tweet.save() words = form.cleaned_data['text'].split(" ") for word in words: if word[0] == "#": hashtag, created = HashTag.objects.get_or_create(name=word[1:]) hashtag.tweet.add(tweet) return HttpResponseRedirect('/user/'+username)
def new_tweet(request): if request.user.is_authenticated: html = "new_tweet_form.html" form = TweetForm() if request.method == "POST": filled_form = TweetForm(request.POST) if filled_form.is_valid(): data = filled_form.cleaned_data Tweet.objects.create(tweet=data['tweet'], author=request.user, creation_date=datetime.now()) create_notifications(Tweet.objects.last()) return HttpResponseRedirect( request.GET.get('next', reverse('homepage')), ) return render(request, html, {"form": form})
def post(self, request): form = TweetForm(request.POST) if form.is_valid(): data = form.cleaned_data tweet = Tweet.objects.create(text = data.get('text'), user_tweeted = request.user) notification = re.findall(r"@([\w]+)", data.get('text')) if notification: for username in notification: user = TwitterUser.objects.get(username=username) Notification.objects.create(notification_tweet = tweet, notification_user = user, ) return redirect('/') else: return render(request, 'tweet_form.html', {"form": form})
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 tweet_form(request): pings = Notification.objects.filter(receiver=request.user) if request.method == "POST": form = TweetForm(request.POST) if form.is_valid(): data = form.cleaned_data tweet = Tweet.objects.create(body=data['body'], tweeter=request.user) if '@' in data['body']: recipient = re.findall(r'@(\w+)', data.get('body')) for receipt in recipient: message = Notification.objects.create( msg_content=tweet, receiver=TwitterUser.objects.get(username=receipt)) return HttpResponseRedirect(reverse('homepage')) form = TweetForm() return render(request, "generic_form.html", {"form": form, "pings": pings})
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 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 add_tweet(request, id): if request.method == "POST": form = TweetForm(request.POST) if form.is_valid(): data = form.cleaned_data user = MyUser.objects.get(id=id) users = MyUser.objects.all() tweet = Tweet.objects.create(tweet=data['tweet'], author=user) notify = re.findall(r'@(\w+)', data['tweet']) for user in notify: Notification.objects.create( receiver=MyUser.objects.get(displayname=user), tweet=tweet, ) return HttpResponseRedirect(reverse('homepage')) form = TweetForm() return render(request, 'addtweet.html', {'form': form})
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 post_tweet(request): if request.method == "POST": form = TweetForm(request.POST) if form.is_valid(): data = form.cleaned_data twitteruser = request.user messagebox = Tweet.objects.create(messagebox=data['messagebox'], this_user=twitteruser) # notification if '@' in messagebox.messagebox: username = re.findall(r'@(\w+)', messagebox.messagebox) for user in username: target = TwitterUser.objects.get(username=user) Notification.objects.create(notified_tweet=messagebox, target_user=target) return HttpResponseRedirect(reverse('homepage')) form = TweetForm() return render(request, 'tweet.html', {'form': form})
def tweet_form(request): if request.method == "POST": form = TweetForm(request.POST) if form.is_valid(): data = form.cleaned_data new_tweet = Tweet.objects.create( body=data.get("body"), author=request.user, ) reg = re.findall(r'@(\w{1,})', data.get("body")) for r in reg: match = TwitterUser.objects.filter(username=r) if match: Notification.objects.create(user_notified=match.first(), tweet_notification=new_tweet) return HttpResponseRedirect(reverse("homepage")) form = TweetForm() return render(request, "new_tweet.html", {"form": form})
def add_tweet_view(request): if request.method == 'POST': form = TweetForm(request.POST) if form.is_valid(): data = form.cleaned_data tweet_post = Tweet.objects.create( tweet=data['tweet'], tweet_maker=request.user ) if "@" in data['tweet']: recipients = re.findall(r'@(\w+)', data.get("tweet")) for recipient in recipients: match_user = TwitterUser.objects.get(username=recipient) if match_user: message = Notification.objects.create(msg_content=tweet_post, receiver=match_user) return HttpResponseRedirect(reverse('homepage')) form = TweetForm() return render(request, 'generic_form.html', {'form': form})
def feed(request): if request.method == 'POST': form = TweetForm(request.POST, request.FILES) if form.is_valid(): tweet = Tweet() tweet.content = form.cleaned_data['tweet'] tweet.image = form.cleaned_data.get('image') tweet.user = request.user tweet.save() form = TweetForm() else: form = TweetForm() tweets = Tweet.objects.order_by('-post_date') context = { 'tweets': tweets, 'form': form, 'user': request.user, } return render(request, "tweet/feed.html", context)
def add_tweet(request): if request.method == "POST": form = TweetForm(request.POST) if form.is_valid(): data = form.cleaned_data Tweets.objects.create( text=data['text'], author=request.user, ) all_users = re.findall(r'@(\w+)', data['text']) for pinged in all_users: Notifications.objects.create( pinged_user=TwitterUser.objects.get(username=pinged), text=text, ) return HttpResponseRedirect(reverse('homepage')) form = TweetForm() return render(request, 'addtweet.htm', {"form": form})
def post(self, request): form = TweetForm(request.POST) if form.is_valid(): data = form.cleaned_data msg = TweetMessage.objects.create( user=CustomUser.objects.get(username=request.user.username), body=data['body'], ) users = re.findall(r'@\S+', msg.body) if users: for i in users: try: if CustomUser.objects.get(username=i[1:]): NotificationModel.objects.create( user=CustomUser.objects.get(username=i[1:]), tweet=msg) except CustomUser.DoesNotExist: continue return HttpResponseRedirect(reverse('homepage'))
def create_tweet_view(request): if request.method == "POST": form = TweetForm(request.POST) if form.is_valid(): data = form.cleaned_data tweetpost = TweetModel.objects.create( body=data['body'], author=request.user, ) if "@" in data['body']: recipients = re.findall(r'@(\w+)', data.get('body')) for recipient in recipients: match_user = TwitUser.objects.get(username=recipient) if match_user: message = Notification.objects.create( message_content=tweetpost, receiver=match_user) return HttpResponseRedirect(reverse("homepage")) form = TweetForm() return render(request, 'base.html', {"form": form})
def tweet_form_view(request): if request.method == "POST": form = TweetForm(request.POST) if form.is_valid(): data = form.cleaned_data new_tweet = Tweet.objects.create( description=data.get('description'), author=request.user) mentions = re.findall(r'@(\w+)', data.get('description')) if mentions: users = MyUser.objects.all() for mention in mentions: matched_user = MyUser.objects.get(username=mention) if matched_user: Notification.objects.create( sender=matched_user, notif_tweets=new_tweet, ) return HttpResponseRedirect(reverse("home")) form = TweetForm() return render(request, "signup_form.html", {"form": form})
def create_tweet_view(request): if request.method == 'POST': form = TweetForm(request.POST) if form.is_valid(): data = form.cleaned_data posted_tweet = Tweet.objects.create( user=request.user, message=data['message'], ) mentioned_users = re.findall('@([a-zA-Z0-9_]*)', data['message']) if mentioned_users: for mentioned_user in mentioned_users: matched_user = TwitterUser.objects.get( username=mentioned_user) if matched_user: Notification.objects.create( user_mentioned=matched_user, tweet=posted_tweet, ) return redirect("/") form = TweetForm() return render(request, "create_tweet_form.html", {'form': form})
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 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'))