def add_data(request):
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.speed = get_speed(request.POST['distance'],
                                   request.POST['duration'])
            post.save()
            return redirect('data_list')
        else:
            print(form.is_valid())
    else:
        form = PostForm()
    return render(request, 'myapp/add_data.html', {'form': form})
Beispiel #2
0
def post_view(request):
    # -----------------------------------------here is the function logic------------------------------------------------------------
    user = check_validation(request)

    if user:
        if request.method == 'POST':
            form = PostForm(request.POST, request.FILES)
            if form.is_valid():
                image = form.cleaned_data.get('image')
                caption = form.cleaned_data.get('caption')
                post = PostModel(user=user, image=image, caption=caption)
                post.save()

                path = str(BASE_DIR + "//" + post.image.url)

                client = ImgurClient(client_id, client_sec)
                post.image_url = client.upload_from_path(path, anon=True)['link']
                post.save()

                return redirect('/feed/')

        else:
            form = PostForm()

        return render(request, 'posts.html', {'form' : form})

        return render(request, 'posts.html', {'form': form})

    else:
        return redirect('/login/')
def edit_data(request, id_post):
    post = get_object_or_404(Add_data, pk=id_post)
    if request.method == "POST":
        form = PostForm(request.POST, instance=post)
        if form.is_valid():
            post.author = request.user
            post.speed = get_speed(request.POST['distance'],
                                   request.POST['duration'])
            post.save()
            return redirect('data_list')
        else:
            print(form.is_valid())
    else:
        print(post)
        form = PostForm(instance=post)
    return render(request, 'myapp/add_data.html', {'form': form})
Beispiel #4
0
def post_update(request, pk):
    post = get_object_or_404(Post, pk=pk)

    if request.method == 'POST':
        f = PostForm(request.POST, instance=post)

        if f.is_valid():
            # if author is not selected and user is superuser, then assign the post to the author named staff
            if request.POST.get('author') == "" and request.user.is_superuser:
                updated_post = f.save(commit=False)
                author = Author.objects.get(user__username='******')
                updated_post.author = author
                updated_post.save()
                f.save_m2m()
            # if author is selected and user is superuser
            elif request.POST.get('author') and request.user.is_superuser:
                updated_post = f.save()
            # if user is not a superuser
            else:
                updated_post = f.save(commit=False)
                updated_post.author = Author.objects.get(
                    user__username=request.user.username)
                updated_post.save()
                f.save_m2m()

            messages.add_message(request, messages.INFO, 'Post updated.')
            return redirect(reverse('post_update', args=[post.id]))
    else:
        f = PostForm(instance=post)

    return render(request, 'cadmin/post_update.html', {
        'form': f,
        'post': post
    })
Beispiel #5
0
def post_view(request):
    user = check_validation(request)

    if user:
        if request.method == 'POST':
            form = PostForm(request.POST, request.FILES)
            if form.is_valid():
                image = form.cleaned_data.get('image')
                caption = form.cleaned_data.get('caption')
                text = form.cleaned_data.get('text')
                post = PostModel(user=user, image=image, caption=caption, text=text)
                post.save()

                path = str(BASE_DIR + '/' + post.image.url)

                client = ImgurClient('37e25d0cbc857c0', 'f7a8c883baef922700e185d6b987a607c7a56840')
                post.image_url = client.upload_from_path(path,anon=True)['link']
                post.save()
                return redirect('/feed/')

        else:
            form = PostForm()
        return render(request, 'feed.html', {'form': form})
    else:
        return redirect('/')
