From 6086ea112b82135a56c485ebf1596d9014d7b247 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Mon, 11 Aug 2014 16:01:03 +0200 Subject: [PATCH] [TASK] Add another view with a fileupload form. --- citavi_mapper/settings.py | 1 + citavi_mapper/urls.py | 5 +++-- frontend/admin.py | 4 ++-- frontend/forms.py | 19 +++++++++++++++++-- frontend/models.py | 4 +++- frontend/templates/project.html | 10 ++++++++++ frontend/templates/projects.html | 31 ++++++++++++++++++++++++++----- frontend/views.py | 26 +++++++++++++++++++++++--- 8 files changed, 85 insertions(+), 15 deletions(-) create mode 100644 frontend/templates/project.html diff --git a/citavi_mapper/settings.py b/citavi_mapper/settings.py index b0117c6..bfc91c3 100644 --- a/citavi_mapper/settings.py +++ b/citavi_mapper/settings.py @@ -47,6 +47,7 @@ INSTALLED_APPS = ( 'bootstrap3_datetime', 'django_bootstrap_breadcrumbs', 'frontend', + 'debug_toolbar' ) MIDDLEWARE_CLASSES = ( diff --git a/citavi_mapper/urls.py b/citavi_mapper/urls.py index 8164945..0f04b05 100644 --- a/citavi_mapper/urls.py +++ b/citavi_mapper/urls.py @@ -6,12 +6,13 @@ from django.contrib import admin admin.autodiscover() from frontend.views import login_wrap, logout_wrap -from frontend.views import IndexView, ProjectView +from frontend.views import IndexView, ProjectOverView, ProjectView urlpatterns = patterns('', url(r'^$', IndexView.as_view(), name='frontend-index'), - url(r'^projects/$', ProjectView.as_view(), name='frontend-projects'), + url(r'^projects/$', ProjectOverView.as_view(), name='frontend-projects'), + url(r'^projects/(?P\d+)/$', ProjectView.as_view(), name='frontend-project-detail'), url(r'^login/$', login_wrap, name='frontend-login'), url(r'^logout/$', logout_wrap, name='frontend-logout'), url(r'^admin/', include(admin.site.urls)), diff --git a/frontend/admin.py b/frontend/admin.py index 4155877..2bf7803 100644 --- a/frontend/admin.py +++ b/frontend/admin.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- from django.contrib import admin -from frontend.models import ProjectContext +from frontend.models import Project -admin.site.register(ProjectContext) \ No newline at end of file +admin.site.register(Project) \ No newline at end of file diff --git a/frontend/forms.py b/frontend/forms.py index 5239d29..e78680b 100644 --- a/frontend/forms.py +++ b/frontend/forms.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- from django import forms -from frontend.models import ProjectContext +from frontend.models import Project from crispy_forms.helper import FormHelper from crispy_forms.layout import Submit, Layout @@ -31,5 +31,20 @@ class NewProjectForm(forms.ModelForm): ) class Meta: - model = ProjectContext + model = Project fields = ['name', 'description'] + +class FileUploadForm(forms.Form): + def __init__(self, *args, **kwargs): + super(FileUploadForm, self).__init__(*args, **kwargs) + self.helper = FormHelper() + self.helper.form_class = 'form-horizontal' + self.helper.label_class = 'col-lg-2' + self.helper.field_class = 'col-lg-4' + self.helper.form_method = 'post' + self.helper.layout = Layout( + 'file', + Submit('send', 'Upload', css_class = 'btn-default') + ) + + file = forms.FileField() \ No newline at end of file diff --git a/frontend/models.py b/frontend/models.py index 6c04a1c..a2d09a0 100644 --- a/frontend/models.py +++ b/frontend/models.py @@ -2,7 +2,7 @@ from django.db import models -class ProjectContext(models.Model): +class Project(models.Model): name = models.CharField(max_length=255) description = models.TextField() associated_filename = models.CharField(max_length=255,blank=True,null=True) @@ -11,4 +11,6 @@ class ProjectContext(models.Model): repr = self.name if self.associated_filename: repr += " (" + self.associated_filename + ")" + else: + repr += " (Empty)" return repr \ No newline at end of file diff --git a/frontend/templates/project.html b/frontend/templates/project.html new file mode 100644 index 0000000..b6619c4 --- /dev/null +++ b/frontend/templates/project.html @@ -0,0 +1,10 @@ +{% extends "layout/base.html" %} +{% block head%} + {% load crispy_forms_tags %} +{% endblock %} +{% block content %} +

Update Project "{{project.name}}"

+

{{project.description}}

+

{% crispy form %}

+ +{% endblock %} \ No newline at end of file diff --git a/frontend/templates/projects.html b/frontend/templates/projects.html index fb44a90..1817f2f 100644 --- a/frontend/templates/projects.html +++ b/frontend/templates/projects.html @@ -3,10 +3,31 @@ {% load crispy_forms_tags %} {% endblock %} {% block content %} -

Create a new Project Context

+

Create a new Project

{% crispy form %}

-

Existing Contexts

-

- TABLE HERE -

+

Existing Projects

+ {% if not projects %} +

No projects to find here. Maybe go ahead and create one?

+ {% else %} + + + + + + + + + + + {% for project in projects %} + + + + + + + {% endfor %} + +
IDNameDescriptionFeatures
{{project.id}}{{project.name}}{{project.description}}Enter project
+ {% endif %} {% endblock %} \ No newline at end of file diff --git a/frontend/views.py b/frontend/views.py index a5f595b..30f2cb8 100644 --- a/frontend/views.py +++ b/frontend/views.py @@ -2,11 +2,14 @@ from django.views.generic import TemplateView, FormView, CreateView, UpdateView +from django.views.generic.detail import SingleObjectMixin from django.contrib.auth.decorators import login_required from django.utils.decorators import method_decorator from django.contrib.auth.views import login, logout -from frontend.forms import NewProjectForm +from frontend.forms import NewProjectForm, FileUploadForm + +from frontend.models import Project FRONTEND_PAGE_NAME = 'Citavi Mapper' @@ -85,11 +88,28 @@ class IndexView(ProtectedTemplateView): template_name = 'index.html' page_title = 'Index' -class ProjectView(ProtectedFormView): +class ProjectOverView(ProtectedFormView): template_name = 'projects.html' page_title = 'Projects' form_class = NewProjectForm success_url = '/projects' + def get_context_data(self, **kwargs): + kwargs[u'projects'] = Project.objects.all() + return super(ProjectOverView, self).get_context_data(**kwargs) + def form_valid(self, form): - return super(ProjectView, self).form_valid(form) + form.save() + return super(ProjectOverView, self).form_valid(form) + +class ProjectView(ProtectedFormView, SingleObjectMixin): + template_name = 'project.html' + page_title = 'Project Detail View' + form_class = FileUploadForm + slug_field = 'project_id' + slug_url_kwarg = 'id' + object = Project + + def get_context_data(self, **kwargs): + kwargs[u'project'] = self.object + return super(ProjectView, self).get_context_data(**kwargs)