Exemplo n.º 1
0
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/')
Exemplo n.º 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/')
Exemplo n.º 3
0
def index():
    form = PostForm()
    if form.validate_on_submit():
        language = guess_language(form.post.data)
        if language == 'UNKNOWN' or len(language) > 5:
            language = ''
        post = Post(body=form.post.data,
                    author=current_user,
                    language=language)
        db.session.add(post)
        db.session.commit()
        flash(_('Your post is now live!'))
        return redirect(url_for('index'))
    page = request.args.get('page', 1, type=int)
    posts = current_user.followed_posts().paginate(
        page, app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('index', page=posts.next_num) \
        if posts.has_next else None
    prev_url = url_for('index', page=posts.prev_num) \
        if posts.has_prev else None
    return render_template('index.html',
                           title=_('Home'),
                           form=form,
                           posts=posts.items,
                           next_url=next_url,
                           prev_url=prev_url)
Exemplo n.º 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')
                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('/')
Exemplo n.º 5
0
def write():
	form = PostForm()
	if form.validate_on_submit():
		post = Post(title=form.title.data,content = form.content.data,user_id = current_user.id)
		db.session.add(post)
		db.session.commit()
		flash('Your post is now live!')
		return redirect(url_for('index'))
	return render_template('write.html',title='Write',form=form)
Exemplo n.º 6
0
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        p = Post(title = form.title.data, body = form.body.data,\
            subject = form.subject.data, user_id = current_user.id)
        db.session.add(p)
        db.session.commit()
        return redirect(url_for('index'))
    return render_template('new_post.html', form=form)
Exemplo n.º 7
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/')
Exemplo n.º 8
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/')
Exemplo n.º 9
0
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(title=form.title.data,
                    content=form.content.data,
                    author=current_user)
        db.session.add(post)
        db.session.commit()
        flash('your post has been created!', 'success')
        return redirect(url_for('home'))
    return render_template('create_post.html',
                           title='New post',
                           form=form,
                           legend='Update Post')
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})
Exemplo n.º 11
0
def post(request):
    if request.method == 'GET':
        form = PostForm()  # フォームの作成
        context = { 'form': form }  # コンテキストにフォームを納格
        return render(request, 'myapp/post.html', context)  # 描画
    else:
        return HttpResponse('未実装')
Exemplo n.º 12
0
def post():
    """
    記事をポストするページ
    """
    form = PostForm()
    if form.validate_on_submit():
        article = Post(title=form.title.data,
                       author=current_user,
                       tag=form.tag.data,
                       body=form.body.data,
                       published_on=datetime.now())
        db.session.add(article)
        db.session.commit()
        print("post success")
        flash('Successfully posted')
        return redirect(url_for('index'))
    return render_template('post.html', form=form)
Exemplo n.º 13
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)
Exemplo n.º 14
0
def update_post(post_id):
    post = Post.query.get_or_404(post_id)
    if post.author != current_user:
        abort(403)
    form = PostForm()
    if form.validate_on_submit():
        post.title = form.title.data
        post.content = form.content.data
        db.session.commit()
        flash('Your post has been updated!', 'success')
        return redirect(url_for('post', post_id=post.id))
    elif request.method == 'GET':
        form.title.data = post.title
        form.content.data = post.content
    return render_template('create_post.html',
                           title='Update Post',
                           form=form,
                           legend='Update Post')
Exemplo n.º 15
0
def edit_post(request, post_id):

    post_a_editar = Post.objects.get(id=post_id)
    form = PostForm(request.POST, instance=post_a_editar)

    return render(request, "edit_post.html", {
        'form': form,
        'post_a_editar': post_a_editar
    })
Exemplo n.º 16
0
def post():
    """
    記事をポストするページ
    """
    form = PostForm()
    if form.validate_on_submit():
        article = Post(
            title=form.title.data,
            author=current_user,
            tag=form.tag.data,
            body=form.body.data,
            published_on=datetime.now(),
        )
        db.session.add(article)
        db.session.commit()
        print("post success")
        flash("Successfully posted")
        return redirect(url_for("index"))
    return render_template("post.html", form=form)
Exemplo n.º 17
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/')
Exemplo n.º 18
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'))
Exemplo n.º 19
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)
Exemplo n.º 20
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})
Exemplo n.º 21
0
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})
Exemplo n.º 22
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})
Exemplo n.º 23
0
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})
Exemplo n.º 24
0
def editor(request):
    form = PostForm()  # An unbound form
    return render(request, 'editor.html', {
        'form': form,
    })
Exemplo n.º 25
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})
Exemplo n.º 26
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
    })