Project

General

Profile

Download (2.86 KB) Statistics
| Branch: | Tag: | Revision:

calebasse / calebasse / ressources / views.py @ dd986559

1
from django.db import models
2
from django.http import Http404
3
from django.shortcuts import render, get_object_or_404
4

    
5
from calebasse.cbv import (ListView, CreateView, UpdateView, DeleteView,
6
        ReturnToObjectMixin)
7
from calebasse.ressources.models import Service
8

    
9

    
10
_models = None
11

    
12

    
13
def get_ressource_model(model_name):
14
    global _models
15
    if _models is None:
16
        _models = models.get_models()
17
    for model in _models:
18
        meta = model._meta
19
        if meta.module_name == model_name and \
20
                (meta.app_label == 'ressources' or meta.app_label == 'personnes'):
21
            return model
22
    return None
23

    
24

    
25
def homepage(request, service):
26
    global _models
27
    if _models is None:
28
        _models = models.get_models()
29
    ressources_models = [
30
            (model._meta.verbose_name_plural, model._meta.module_name)
31
            for model in _models
32
            if model._meta.app_label == 'ressources' ]
33
    service_name = get_object_or_404(Service, slug=service)
34
    return render(request, 'ressources/index.html',
35
            dict(models=sorted(ressources_models),
36
                service=service,
37
                service_name=service_name))
38

    
39

    
40
def list_view(request, service, model_name):
41
    model = get_ressource_model(model_name)
42
    if model is None:
43
        raise Http404
44
    view = ListView.as_view(model=model,
45
            queryset=model.objects.select_related(),
46
            template_name='ressources/list.html')
47
    return view(request, service=service)
48

    
49
class RessourceCreateView(CreateView):
50
    template_name="ressources/new.html"
51
    template_name_suffix='_new'
52

    
53
    def get_initial(self):
54
        initial = super(RessourceCreateView, self).get_initial()
55
        initial['service'] = self.service
56
        return initial
57

    
58
    def get_success_url(self):
59
        if self.request.GET.has_key('next_url'):
60
            return self.request.GET['next_url']
61
        else:
62
            return '../?new_id=%s' % self.object.id
63

    
64
def create_view(request, service, model_name):
65
    model = get_ressource_model(model_name)
66
    if model is None:
67
        raise Http404
68
    view = RessourceCreateView.as_view(model=model)
69
    return view(request, service=service)
70

    
71
class RessourceUpdateView(ReturnToObjectMixin, UpdateView):
72
    template_name='ressources/update.html'
73
    template_name_suffix='_update'
74

    
75
def update_view(request, service, model_name, pk):
76
    model = get_ressource_model(model_name)
77
    if model is None:
78
        raise Http404
79
    view = RessourceUpdateView.as_view(model=model)
80
    return view(request, pk=pk, service=service)
81

    
82

    
83
def delete_view(request, service, model_name, pk):
84
    model = get_ressource_model(model_name)
85
    if model is None:
86
        raise Http404
87
    view = DeleteView.as_view(model=model,
88
            success_url='../../',
89
            template_name='ressources/delete.html',
90
            template_name_suffix='_delete')
91
    return view(request, pk=pk, service=service)
(7-7/7)