Ejemplo n.º 1
0
def create():
    if request.method == 'POST':
        form = EntryForm(request.form)
        if form.validate():
            image_file = request.files['post_image']
            if image_file.filename == '':
                flash('No selected file!', 'danger')
                return redirect(request.url)
            if not allowed_image(image_file.filename):
                flash("Wrong image format! Only 'png', 'jpg', 'jpeg', 'gif' formats.", "danger")
                return redirect(request.url)
            filename_path = os.path.join(app.config['IMAGES_DIR'], secure_filename(image_file.filename))
            image_file.save(filename_path)
            img_url = image_file.filename
            entry = Entry(
                title=form.title.data,
                body=form.body.data,
                image_url=img_url,
                status=form.status.data,
                tags=form.tags.data
                )
            db.session.add(entry)
            db.session.commit()
            flash('Entry "%s" created successfully.' % entry.title, 'success')
            return redirect(url_for('entries.detail', slug=entry.slug))

    else:
        form = EntryForm()
    
    return render_template('entries/create.html', form=form, title="Create New Post")
Ejemplo n.º 2
0
def test_blank_data(self):
    # Test for blank data fields errors
    form = EntryForm({}, entry=self.entry)
    self.assertFalse(form.is_valid())
    self.assertEqual(form.errors, {
        'title': ['required'],
        'event': ['required'],
    })
Ejemplo n.º 3
0
 def test_login_max_length(self):
     self.initial_data['login'] = '******' * 51
     form = EntryForm(data=self.initial_data)
     self.assertFalse(form.is_valid())
     self.assertEqual(
         form.errors['login'],
         [
             _('Ensure this value has at most 50 characters (it has 51).')
         ]
     )
Ejemplo n.º 4
0
 def test_password_max_length(self):
     self.initial_data['password'] = '******' * 51
     form = EntryForm(data=self.initial_data)
     self.assertFalse(form.is_valid())
     self.assertEqual(
         form.errors['password'],
         [
             _('The password is too long. (max 50 characters)')
         ]
     )
Ejemplo n.º 5
0
def update_entry(request, id):
    """
    Updates an entry in the database
    """
    entry = get_object_or_404(Entry, id=id)
    form = EntryForm(request.POST or None, instance=entry)
    if form.is_valid():
        form.save()
        return redirect('entries')
    return render(request, 'create_entry.html', {'form': form})
Ejemplo n.º 6
0
 def test_url_max_length(self):
     self.initial_data['url'] = 'http://google.com/' + 'x' * 200
     form = EntryForm(data=self.initial_data)
     self.assertFalse(form.is_valid())
     self.assertEqual(
         form.errors['url'],
         [
             _('Ensure this value has at most 200 characters (it has 218).')
         ]
     )
Ejemplo n.º 7
0
 def test_invalid_url(self):
     self.initial_data['url'] = 'asdf'
     form = EntryForm(data=self.initial_data)
     self.assertFalse(form.is_valid())
     self.assertEqual(
         form.errors['url'],
         [
             _('Enter a valid URL.')
         ]
     )
Ejemplo n.º 8
0
def edit(slug):
    entry = Entry.query.filter(Entry.slug == slug).first_or_404()
    if request.method == 'POST':
        form = EntryForm(request.form, obj=entry)
        db.session.add(entry)
        db.session.commit()
        flash('Entry "%s" has been saved' % entry.title, 'success')
        return redirect(url_for('entries.detail', slug=entry.slug))
    else:
        form = EntryForm(obj=entry)

    return render_template('entries/edit.html', entry=entry, form=form)
Ejemplo n.º 9
0
def create():
    if request.method == 'POST':
        form = EntryForm(request.form)
        if form.validate():
            entry = form.save_entry(Entry())
            db.session.add(entry)
            db.session.commit()
            flash('Entry "%s" created successfully.' % entry.title, 'success')
            return redirect(url_for('entries.detail', slug=entry.slug))
    else:
        form = EntryForm()
    return render_template('entries/create.html', form=form)
