Example #1
0
def post_view(request):
    user = check_validation(request)
    if user:
        if request.method == 'POST':
            form = PostForm(request.POST, request.FILES)
            #fetching image and caption from the form...................................................................
            if form.is_valid():
                image = form.cleaned_data.get('image')
                caption = form.cleaned_data.get('caption')
                post = PostModel(image=image, caption=caption, user=user)
                post.save()

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

                #uploding image on cloudinary cloud.....................................................................
                #path = str(BASE_DIR + '/' + post.image.url)
                #upload = cloudinary.uploader.upload(path)
                #pdb.set_trace()
                #post.image_url = upload['url']
                #post.save()

                #doing the image analysis with the clarifai api.........................................................
                response = model.predict_by_url(post.image_url)
                response = response['outputs'][0]['data']['concepts']
                is_dirty = False
                for word in dirty:
                    for imgword in response:
                        image_word = imgword['name']
                        image_word = image_word.upper()
                        if image_word == word:
                            is_dirty = True
                            break

                if is_dirty:
                    post.dirty = True
                    post.save()

                #redirecting again to feed..............................................................................
                return redirect('/feed/')
        #handling get request...........................................................................................
        else:
            form = PostForm()
            return render(request, 'post.html', {'form': form})
    #if user is not logged in...........................................................................................
    else:
        return redirect('/login/')