Skip to content

Commit

Permalink
rewritten uploads as section in admin
Browse files Browse the repository at this point in the history
  • Loading branch information
anxolerd committed Aug 13, 2015
1 parent 763303c commit b86f785
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 25 deletions.
50 changes: 28 additions & 22 deletions project/admin/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from flask import render_template, url_for, session, redirect
from flask import request, current_app
from project.admin import forms
from project.blueprints import admin_app
from project.pages.forms import PageBlockForm, PageForm
from project.pages.utils import PageDetail
from project.admin.utils import EntryDetail, EntryList, VacancyList
from project.admin.utils import (
EntryDetail, EntryList, VacancyList, GalleryImageDetail
)
from project.auth.forms import RegisterForm
from project import models

Expand Down Expand Up @@ -232,6 +233,31 @@ def register_section(*, section_name, list_endpoint,
list_endpoint="mail_templates_list",
)

# Gallery Images
gallery_images_list = EntryList.as_view(
name='gallery_images_list',
model=models.UploadedImage,
template="admin/gallery_images.html",
)

gallery_image_detail = GalleryImageDetail.as_view(
name='gallery_image_detail',
create_form=forms.image_upload_form_factory,
update_form=forms.EditImageForm,
model=models.UploadedImage,
success_url='gallery_images_list',
template='admin/image_upload.html',
)

register_section(
section_name="Галлерея",
list_route="/gallery_images/",
detail_route="/gallery_image/",
list_view=gallery_images_list,
detail_view=gallery_image_detail,
list_endpoint="gallery_images_list",
)


@admin_app.route("/")
def mainpage():
Expand All @@ -245,26 +271,6 @@ def mainpage():
)


# gallery uploads
@admin_app.route(
"/gallery/upload",
methods=['GET', 'POST'],
)
def upload():
clazz = forms.image_upload_form_factory(config=current_app.config)
form = clazz()
if form.validate_on_submit():
image = request.files['image']
models.UploadedImage.bl.save_image(
image=image,
img_category=models.UploadedImage.IMG_CATEGORY.gallery,
title=form.data['title'],
description=form.data['description'],
)
return redirect(url_for('admin.mainpage'))
return render_template("admin/image_upload.html", form=form)


# noinspection PyUnusedLocal
@admin_app.errorhandler(403)
def handle_forbidden(error):
Expand Down
34 changes: 34 additions & 0 deletions project/templates/admin/gallery_images.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{% extends "admin/base.html" %}

{% block content %}
<table class="table table-stripped">
<thead>
<tr>
<th>Превью</th>
<th>Название</th>
<th>Описание</th>
</thead>
<tbody>
{% for entry in entries %}
<tr>
<td>
<a href="{{ url_for('admin.gallery_image_detail', entry_id=entry.id) }}">
<img class="responsive-image" src="/media/{{ entry.img_category.name }}/thumb/{{ entry.name.hex }}.{{ entry.ext }}"/>
</a>
</td>
<td>
<a href="{{ url_for('admin.gallery_image_detail', entry_id=entry.id) }}">
{{ entry.title }}
</a>
</td>
<td>
{{ entry.description }}
</td>
</tr>
{% endfor %}
</tbody>
</table>

<a href="{{ url_for('admin.gallery_image_detail') }}"
class="btn btn-primary">Загрузить еще картинку</a>
{% endblock %}
4 changes: 2 additions & 2 deletions project/templates/admin/image_upload.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

{% block content %}
<form method="post" enctype="multipart/form-data">
{{ form.hidden_tag() }}
{% for field in form if field.widget.input_type != 'hidden' %}
{{ entry_form.hidden_tag() }}
{% for field in entry_form if field.widget.input_type != 'hidden' %}
{{ field.label }}
{% if field.errors %}
{{field.errors}}
Expand Down
3 changes: 2 additions & 1 deletion project/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
('Страницы', '/admin/pages/'),
('Элементы страниц', '/admin/pagechunks/'),
('Шаблоны писем', '/admin/mail_templates/'),
('Галлерея', '/admin/gallery_images/'),
])


Expand Down Expand Up @@ -62,5 +63,5 @@ def test_all_endpoints_can_be_resolved(self):
for name in expected_sections:
self.assertEqual(
expected_sections[name],
url_for("admin."+SECTIONS[name])
url_for("admin." + SECTIONS[name])
)

0 comments on commit b86f785

Please sign in to comment.