Ejemplo n.º 10
0
def edit(slug):
    entry = Entry.query.filter(Entry.slug == slug).first_or_404()
    if request.method == 'POST':
        form = EntryForm(request.form, obj=entry)
        if form.validate():
            entry = form.save_entry(entry)
            db.session.add(entry)
            db.session.commit()
            return redirect(url_for('entries.detail', slug=entry.slug))
    else:
        form = EntryForm(obj=entry)
        return render_template('entries/edit.html', entry=entry, form=form)
Ejemplo n.º 11
0
def edit(slug):
    entry = get_entry_or_404(slug)
    if request.method == 'POST':
        form = EntryForm(request.form, obj=entry)
        if form.validate():
            entry = form.save_entry(entry)
            db.session.add(entry)
            db.session.commit()
            flash('Entry "%s" has been saved.' % entry.title, 'success')
            return redirect(url_for('entries.detail', slug=entry.slug))
    else:
        form = EntryForm(obj=entry)
    return render_template('entries/edit.html', entry=entry, form=form)
Ejemplo n.º 12
0
def test_valid_data(self):
    # Test for valid data
    form = EntryForm({
        'title': "Entry Test Title",
        'event': "Entry Test Event"
    },
                     entry=self.entry)

    self.assertTrue(form.is_valid())
    my_entry = form.save()
    self.assertEqual(my_entry.title, "Entry Test Title")
    self.assertEqual(my_entry.event, "Entry Test Event")
    self.assertEqual(my_entry.entry, self.entry)
Ejemplo n.º 13
0
def create():
    if request.method == "POST":
        form = EntryForm(request.form)
        if form.validate():
            entry = form.save_entry(Entry(author=g.user))
            db.session.add(entry)
            db.session.commit()
            flash('Entry "%s" created successfully.' % entry.title, "success")
            return redirect(url_for("entries.detail", slug=entry.slug))
    else:
        form = EntryForm()

    return render_template("entries/create.html", form=form)
Ejemplo n.º 14
0
def create():
    if request.method == 'POST':
        form = EntryForm(request.form)
        if form.validate() == False:
            return render_template('entries/create.html', form=form)
        else:
            entry = form.save_entry(Entry())
            db.session.add(entry)
            db.session.commit()
            return redirect(url_for('entries.detail', slug=entry.
            slug))
    else:
        form = EntryForm()
        return render_template('entries/create.html', form=form)
Ejemplo n.º 15
0
def create():
    if request.method == 'POST':
        # use the form entry data from the page to populate a new Entry() object and save it to the DB
        form = EntryForm(request.form)
        if form.validate():
            entry = form.save_entry(Entry(author=g.user))
            db.session.add(entry)
            db.session.commit()
            flash('Entry "%s" created successfully' % entry.title, 'success')
            # redirect the user to his/her newly-created form
            return redirect(url_for('entries.detail', slug=entry.slug))
    # executes when someone sends a GET request - a new 'Create Blog Post' page is created
    else:
        form = EntryForm()
    return render_template('entries/create.html', form=form)
Ejemplo n.º 16
0
def create_entry(request):
    context = RequestContext(request)

    if request.method == 'POST':
        form = EntryForm(request.POST)

        if form.is_valid():
            new_id = form.save().pk

            return redirect('entry-show', new_id)
    else:
        form = EntryForm()

    context_dict = {'form': form}
    return render_to_response('entries/create_entry.html', context_dict, context)
Ejemplo n.º 17
0
def edit_entry(request, entry_id):
    context = RequestContext(request)

    if request.method == 'POST':
        entry = get_entry(int(entry_id))
        form = EntryForm(instance=entry, data=request.POST)

        if form.is_valid():
            form.save()
            return redirect('entry-show', entry_id)
    else:
        entry = get_entry(int(entry_id))
        form = EntryForm(instance=entry)

    context_dict = {'form': form}
    return render_to_response('entries/edit_entry.html', context_dict, context)