Beispiel #6
0
def post_add(request):
    if request.method == "POST":
        f = PostForm(request.POST)
        if f.is_valid():
            # if author is not selected and user is superuser,
            # then assign the post to the author named staff
            if request.POST.get('author') == "" and request.user.is_superuser:
                new_post = f.save(commit=False)
                author = Author.objects.get(user__username='******')
                new_post.author = author
                new_post.save()
                f.save_m2m()

            # if author is selected and user is superuser
            elif request.POST.get('author') and request.user.is_superuser:
                new_post = f.save()

            # if user is not a superuser
            else:
                new_post = f.save(commit=False)
                new_post.author = Author.objects.get(
                    user__username=request.user.username)
                new_post.save()
                f.save_m2m()

            messages.add_message(request, messages.INFO, 'Post added.')
            return redirect('post_add')
    else:
        f = PostForm()

    return render(request, 'cadmin/post_add.html', {'form': f})
Beispiel #7
0
def post_view(request):
    user = check_validation(request)
    form = PostForm()
    if user:
        if request.method == 'GET':
            form = PostForm()
            return render(request, 'post.html', {'form': form})
        elif request.method == 'POST':
            form = PostForm(request.POST, request.FILES)
            if form.is_valid():
                image = form.cleaned_data.get('image')
                caption = form.cleaned_data.get('caption')
                userpost = PostModel(user=user, image=image, caption=caption)
                userpost.save()
                print userpost.image.url
                path = os.path.join(BASE_DIR, userpost.image.url)
                print BASE_DIR
                print path
                client = ImgurClient(
                    '152e68c38c8c4b3',
                    'b1f871c0a6d1cb40fbdd3eab5d9b6d8f5e17dbc5')
                userpost.image_url = client.upload_from_path(path,
                                                             anon=True)['link']
                userpost.save()
                return redirect('/feed/')
            else:
                form = PostForm()
                return render(request, 'post.html', {'form': form})
    else:
        return redirect('/login/')
def post_view(
    request
):  # post view funtion is used to upload file or image that you want to add to the feed and also provide funtionality to add caption also
    user = check_validation(request)

    if user:
        if request.method == 'POST':
            form = PostForm(request.POST, request.FILES)
            if form.is_valid():
                image = form.cleaned_data.get('image')
                caption = form.cleaned_data.get('caption')
                post = PostModel(user=user, image=image, caption=caption)
                post.save()

                path = str(BASE_DIR + '\\' + post.image.url)

                client = ImgurClient(
                    '319feb40023adce',
                    '2a01664decd3b8b2439bfce7caae16d47b671837')
                post.image_url = client.upload_from_path(path,
                                                         anon=True)['link']
                post.save()

                add_category(post)
                ctypes.windll.user32.MessageBoxW(
                    0, u"post successsfully created", u"SUCCESS", 0)

                return redirect('/feed/')

        else:
            form = PostForm()
        return render(request, 'post.html', {'form': form})
    else:
        return redirect('/login/')
Beispiel #9
0
def post_view(request):
    user = check_validation(request)

    if user:
        if request.method == 'POST':
            form = PostForm(request.POST, request.FILES)

            if form.is_valid():
                image = form.cleaned_data.get('image')
                caption = form.cleaned_data.get('caption')
                post = PostModel(user=user, image=image, caption=caption)
                post.save()
                path = str(BASE_DIR +'/'+ post.image.url)


                client = ImgurClient(YOUR_CLIENT_ID, YOUR_CLIENT_SECRET)

                post.image_url = client.upload_from_path(path, anon=True)['link']
                #Creating instance of an API with KEY
                app = ClarifaiApp(api_key=KEY)

                model = app.models.get('nsfw-v1.0')

                response_image = model.predict_by_url(url=post.image_url)
                safe=response_image['outputs'][0]['data']['concepts'][0]['value']

                set_api_key(PKEY)


                response = sentiment(str(caption))

                sentiment_value = response['sentiment']

                if sentiment_value > 0.6 and safe > 0.6:
                    post.save()
                    success_message = 'Post can be submitted'
                    return render(request, 'post.html', {'success_message': success_message})
                else:
                    error_message = 'Post cannot be submitted'
                    post.delete()
                    return render(request, 'post.html', {'error_message': error_message})

                return redirect('/feed/')




        else:

            form = PostForm()



        return render(request, 'post.html', {'form' : form})
    else:
        return redirect('/login/')
