views.py 3.54 KB
Newer Older
Guilhem Saurel's avatar
Guilhem Saurel committed
1
2
from subprocess import PIPE, run

3
from django.http.response import HttpResponse, JsonResponse
Guilhem Saurel's avatar
Guilhem Saurel committed
4
from django.views.generic import DetailView
Guilhem Saurel's avatar
initial  
Guilhem Saurel committed
5

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

Guilhem Saurel's avatar
Guilhem Saurel committed
10
from . import filters, models, tables
Guilhem Saurel's avatar
Guilhem Saurel committed
11
12


Guilhem Saurel's avatar
use ndh    
Guilhem Saurel committed
13
14
15
16
17
18
19
20
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
21
22


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


Guilhem Saurel's avatar
Guilhem Saurel committed
29
30
31
32
class GepettoProjectsView(ProjectsView):
    queryset = models.Project.objects.filter(from_gepetto=True)


Guilhem Saurel's avatar
Guilhem Saurel committed
33
class ProjectView(DetailView):
Guilhem Saurel's avatar
use ndh    
Guilhem Saurel committed
34
    model = models.Project
Guilhem Saurel's avatar
Guilhem Saurel committed
35

Guilhem Saurel's avatar
Guilhem Saurel committed
36

37
38
class ProjectTableView(ProjectView):
    order_by = None
Guilhem Saurel's avatar
Guilhem Saurel committed
39

Guilhem Saurel's avatar
Guilhem Saurel committed
40
41
    def get_context_data(self, **kwargs):
        ctx = super().get_context_data(**kwargs)
42
43
44
45
        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
46
        return ctx
Guilhem Saurel's avatar
Guilhem Saurel committed
47
48


49
50
class ProjectReposView(ProjectTableView):
    table_class = tables.RepoTable
Guilhem Saurel's avatar
Guilhem Saurel committed
51

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    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):
Guilhem Saurel's avatar
Guilhem Saurel committed
69
        return models.Image.objects.filter(robotpkg__project=self.object, target__active=True)
Guilhem Saurel's avatar
Guilhem Saurel committed
70
71
72
73
74
75
76


class ProjectContributorsView(ProjectTableView):
    table_class = tables.ContributorTable

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

Guilhem Saurel's avatar
Guilhem Saurel committed
78

Guilhem Saurel's avatar
Guilhem Saurel committed
79
80
81
82
class ProjectGitlabView(ProjectView):
    template_name = 'rainboard/gitlab-ci.yml'
    content_type = 'application/x-yaml'

Guilhem Saurel's avatar
Guilhem Saurel committed
83

Guilhem Saurel's avatar
Guilhem Saurel committed
84
85
86
87
88
89
class DistinctMixin(object):
    def get_queryset(self):
        return super().get_queryset().distinct()


class ContributorsView(SingleTableMixin, DistinctMixin, FilterView):
90
    queryset = models.Contributor.objects.gepettist()
Guilhem Saurel's avatar
Guilhem Saurel committed
91
92
    table_class = tables.ContributorProjectTable
    filterset_class = filters.ContributorFilter
Guilhem Saurel's avatar
Guilhem Saurel committed
93
94
95
96
97
98


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
99
100
    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)]})
101
102
103


def docker(request):
104
    cmd = 'build'
105
    filters = request.GET.dict()
106
107
    if 'cmd' in filters and filters['cmd'] in ['push', 'pull', 'build']:
        cmd = filters.pop('cmd')
108
    images = models.Image.objects.filter(**filters)
109
    return HttpResponse('\n'.join([' '.join(getattr(image, cmd)()) for image in images]), content_type="text/plain")
110
111
112



Guilhem Saurel's avatar
Guilhem Saurel committed
113
def graph_svg(request):
114
115
    with open('/tmp/graph', 'w') as f:
        print('digraph {', file=f)
Guilhem Saurel's avatar
Guilhem Saurel committed
116
        for project in models.Project.objects.all():
117
            print(f'{{I{project.pk} [label="{project}"];}}', file=f)
Guilhem Saurel's avatar
Guilhem Saurel committed
118
        for dep in models.Dependency.objects.all():
119
120
            print(f'I{dep.project.pk} -> I{dep.library.pk};', file=f)
        print('}', file=f)
Guilhem Saurel's avatar
Guilhem Saurel committed
121
    svg = run(['dot', '/tmp/graph', '-Tsvg'], stdout=PIPE).stdout.decode()
122
    return HttpResponse(svg, content_type='image/svg+xml')