Ejemplo n.º 18
0
def edit(slug):
    """doc."""
    entry = get_entry_or_404(slug, author=None)
    if request.method == 'POST':
        form = EntryForm(request.form, obj=entry)
        if form.validate():
            entry = form.save_entry(Entry(author=g.user))
            entry = form.save_entry(entry)
            db.session.add(entry)
            db.session.commit()
            flash('Entry "%s" created successfully.' % entry.title, 'success')
            return redirect(url_for('entries.detail', slug=entry.slug))
    else:
        form = EntryForm(obj=entry)

    return render_template('entries/edit.html', entry=entry, form=form)
Ejemplo n.º 19
0
def edit(slug):
    entry = get_entry_or_404(slug, author=None)
    # if saving an entry, we are sending data via POST
    if request.method == 'POST':
        form = EntryForm(request.form, obj=entry)
        if form.validate():
            entry = form.save_entry(entry)
            db.session.add(entry)
            db.session.commit()
            flash('Entry "%s" has been saved' % entry.title, 'success')
            return redirect(url_for('entries.detail', slug=entry.slug))
    # else if attempting to edit an entry, we are retrieving data via GET
    else:
        # attempt to prepopulate the form with entry's attributes
        form = EntryForm(obj=entry)
    return render_template('entries/edit.html', entry=entry, form=form)
Ejemplo n.º 20
0
def edit(slug):
    entry = get_entry_or_404(slug)
    if request.method == 'POST':
        form = EntryForm(request.form, obj=entry)
        if form.validate():
            entry.title = form.title.data
            entry.body = form.body.data
            entry.status = form.status.data
            entry.tags = form.tags.data 
            db.session.add(entry)
            db.session.commit()
            flash(f'Entry "{entry.title}" has been saved.', 'success')
            return redirect(url_for('entries.detail',
            slug=entry.slug))
    else:
        form = EntryForm(obj=entry)

    return render_template('entries/edit.html', entry=entry, form=form)
Ejemplo n.º 21
0
def delete_entry(request, id):
    """
    Deletes an entry in the database
    """
    entry = get_object_or_404(Entry, id=id)
    form = EntryForm(request.POST or None, instance=entry)
    if request.method == 'POST':
        entry.delete()
        return redirect('entries')
    return render(request, 'confirm_delete.html', {'entry': entry})
Ejemplo n.º 22
0
    def test_cannot_submit_temp_ban_without_length(self):
        form = EntryForm(data={
            'user': '******',
            'rule': self.rule1,
            'action': Entry.ACTION_TEMP_BAN,
        })

        self.assertIn(
            'Ban Length is required for temporary bans.',
            form.errors['__all__']
        )
Ejemplo n.º 23
0
def add_view(request):
	if request.method == "POST":
		form = EntryForm(request.POST)
		if form.is_valid():
			form.save()
			return HttpResponseRedirect('/entries/home/')
	else:
		form = EntryForm()
	context = {'form':form }
	return render(request,'entries/adds.html',context)
Ejemplo n.º 24
0
def add(request):
    if request.method == 'POST':
        # checks if the  method is a post request -is posting something
        form = EntryForm(request.POST)
        if form.is_valid():
            form.save()  # only works if you used EntryForm
            return redirect('home')
            # the name that was given to the  url of index
    else:
        form = EntryForm()  # checks  if the method is a get method
    context = {'form': form}
    return render(request, 'entries/add.html', context)
Ejemplo n.º 25
0
def add(request):
    if request.method == 'POST':
        form = EntryForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('home')
    else:
        form = EntryForm()

    context = {'form': form}

    return render(request, 'entries/add.html', context)
Ejemplo n.º 26
0
def create():
    if request.method == 'POST':
        form = EntryForm(request.form)
        if form.validate():
            entry = form.save_entry(Entry())
            db.session.add(entry)
            db.session.commit()
            return redirect(url_for('entries.detail', slug=entry.slug))

    form = EntryForm()
    return render_template('entries/create.html', form=form)
