views.py 2.92 KB
Newer Older
1
from django.http.response import HttpResponse, JsonResponse
Guilhem Saurel's avatar
Guilhem Saurel committed
2
from django.views.generic import DetailView
Guilhem Saurel's avatar
initial  
Guilhem Saurel committed
3

Guilhem Saurel's avatar
Guilhem Saurel committed
4
from django_filters.views import FilterView
Guilhem Saurel's avatar
Guilhem Saurel committed
5
from django_tables2 import RequestConfig
Guilhem Saurel's avatar
use ndh    
Guilhem Saurel committed
6
from django_tables2.views import SingleTableMixin, SingleTableView
Guilhem Saurel's avatar
Guilhem Saurel committed
7

Guilhem Saurel's avatar
Guilhem Saurel committed
8
from . import filters, models, tables
Guilhem Saurel's avatar
Guilhem Saurel committed
9
10


Guilhem Saurel's avatar
use ndh    
Guilhem Saurel committed
11
12
13
14
15
16
17
18
class ForgesView(SingleTableView):
    model = models.Forge
    table_class = tables.ForgeTable


class NamespacesView(SingleTableView):
    model = models.Namespace
    table_class = tables.NamespaceTable
Guilhem Saurel's avatar
Guilhem Saurel committed
19
20


Guilhem Saurel's avatar
Guilhem Saurel committed
21
class ProjectsView(SingleTableMixin, FilterView):
Guilhem Saurel's avatar
use ndh    
Guilhem Saurel committed
22
23
    model = models.Project
    table_class = tables.ProjectTable
Guilhem Saurel's avatar
Guilhem Saurel committed
24
    filterset_class = filters.ProjectFilter
Guilhem Saurel's avatar
Guilhem Saurel committed
25
26


Guilhem Saurel's avatar
Guilhem Saurel committed
27
class ProjectView(DetailView):
Guilhem Saurel's avatar
use ndh    
Guilhem Saurel committed
28
    model = models.Project
Guilhem Saurel's avatar
Guilhem Saurel committed
29

Guilhem Saurel's avatar
Guilhem Saurel committed
30

31
32
class ProjectTableView(ProjectView):
    order_by = None
Guilhem Saurel's avatar
Guilhem Saurel committed
33

Guilhem Saurel's avatar
Guilhem Saurel committed
34
35
    def get_context_data(self, **kwargs):
        ctx = super().get_context_data(**kwargs)
36
37
38
39
        object_list = self.get_object_list()
        table = self.table_class(object_list, order_by=self.order_by)
        RequestConfig(self.request).configure(table)
        ctx.update(table=table, object_list=object_list)
Guilhem Saurel's avatar
Guilhem Saurel committed
40
        return ctx
Guilhem Saurel's avatar
Guilhem Saurel committed
41
42


43
44
class ProjectReposView(ProjectTableView):
    table_class = tables.RepoTable
Guilhem Saurel's avatar
Guilhem Saurel committed
45

46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    def get_object_list(self):
        return self.object.repo_set.all()


class ProjectBranchesView(ProjectTableView):
    table_class = tables.BranchTable
    order_by = '-updated'

    def get_object_list(self):
        return self.object.branch_set.all()


class ProjectImagesView(ProjectTableView):
    table_class = tables.ImageTable
    order_by = 'target'

    def get_object_list(self):
        return models.Image.objects.filter(robotpkg__project=self.object)
Guilhem Saurel's avatar
Guilhem Saurel committed
64
65
66
67
68
69
70


class ProjectContributorsView(ProjectTableView):
    table_class = tables.ContributorTable

    def get_object_list(self):
        return self.object.contributors()
Guilhem Saurel's avatar
Guilhem Saurel committed
71

Guilhem Saurel's avatar
Guilhem Saurel committed
72

Guilhem Saurel's avatar
Guilhem Saurel committed
73
74
75
76
class ProjectGitlabView(ProjectView):
    template_name = 'rainboard/gitlab-ci.yml'
    content_type = 'application/x-yaml'

Guilhem Saurel's avatar
Guilhem Saurel committed
77

Guilhem Saurel's avatar
Guilhem Saurel committed
78
79
80
81
82
83
class DistinctMixin(object):
    def get_queryset(self):
        return super().get_queryset().distinct()


class ContributorsView(SingleTableMixin, DistinctMixin, FilterView):
84
    queryset = models.Contributor.objects.filter(projects__from_gepetto=True)
Guilhem Saurel's avatar
Guilhem Saurel committed
85
86
    table_class = tables.ContributorProjectTable
    filterset_class = filters.ContributorFilter
Guilhem Saurel's avatar
Guilhem Saurel committed
87
88
89
90
91
92


def json_doc(request):
    """
    Get the list of project / namespace / branch of which we want to keep the doc
    """
Guilhem Saurel's avatar
Guilhem Saurel committed
93
94
    return JsonResponse({'ret': [(b.project.slug, b.repo.namespace.slug, b.name.split('/', maxsplit=2)[2])
                                 for b in models.Branch.objects.filter(keep_doc=True)]})
95
96
97


def docker(request):
98
    cmd = 'build'
99
    filters = request.GET.dict()
100
101
    if 'cmd' in filters and filters['cmd'] in ['push', 'pull', 'build']:
        cmd = filters.pop('cmd')
102
    images = models.Image.objects.filter(**filters)
103
    return HttpResponse('\n'.join([' '.join(getattr(image, cmd)()) for image in images]), content_type="text/plain")