[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, ...) # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os import os
from django.conf import global_settings
BASE_DIR = os.path.dirname(os.path.dirname(__file__)) BASE_DIR = os.path.dirname(os.path.dirname(__file__))
@ -28,6 +29,8 @@ TEMPLATE_DIRS = (
'frontend/templates' 'frontend/templates'
) )
TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + ('django.core.context_processors.request',)
LOGIN_URL = '/login' LOGIN_URL = '/login'
LOGOUT_URL = '/logout' LOGOUT_URL = '/logout'
LOGIN_REDIRECT_URL = '/' LOGIN_REDIRECT_URL = '/'

View File

@ -7,12 +7,14 @@ admin.autodiscover()
from frontend.views import login_wrap, logout_wrap from frontend.views import login_wrap, logout_wrap
from frontend.views import IndexView, ProjectOverView, ProjectView from frontend.views import IndexView, ProjectOverView, ProjectView
from frontend.views import enterProject, leaveProject
urlpatterns = patterns('', urlpatterns = patterns('',
url(r'^$', IndexView.as_view(), name='frontend-index'), url(r'^$', IndexView.as_view(), name='frontend-index'),
url(r'^projects/$', ProjectOverView.as_view(), name='frontend-projects'), 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+)/$', 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'^login/$', login_wrap, name='frontend-login'),
url(r'^logout/$', logout_wrap, name='frontend-logout'), url(r'^logout/$', logout_wrap, name='frontend-logout'),
url(r'^admin/', include(admin.site.urls)), url(r'^admin/', include(admin.site.urls)),

View File

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

View File

@ -2,6 +2,10 @@
{% block head%} {% block head%}
{% load crispy_forms_tags %} {% load crispy_forms_tags %}
{% endblock %} {% 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 %} {% block content %}
<h3>Update Project "{{project.name}}"</h3> <h3>Update Project "{{project.name}}"</h3>
<p>{{project.description}}</p> <p>{{project.description}}</p>

View File

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from django.http import HttpResponse, HttpResponseRedirect
from django.views.generic import TemplateView, FormView, CreateView, UpdateView from django.views.generic import TemplateView, FormView, CreateView, UpdateView
from django.views.generic.detail import SingleObjectMixin from django.views.generic.detail import SingleObjectMixin
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
@ -83,6 +84,27 @@ class ProtectedUpdateView(LoggedInMixin, MyUpdateView):
pass 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 # Actual Views
class IndexView(ProtectedTemplateView): class IndexView(ProtectedTemplateView):
template_name = 'index.html' template_name = 'index.html'
@ -104,12 +126,28 @@ class ProjectOverView(ProtectedFormView):
class ProjectView(ProtectedFormView, SingleObjectMixin): class ProjectView(ProtectedFormView, SingleObjectMixin):
template_name = 'project.html' template_name = 'project.html'
page_title = 'Project Detail View' page_title = 'Project View'
form_class = FileUploadForm form_class = FileUploadForm
slug_field = 'project_id'
slug_url_kwarg = 'id'
object = Project
def get_context_data(self, **kwargs): success_url = '/projects/'
kwargs[u'project'] = self.object
return super(ProjectView, self).get_context_data(**kwargs) 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)