Ejemplo n.º 27
0
def edit(slug):
    entry = Entry.query.filter(Entry.slug == slug).first_or_404()
    if request.method == 'POST':
        form = EntryForm(request.form, obj=entry)
        if form.validate():
            entry = form.save_entry(entry)
            db.session.add(entry)
            db.session.commit()
            return redirect(url_for('entries.detail', slug=entry.slug))
    else:
        form = EntryForm(obj=entry)
    return render_template('entries/edit.html', entry=entry, form=form)
Ejemplo n.º 28
0
def create():
    if request.method == 'POST':
        form = EntryForm(request.form)
        if form.validate():
            entry = form.save_entry(Entry())
            db.session.add(entry)
            db.session.commit()
            flash('Entry "%s" created successfully.' % entry.title, 'success')
            return redirect(url_for('entries.detail', slug=entry.slug))
    else:
        form = EntryForm()
    return render_template('entries/create.html', form=form)
Ejemplo n.º 29
0
def edit(slug):
    entry = get_entry_or_404(slug, author=None)
    if request.method == 'POST':
        form = EntryForm(request.form, obj=entry)
        if form.validate():
            entry = form.save_entry(entry)
            db.session.add(entry)
            db.session.commit()
            flash('Entry "%s" has been saved.' % entry.title, 'success')
            return redirect(url_for('entries.detail', slug=entry.slug))
    else:
        form = EntryForm(obj=entry)
    return render_template('entries/edit.html', entry=entry, form=form)
Ejemplo n.º 30
0
def create():
    if request.method == 'POST':
        form = EntryForm(request.form)
        if form.validate():
            entry = form.save_entry(Entry(author=g.user))
            for tag in entry.tags:
                app.logger.info("***> " + str(tag.id) + "|" + str(tag.name))
            db.session.add(entry)
            db.session.commit()
            flash('Entry "%s" created successfully.' % entry.title, 'success')
            return redirect(url_for('entries.detail', slug=entry.slug))
    else:
        form = EntryForm()

    return render_template('entries/create.html', form=form)
Ejemplo n.º 31
0
class TestAdd(TestCase):
    url = RequestFactory().get('add/')
    response = views.add(url)
    form = EntryForm(data={'text': 'text'})
    assert form.is_valid() is True
    assert response.status_code == 200

    def test_add_post_valid(self):
        url = reverse('add')
        response = self.client.post(url, data={'text': 'this boy'})
        self.assertEqual(response.status_code, 302)
        self.assertRedirects(
            response, '/', status_code=302,
            target_status_code=200, fetch_redirect_response=True)

    def test_add_post_invalid(self):
        form = EntryForm(data={'text': ''})
        url = reverse('add')
        response = self.client.post(url, data={'text': ''})
        self.assertEqual(response.status_code, 200)
        assert form.is_valid() is False
Ejemplo n.º 32
0
def create():
    if request.method == 'POST':
        form = EntryForm(request.form)
        if form.validate():
            entry = form.save_entry(Entry(author=g.user))
            with db.session.no_autoflush:
                if db.session.query(exists().where(Entry.slug == entry.slug)).scalar():
                    flash('Error create Entry "%s". already exists slug' % entry.title, 'danger')
                    return redirect(url_for('entries.create', entry=entry))
                else:
                    db.session.add(entry)
                    db.session.commit()
                    return redirect(url_for('entries.detail', slug=entry.slug))
    else:
        form = EntryForm()
    return render_template('entries/create.html', form=form)
Ejemplo n.º 33
0
def edit(slug):
    entry = Entry.query.filter(Entry.slug == slug).first_or_404()
    if g.user != entry.author:
        flash("Only Author of The Post can Edit The Post", "info")
        return redirect(url_for('entries.detail', slug=slug))
    if request.method == 'POST':
        form = EntryForm(request.form, obj=entry)
        if form.validate():
            entry = form.save_entry(entry)
            db.session.add(entry)
            db.session.commit()
            flash('Entry "%s" has been saved.' % entry.title, 'success')
            return redirect(url_for('entries.detail', slug=entry.slug))
    else:
        form = EntryForm(obj=entry)

    return render_template('entries/edit.html', entry=entry, form=form)
