Exemple #1
0
def edit_post(request, pk):
    print("Edit post")
    try:
        post = Post.objects.get(pk=pk, author=request.user)
        # print("Found post")
    except Post.DoesNotExist:
        return render(request, 'accounts/index.html', {})
    if request.method == "POST":
        text = request.POST.get("text")
        if text == "":
            return feed(request)
        post.text = text
        post.is_edited = True
        post.save()
        hashtags = get_hashtags(text)
        for hashtag in hashtags:
            htag = HashTags.objects.get_or_create(keyword=hashtag)[0]
            _ = HashTagsPostTable.objects.get_or_create(post=post,
                                                        hashtag=htag)
        return view_post(request, post.pk)
    else:
        form = dict()
        form["text"] = post.text
        form["time"] = post.time_of_posting
        form["tags"] = post.tags.all()
        form["pk"] = pk
        # print(form["tags"])
        return render(request, 'post/edit_post.html', {'form': form})
def profile_endorsements(request, username):
    eligible_profiles = Profile.objects.exclude(pk=request.profile.id)
    profile = get_object_or_404(eligible_profiles, user__username=username)
    return feed(request,
                item_type=Endorsement,
                recipient=profile,
                template='profile_endorsements.html',
                extra_context={'profile': profile})
Exemple #3
0
def create_post(request):
    #print("Cretaing Post")
    user = request.user
    if request.method == "POST":
        text = request.POST.get("text")
        from_feed = request.POST.get("from_feed")
        print(text)
        if text == "":
            return feed(request)
        tagged_friends = request.POST.getlist("checkbox_tag")
        print(tagged_friends)
        post = Post.objects.create(text=text, author=request.user, time_of_posting = timezone.now())
        print(post)
        post.save()
        for friend in tagged_friends:
            print("Tagging ",friend)
            tagged_friend = User.objects.get(username=friend)
            post.tags.add(tagged_friend)
            tag_notif = TagNotification.objects.create(post=post, 
                                        tagged_user=tagged_friend, time_of_tagging=timezone.now())
            print(tag_notif)
            #post.save()
        post.save()
        
        print(post.tags.all())

        if from_feed:
            print("From Feed")
            return JsonResponse({"text":text,
                                 "pk": post.pk,
                                "like_btn_class": "btn-outline-success",
                                "dislike_btn_class" :"btn-outline-danger", 
                                "num_dislikes": 0, 
                                "num_likes": 0})
        return redirect("feed:news_feed")
    else:
        form = PostForm()
        friends = UserProfileInfo.objects.filter(user=user
                                                ).annotate(friend_name=F("friend__username"
                                                )).values("friend_name")

        #friend_list = UserProfileInfo.objects.get(user=user).friend.all().values_list("username")
        friends_list = list()
        for friend in friends:
            friends_list.append(friend["friend_name"])
        print(friends_list)
        print(friends)
        friends_exist = True
        if not friends:
            friends_exist = False
        print(friends_exist)
        return render(request, 'post/new_post.html', {'friends':list(friends),
                            "friends_list": friends_list,
                             'friends_exist':friends_exist})
def profile_posts(request, username):
    profile = get_object_or_404(Profile, user__username=username)
    if profile == request.profile:
        template = 'my_posts.html'
        extra_context = {}
    else:
        template = 'profile_posts.html'
        extra_context = {'profile': profile}
    return feed(request,
                item_type=Post,
                poster=profile,
                template=template,
                extra_context=extra_context)
Exemple #5
0
def edit_post(request, pk):
    print("Edit post")
    try:
        post = Post.objects.get(pk=pk, author=request.user)
        print("Found post")
    except Post.DoesNotExist:
        return render(request, 'accounts/index.html', {})
    if request.method == "POST":
        text = request.POST.get("text")
        if text == "":
            return feed(request)
        post.text = text
        post.is_edited = True
        post.save()
        return view_post(request, post.pk)
    else:
        form = dict()
        form["text"] = post.text
        form["time"] = post.time_of_posting
        form["tags"] = post.tags.all()
        form["pk"] = pk
        print(form["tags"])
        return render(request, 'post/edit_post.html', {'form': form})
Exemple #6
0
def create_post(request):
    #print("Cretaing Post")
    user = request.user
    upi = UserProfileInfo.objects.get(user=user)
    if request.method == "POST":
        text = request.POST.get("text")
        from_feed = request.POST.get("from_feed")
        if text == "":
            return feed(request)
        tagged_friends = request.POST.getlist("checkbox_tag")
        try:
            post = Post.objects.create(text=text,
                                       author=request.user,
                                       author_profile=upi,
                                       time_of_posting=timezone.now())
        except Exception as e:
            raise e
        file = None
        try:
            base_url = "https://api.twitter.com/1/statuses/oembed.json?url="
            tweet_url = request.POST.get("tweet_url")
            embed_url = base_url + tweet_url
            embedded_tweet = python_requests.get(embed_url).json()
            embedded_tweet = embedded_tweet['html']
            embedded_tweet = embedded_tweet.split('\n')[0]
            post.tweet_url = embedded_tweet
        except Exception as e:
            print(e)
        try:
            base_url = "https://open.spotify.com/embed/track/"
            spotify_song_url = request.POST.get("spotify_song_url")
            track_id = get_track_id(spotify_song_url)
            spotify_song_url = base_url + track_id
            post.spotify_url = spotify_song_url
        except Exception as e:
            print(e)
        try:
            youtube_video_url = request.POST['youtube_url']
            youtube_video_id = video_id(youtube_video_url).split('&')[0]
            if youtube_video_id:
                default_youtube_embed_url = "https://www.youtube.com/embed/"
                post.youtube_video_url = default_youtube_embed_url + youtube_video_id
                # print(post.youtube_video_url)
        except Exception as e:
            print(e)
        try:
            file = request.FILES['image']
            file, extension = upload_image(file)
            post.imgur_url = file
            post.is_video = extension
            # print("Got the file")
        except Exception as e:
            print(e)
        try:
            article_url = request.POST['article_url']
            print(article_url)
            if article_url:
                article_preview = get_link_preview(article_url)
                if article_preview:
                    post.article_link = article_url
                    post.article_preview = article_preview
        except Exception as e:
            print(e)
        # post.save()
        for friend in tagged_friends:
            tagged_friend = UserProfileInfo.objects.get(user__username=friend)
            _ = TaggedPost.objects.create(post=post, user=tagged_friend)
            tag_notif = TagNotification.objects.create(
                post=post,
                tagged_user=tagged_friend.user,
                time_of_tagging=timezone.now())
        post.save()
        hashtags = get_hashtags(text)
        # print(hashtags)
        for hashtag in hashtags:
            htag = HashTags.objects.get_or_create(keyword=hashtag)[0]
            _ = HashTagsPostTable.objects.create(post=post, hashtag=htag)

        if from_feed:
            return JsonResponse({
                "text": text,
                "pk": post.pk,
                "like_btn_class": "btn-outline-success",
                "dislike_btn_class": "btn-outline-danger",
                "num_dislikes": 0,
                "num_likes": 0
            })
        return redirect("feed:news_feed")
    else:
        form = PostForm()
        # print("8")
        friends = Friend.objects.filter(source__user=user).annotate(
            friend_name=F("destination__user__username")).values("friend_name")
        # friends = list(friends)
        return render(request, 'post/new_post.html', {
            "friends": friends,
            'friends_exist': friends.exists()
        })