views.py 6.44 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
drf    
Guilhem Saurel committed
9
from rest_framework import permissions, viewsets
Guilhem Saurel's avatar
Guilhem Saurel committed
10

Guilhem Saurel's avatar
drf    
Guilhem Saurel committed
11
from . import filters, models, serializers, tables
Guilhem Saurel's avatar
Guilhem Saurel committed
12
13


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


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


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


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

Guilhem Saurel's avatar
Guilhem Saurel committed
37

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

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


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

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


class ProjectContributorsView(ProjectTableView):
    table_class = tables.ContributorTable

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

Guilhem Saurel's avatar
Guilhem Saurel committed
79

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

Guilhem Saurel's avatar
Guilhem Saurel committed
84

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


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


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


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


Guilhem Saurel's avatar
Guilhem Saurel committed
113
def graph_svg(request):
114
    with open('/tmp/graph', 'w') as f:
Guilhem Saurel's avatar
Guilhem Saurel committed
115
        print('digraph { rankdir=LR;', file=f)
Guilhem Saurel's avatar
Guilhem Saurel committed
116
        for project in models.Project.objects.filter(from_gepetto=True):
Guilhem Saurel's avatar
Guilhem Saurel committed
117
            print(f'{{I{project.pk} [label="{project}" URL="{project.get_absolute_url()}"];}}', file=f)
118
        for dep in models.Dependency.objects.filter(project__from_gepetto=True, library__from_gepetto=True):
Guilhem Saurel's avatar
Guilhem Saurel committed
119
            print(f'I{dep.library.pk} -> I{dep.project.pk};', file=f)
120
        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')
Guilhem Saurel's avatar
drf    
Guilhem Saurel committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200


class NamespaceViewSet(viewsets.ModelViewSet):
    queryset = models.Namespace.objects.all()
    serializer_class = serializers.NamespaceSerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)


class LicenseViewSet(viewsets.ModelViewSet):
    queryset = models.License.objects.all()
    serializer_class = serializers.LicenseSerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)


class ForgeViewSet(viewsets.ModelViewSet):
    queryset = models.Forge.objects.all()
    serializer_class = serializers.ForgeSerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)


class ProjectViewSet(viewsets.ModelViewSet):
    queryset = models.Project.objects.all()
    serializer_class = serializers.ProjectSerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)


class RepoViewSet(viewsets.ModelViewSet):
    queryset = models.Repo.objects.all()
    serializer_class = serializers.RepoSerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)


class BranchViewSet(viewsets.ModelViewSet):
    queryset = models.Branch.objects.all()
    serializer_class = serializers.BranchSerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)


class TargetViewSet(viewsets.ModelViewSet):
    queryset = models.Target.objects.all()
    serializer_class = serializers.TargetSerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)


class RobotpkgViewSet(viewsets.ModelViewSet):
    queryset = models.Robotpkg.objects.all()
    serializer_class = serializers.RobotpkgSerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)


class ImageViewSet(viewsets.ModelViewSet):
    queryset = models.Image.objects.all()
    serializer_class = serializers.ImageSerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)


class ContributorViewSet(viewsets.ModelViewSet):
    queryset = models.Contributor.objects.all()
    serializer_class = serializers.ContributorSerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)


class ContributorNameViewSet(viewsets.ModelViewSet):
    queryset = models.ContributorName.objects.all()
    serializer_class = serializers.ContributorNameSerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)


class ContributorMailViewSet(viewsets.ModelViewSet):
    queryset = models.ContributorMail.objects.all()
    serializer_class = serializers.ContributorMailSerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)


class DependencyViewSet(viewsets.ModelViewSet):
    queryset = models.Dependency.objects.all()
    serializer_class = serializers.DependencySerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)