Ejemplo n.º 34
0
def edit(slug):
    #entry = Entry.query.filter(Entry.slug == slug).first_or_404()
    entry = get_entry_or_404(slug)
    if request.method == 'POST':
        app.logger.info("===> edit")
        form = EntryForm(request.form, obj=entry)
        if form.validate():
            entry = form.save_entry(entry)
            for tag in entry.tags:
                app.logger.info("***> " + str(tag.id) + "|" + str(tag.name))
            db.session.add(entry)
            db.session.commit()
            flash('Entry "%s" has been saved.' % entry.title, 'success')
            return redirect(url_for('entries.detail', slug=entry.slug))
    else:
        form = EntryForm(obj=entry)

    return render_template('entries/edit.html', entry=entry, form=form)
Ejemplo n.º 35
0
def edit(slug):
    entry = get_entry_or_404(slug, author=g.user)
    if request.method == 'POST':
        # Mengisi atribut dari obj yang dilewatkan dengan data dari kolom form.
        # mengisi object form dengan acuan entry object yang berisi request.form
        form = EntryForm(request.form, obj=entry)
        if form.validate():

            # form.populate_obj(entry) # mengubah data entry object dengan data dari form
            # entry.generate_slug()
            entry = form.save_entry(entry)

            db.session.commit()
            flash('Entry "%s" has been saved.' % entry.title, 'success')
            return redirect(url_for('entries.detail', slug=entry.slug))
    else:
        form = EntryForm(obj=entry)

    return render_template('entries/edit.html', entry=entry, form=form)
Ejemplo n.º 36
0
def create():
    form = EntryForm()
    if request.method == 'POST':
        form = EntryForm(
            request.form
        )  # we want to instantiate the EntryForm and pass in the raw form data.
        if form.validate(
        ):  # ensure that the form is valid by calling form.validate() .
            # If the form validates, we can finally proceed with saving the entry. To do this, we
            # will call our save_entry helper method, passing in a fresh entry instance

            # entry = Entry()
            # form.populate_obj(entry)
            # author = g.user -> backref
            entry = form.save_entry(Entry(author=g.user))
            db.session.add(entry)
            db.session.commit()
            flash('Entry "%s" created successfully.' % entry.title, 'success')
            return redirect(url_for('entries.detail', slug=entry.slug))

    return render_template('entries/create.html', form=form)
Ejemplo n.º 37
0
def create_entry(request):
    """
    Create a new entry in the database
    """
    if request.method == 'POST':
        form = EntryForm(request.POST or None)
        if form.is_valid():
            # Validate form inputs
            title = form.cleaned_data['title']
            event = form.cleaned_data['event']
            """
            prevent autosaving of data before appending the authour and date posted info.
            """
            post = form.save(commit=False)
            post.authour = request.user
            post.published_date = timezone.now()
            post.save()

        return redirect("/entries/")
    else:
        form = EntryForm()
        return render(request, "create_entry.html", {"form": form})
Ejemplo n.º 38
0
def edit(request, entry_id):
    entry = get_object_or_404(Entry, pk=entry_id)
    form = EntryForm()
    context = { 'entry': entry, 'title': 'エントリー編集', 'form': form }
    return render(request, 'entries/edit.html', context)
Ejemplo n.º 39
0
def new(request):
    entry = Entry()
    form = EntryForm()
    context = { 'entry': entry, 'title': 'エントリー新規作成', 'form': form }
    return render(request, 'entries/new.html', context)
Ejemplo n.º 40
0
 def test_valid_initial_data(self):
     form = EntryForm(data=self.initial_data)
     self.assertTrue(form.is_valid())