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') main, sub = image.content_type.split('/') if not (main == 'image' and sub.lower() in ['jpeg', 'pjpeg', 'png', 'jpg']): form = PostForm() # Printing appropriate message when something other than image is uploaded message = {'message': 'Enter JPEG or PNG image','form': form} return render(request, 'post.html', message) else: caption = form.cleaned_data.get('caption') post = PostModel(user=user, image=image, caption=caption) post.save() path = str(BASE_DIR + '\\' + post.image.url) # Saving image to Imgur cloud client = ImgurClient('31a3f32e7b361e8', '6ad2c7acd06b96d4d2e61ae015c2ea5ae016a059') post.image_url = client.upload_from_path(path,anon=True)['link'] # Calling the function to get keywords using Clarifai response_clarifai = get_keywords_from_image(post.image_url) arr_of_dict = response_clarifai['outputs'][0]['data']['concepts'] for i in range(0, len(arr_of_dict)): keyword = arr_of_dict[i]['name'] value = arr_of_dict[i]['value'] if(keyword == 'Dirty' and value >0.5): is_dirty=True # If image is dirty then we send email using Sendgrid API to respective authority message_payload = { "personalizations": [ { "to": [ { "email": '*****@*****.**' #authority } ], "subject": 'Dirty area!' } ], "from": { "email": "*****@*****.**", "name": 'Swacch Bharat' }, "content": [ { "type": "text/html", "value": "<h1>Swacch Bharat</h1><br><br> <img src =" + post.image_url + " <br>" } ] } send_response(message_payload) elif (keyword == 'Clean'and value >0.5): is_dirty = False else: is_dirty = False post.is_dirty=is_dirty post.save() return redirect('/feed/') else: print request.body form = PostForm() return render(request, 'post.html', {'form' : form}) else: return redirect('/login/')