Example #1
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()
                z = post.image.url
                path = str(BASE_DIR + '\\' + post.image.url)

                client = ImgurClient(
                    '0e144dffb567600',
                    '17ed6b09b6b16a35f32bfcd307e1b21cb132b21e')
                post.image_url = client.upload_from_path(path,
                                                         anon=True)['link']

                # using calrifai
                response = model.predict_by_url(url=post.image_url)
                right = response["outputs"][0]["data"]["concepts"][0]["value"]

                # using paralleldots
                set_api_key(PKEY)
                response = sentiment(str(caption))
                sentiment_score = response["sentiment"]

                if sentiment_score >= 0.6 and right > 0.5:
                    post.save()
                    saved_message = 'Post is successfully submitted'
                    return render(request, 'error.html',
                                  {'context': saved_message})
                else:
                    error_message = 'Post cannot be submitted'
                    post.delete()
                    return render(request, 'error.html',
                                  {'context': error_message})

            return redirect('/post/')

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

    if user:
        if request.method == 'POST':
            form = PostForm(request.POST, request.FILES)
            if form.is_valid():
                image = form.cleaned_data['image']
                caption = form.cleaned_data['caption']
                if is_abusive(caption):
                    ctypes.windll.user32.MessageBoxW(
                        0, u"Your caption contain Abusive content", u"Warning",
                        0)
                    return redirect('/post/')
                else:
                    post = PostModel(user=user, image=image, caption=caption)
                    path = BASE_DIR + "\\" + str(post.image.url)
                    client = ImgurClient(client_id, client_secret)
                    post.image_url = client.upload_from_path(path,
                                                             anon=True)['link']
                    # using clarifai api
                    app = ClarifaiApp(api_key='')
                    model = app.models.get('nsfw-v1.0')
                    img_type = model.predict_by_url(post.image_url)

                    if img_type['outputs'][0]['data']['concepts'][0][
                            'name'] == 'nsfw':
                        if img_type['outputs'][0]['data']['concepts'][0][
                                'value'] > img_type['outputs'][0]['data'][
                                    'concepts'][1]['value']:
                            post.delete()
                        else:
                            post.save()
                    else:
                        if img_type['outputs'][0]['data']['concepts'][0][
                                'value'] < img_type['outputs'][0]['data'][
                                    'concepts'][1]['value']:
                            post.delete()
                        else:
                            post.save()
                    post.save()
                return redirect('/feed/')
        else:
            form = PostForm()
        return render(request, 'post.html', {'form': form})
    else:
        return redirect('/login/')
Example #3
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()
                z = post.image.url
                path = str(BASE_DIR + '\\' + post.image.url)

                client = ImgurClient(
                    '4127d9468ed87d3',
                    '2ee021fa920430bbd6d998ce47d483e6f4408c63')
                post.image_url = client.upload_from_path(path,
                                                         anon=True)['link']

                # using paralleldots

                set_api_key(PKEY)
                response = sentiment(str(caption))
                sentiment_score = response['sentiment']

                if sentiment_score >= 0.6:
                    post.save()
                    saved_message = 'Post is successfully submitted'
                    return render(request, 'error.html',
                                  {'context': saved_message})
                else:
                    error_message = 'Post cannot be submitted'
                    post.delete()
                    return render(request, 'error.html',
                                  {'context': error_message})

            return redirect('/post/')

        else:
            form = PostForm()
        return render(
            request,
            'post.html',
            {'form': form},
        )
    else:
        return redirect('/login/')
Example #4
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)
                if checkComment(caption) == 1 and checkImage(path) == 1:

                    print post.image_url
                    # adding imgur client to maintain url of images
                    # try catch edge case if connection fails or image can't be uploaded
                    try:
                        client = ImgurClient(CLIENT_ID, CLIENT_SECRET)
                        post.image_url = client.upload_from_path(
                            path, anon=True)['link']
                    except:
                        return render(
                            request, 'post.html',
                            {'msg': 'Failed to upload! Try again later'})

                    post.save()
                    return render(request, 'login_success.html',
                                  {'msg': 'Post added successfully!'})
                else:
                    return render(request, 'login_success.html', {
                        'msg':
                        'Please avoid use of obscene language and images'
                    })
                    post.delete()

                return redirect('/feeds/')

        else:
            form = PostForm()
        return render(request, 'post.html', {'form': form})
    else:
        return redirect('/login/')
Example #5
0
def post_view(request):
    user = check_validation(request)
    if user:
        if request.method == 'GET':
            form = PostForm()
            return render(request, 'post.html', {'form': form, 'user': user})
        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')
                post = PostModel(user=user, image=image, caption=caption)
                path = str(BASE_DIR + "/user_images/" + post.image.url)
                post.save()
                try:
                    client = ImgurClient(Client_ID, Client_secret)
                    post.image_url = client.upload_from_path(path,
                                                             anon=True)['link']
                    post.save()
                except:
                    print "Unable to connect to internet"
                    post.delete()
                    return render(
                        request, 'post.html', {
                            'form': form,
                            'user': user,
                            'error': "Internet Connectivity Issue"
                        })
                points = win_points(user, post.image_url, caption)
                return render(request, 'post.html', {
                    'form': form,
                    'user': user,
                    'error': points
                })
            else:
                return render(request, 'post.html', {
                    'form': form,
                    'user': user,
                    'error': "Unable to Add Post"
                })

    else:
        return redirect('/login/')
Example #6
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(
                    "cf01c350e85ac5e",
                    "242c1c9414317fa82779c081c50bdffcac64a6ee")
                post.image_url = client.upload_from_path(path,
                                                         anon=True)['link']
                post.save()
                app = ClarifaiApp(api_key=CLARIFAI_API_KEY)
                model = app.models.get('nsfw-v1.0')
                response = model.predict_by_url(url=post.image_url)
                concepts_value = response['outputs'][0]['data']['concepts']
                for i in concepts_value:
                    if i['name'] == 'nsfw':
                        nudity_level = i['value']

                        if nudity_level >= 0.85:
                            print response['outputs'][0]['data']['concepts']
                            print nudity_level
                            post.delete()
                            error_message = "You are trying to post an inappropriate photo!!"
                            return render(request, "error.html",
                                          {'error_message': error_message})
                        else:
                            return redirect('/feed/')
        else:
            form = PostForm()

        return render(request, "post.html", {'form': form})
    else:
        return redirect('login')