Example #1
0
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})
Example #2
0
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
    })
Example #3
0
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)
Example #4
0
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})
Example #5
0
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})
Example #6
0
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,
        },
    )
Example #7
0
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})
Example #8
0
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('/')
Example #9
0
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}
    )
Example #10
0
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})
Example #11
0
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})
Example #12
0
    def get(self, request, username):
        params = dict()
        userProfile = User.objects.get(username=username)
        try:
            userFollower = UserFollower.objects.get(user=userProfile)
            if userFollower.followers.filter(username=request.user.username).exists():
                params["following"] = True
            else:
                params["following"] = False
        except:
            userFollower = []

        form = TweetForm(initial={'country': 'Global'})
        search_form = SearchForm()
        tweets = Tweet.objects.filter(user=userProfile).order_by('-created_date')
        paginator = Paginator(tweets, TWEET_PER_PAGE)
        page = request.GET.get('page')
        try:
            tweets = paginator.page(page)
        except PageNotAnInteger:
            # If page is not an integer, deliver first page.
            tweets = paginator.page(1)
        except EmptyPage:
            # If page is out of range (e.g. 9999), deliver last page of results.
            tweets = paginator.page(paginator.num_pages)

        params["tweets"] = tweets
        params["profile"] = userProfile
        params["form"] = form
        params["search"] = search_form
        return render(request, 'profile.html', params)
Example #13
0
 def get(self, request):
     all_tweets = Tweet.objects.all().order_by('-created')
     all_comments = Comments.objects.all().order_by('-created_comment')
     form = TweetForm()
     return render(request, 'tweet/Homepage.html', context={"all_tweets": all_tweets,
                                                            "form": form,
                                                            "all_comments": all_comments})
Example #14
0
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})
Example #15
0
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)
Example #16
0
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
        })
Example #17
0
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
    })
Example #18
0
 def get(self, request, username):
     params = dict()
     user = User.objects.get(username=username)
     tweets = Tweet.objects.filter(user=user)
     form = TweetForm(initial={'country': 'Global'})
     params["tweets"] = tweets
     params["user"] = user
     params["form"] = form
     return render(request, 'profile.html', params)
Example #19
0
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})
Example #20
0
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('/')
Example #21
0
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})
Example #22
0
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})
Example #23
0
 def get(self, request):
     tweets = TweetMessage.objects.filter(user=request.user)
     num_notif = NotificationModel.objects.filter(user=request.user,
                                                  viewed=False).count()
     form = TweetForm()
     return render(request, 'tweet/tweet.html', {
         'form': form,
         'tweets': tweets,
         'num_notif': num_notif
     })
Example #24
0
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})
Example #25
0
def feed(request):
    userids = []
    for user in request.user.twitterprofile.follows.all():
        userids.append(user.id)

    userids.append(request.user.id)
    tweets = Tweet.objects.filter(user_id__in=userids)[0:25]
    form = TweetForm()

    return render(request, 'feed.html', {'tweets': tweets, 'form': form})
Example #26
0
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)
Example #27
0
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})
Example #28
0
 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")
Example #29
0
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})
Example #30
0
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})