[TASK] Add basic project entering/leaving support.

This commit is contained in:
Jan Philipp Timme 2014-08-29 13:55:43 +02:00
parent 6086ea112b
commit c9eb70e3da
5 changed files with 68 additions and 19 deletions

View File

@ -12,6 +12,7 @@ https://docs.djangoproject.com/en/1.6/ref/settings/
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
from django.conf import global_settings
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
@ -28,6 +29,8 @@ TEMPLATE_DIRS = (
'frontend/templates'
)
TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + ('django.core.context_processors.request',)
LOGIN_URL = '/login'
LOGOUT_URL = '/logout'
LOGIN_REDIRECT_URL = '/'

View File

@ -7,12 +7,14 @@ admin.autodiscover()
from frontend.views import login_wrap, logout_wrap
from frontend.views import IndexView, ProjectOverView, ProjectView
from frontend.views import enterProject, leaveProject
urlpatterns = patterns('',
url(r'^$', IndexView.as_view(), name='frontend-index'),
url(r'^projects/$', ProjectOverView.as_view(), name='frontend-projects'),
url(r'^projects/(?P<project_id>\d+)/$', ProjectView.as_view(), name='frontend-project-detail'),
url(r'^projects/(?P<project_id>\d+)/enter/$', enterProject, name='frontend-enter-project-detail'),
url(r'^projects/(?P<project_id>\d+)/leave/$', leaveProject, name='frontend-leave-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)),

View File

@ -23,16 +23,18 @@
<div class="navbar-header">
<a class="navbar-brand" href="/"><img src="{% static 'images/logo.png' %}"> {{page.name}}</a>
<ul class="nav navbar-nav">
<li><a href="/">Index</a></li>
<li><a href="/register">Register</a></li>
<li>
{% if user.is_anonymous %}
<a href="/login">Login</a>
{% else %}
<a href="/logout">Logout</a>
{% endif %}
</li>
</ul>
{% block navbar-header %}
<li><a href="/">Index</a></li>
<li><a href="/register">Register</a></li>
<li>
{% if user.is_anonymous %}
<a href="/login">Login</a>
{% else %}
<a href="/logout">Logout</a>
{% endif %}
</li>
{% endblock %}
</ul>
</div>
</div>
</nav>

View File

@ -2,6 +2,10 @@
{% block head%}
{% load crispy_forms_tags %}
{% endblock %}
{% block navbar-header %}
{{block.super}}
<li><a href="{% url 'frontend-leave-project-detail' request.session.project_id %}">Leave Project</a></li>
{% endblock %}
{% block content %}
<h3>Update Project "{{project.name}}"</h3>
<p>{{project.description}}</p>

View File

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from django.http import HttpResponse, HttpResponseRedirect
from django.views.generic import TemplateView, FormView, CreateView, UpdateView
from django.views.generic.detail import SingleObjectMixin
from django.contrib.auth.decorators import login_required
@ -19,7 +20,7 @@ def login_wrap(*args, **kwargs):
'page': {
'name': FRONTEND_PAGE_NAME,
'title': 'Login'
}
}
}
return login(*args, **kwargs)
@ -83,6 +84,27 @@ class ProtectedUpdateView(LoggedInMixin, MyUpdateView):
pass
# Helper view methods
def enterProject(request, project_id=None):
try:
if request.session['project_id'] != project_id:
return HttpResponse("Please leave your current project - Project ID " + request.session['project_id'] + " is still active.")
except KeyError:
pass
request.session['project_id'] = project_id
return HttpResponseRedirect('/projects/' + project_id + '/')
def leaveProject(request, project_id=None):
try:
if request.session['project_id'] == project_id:
del request.session['project_id']
else:
return HttpResponse("You tried to leave project with ID " + project_id + ", but your active project id is " + request.session['project_id'] + ".")
except KeyError:
pass
return HttpResponseRedirect('/projects/')
# Actual Views
class IndexView(ProtectedTemplateView):
template_name = 'index.html'
@ -104,12 +126,28 @@ class ProjectOverView(ProtectedFormView):
class ProjectView(ProtectedFormView, SingleObjectMixin):
template_name = 'project.html'
page_title = 'Project Detail View'
page_title = 'Project 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)
success_url = '/projects/'
def get(self, request, *args, **kwargs):
project_id = kwargs[u'project_id']
self.object = Project.objects.get(pk=project_id)
return super(ProjectView, self).get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
project_id = kwargs[u'project_id']
self.success_url = self.success_url + project_id
return super(ProjectView, self).post(request, *args, **kwargs)
def form_valid(self, form, *args, **kwargs):
print("FORM:")
attrs = vars(form)
print ', '.join("%s: %s" % item for item in attrs.items())
return super(ProjectView, self).form_valid(form)