Beispiel #10
0
def post_form(request):
    if request.method == 'GET':
        f = PostForm()
        context = {'postform': f}
        return render(request, 'myapp/post.html', context)
    if request.method == 'POST':
        f = PostForm(request.POST)
        if f.is_valid():
            f.save()
        return redirect(reverse('all posts'))
def newpost(request, pk):
    u = get_object_or_404(User, pk=pk)
    form = PostForm()
    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            form.save(commit=False)
            form.instance.author = u
            form.save()
            return redirect("home")
    return render(request, 'myapp/newpost.html', {'form': form})
Beispiel #12
0
def add(request):
    if (request.method == "POST"):
        form = PostForm(request.POST, request.FILES)
        if (form.is_valid()):
            form.save()
            messages.success(request, 'Post Added Successfully')
        else:
            messages.success(request, "Please Enter Mandatory Details")

    else:
        form = PostForm()

    return render(request, "myapp/post_form.html", {'form': form})
Beispiel #13
0
def add_post(request):

    if request.method == "POST":
        form = PostForm(request.POST)
        if form.is_valid():
            model_instance = form.save(commit=False)
            model_instance.date_published = datetime.datetime.now()
            model_instance.save()
            return redirect('list_posts')
        else:
            return render(request, "add_post.html", {'form': form})
    else:
        form = PostForm()
        return render(request, "add_post.html", {'form': form})
Beispiel #14
0
def create_view(request):
    # dictionary for initial data with
    # field names as keys
    context = {}
    u = User.objects.get(username=request.user)
    aa = u.profile.id

    # add the dictionary during initialization
    form = PostForm(request.POST or None, request.FILES or None)
    if form.is_valid():

        f = form.save(commit=False)
        f.user = u
        f.save()
        return HttpResponseRedirect(reverse('myapp:detail', args=[aa]))

    context['form'] = form
    return render(request, "myapp/post_form.html", context)
Beispiel #15
0
def post_view(request):
    user = check_validation(request)
    dict = {}
    if user:
        if request.method == 'POST':
            form = PostForm(request.POST, request.FILES)
            if form.is_valid():
                image = form.cleaned_data.get('image')
                caption = form.cleaned_data.get('caption')
                post = PostModel(user=user, image=image, caption=caption)
                post.save()

                path = str(BASE_DIR + '/' + post.image.url)
                client = ImgurClient(client_id, client_secret)
                post.image_url = client.upload_from_path(path, anon=True)['link']
                post.save()

                app = ClarifaiApp(api_key=clarifai_api)
                model = app.models.get('logo')
                response = model.predict_by_url(url=post.image_url)
                try:
                    val = float(response['outputs'][0]['data']['regions'][0]['data']['concepts'][0]['value']) * 10
                    val = float("%.2f" % (val))
                except:
                    val = 7.55
                else:
                    pass

                point = PointsModel(post=post, point=val)
                point.save()

                return redirect('/feed/')

        else:
            form = PostForm()

        dict['form'] = form
        return render(request, 'post.html', dict)

    else:
        return redirect('/login/')
Beispiel #16
0
def update_view(request, id):
    # dictionary for initial data with
    # field names as keys
    context = {}

    # fetch the object related to passed id
    obj = get_object_or_404(Post, id=id)
    p = Post.objects.get(id=id)
    pp = p.user.profile.id

    # pass the object as instance in form
    form = PostForm(request.POST or None, request.FILES or None, instance=obj)

    # save the data from the form and
    # redirect to detail_view
    if form.is_valid():
        form.save()
        return HttpResponseRedirect(reverse('myapp:detail', args=[pp]))

    # add form dictionary to context
    context["form"] = form

    return render(request, "myapp/post_form.html", context)