Completed
Push — master ( 2c16e2...28a4a1 )
by Paolo
17s queued 14s
created

zooma.views   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 73
dl 0
loc 122
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A AnnotateViewMixin.post() 0 10 1
A AnnotateViewMixin.get() 0 9 1
A OntologiesReportView.get_context_data() 0 8 1
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
"""
4
Created on Fri Sep 27 15:39:30 2019
5
6
@author: Paolo Cozzi <[email protected]>
7
"""
8
9
import logging
10
11
from django.http import JsonResponse
12
from django.views.generic import TemplateView
13
from django.contrib.auth.mixins import LoginRequiredMixin
14
15
from common.views import AjaxTemplateView
16
from image_app.models import (
17
    missing_terms, DictBreed, DictCountry, DictSpecie, DictUberon,
18
    DictDevelStage, DictPhysioStage)
19
20
from .tasks import (
21
    AnnotateBreeds as AnnotateBreedsTask,
22
    AnnotateCountries as AnnotateCountriesTask,
23
    AnnotateSpecies as AnnotateSpeciesTask,
24
    AnnotateOrganismPart as AnnotateOrganismPartTask,
25
    AnnotateDevelStage as AnnotateDevelStageTask,
26
    AnnotatePhysioStage as AnnotatePhysioStageTask
27
)
28
29
# Get an instance of a logger
30
logger = logging.getLogger(__name__)
31
32
33
class OntologiesReportView(LoginRequiredMixin, TemplateView):
34
    template_name = 'zooma/ontologies_report.html'
35
36
    def get_context_data(self, **kwargs):
37
        # Call the base implementation first to get a context
38
        context = super().get_context_data(**kwargs)
39
40
        # call report from UID model
41
        context["missing_terms"] = missing_terms()
42
43
        return context
44
45
46
class AnnotateViewMixin():
47
    # forcing POST methods only
48
    # https://stackoverflow.com/a/36865283/4385116
49
    http_method_names = ['post', 'get']
50
    task_class = None
51
    task_name = None
52
    dict_class = None
53
54
    def get(self, request):
55
        missing = self.dict_class.objects.filter(term=None).count()
56
        total = self.dict_class.objects.count()
57
58
        return JsonResponse(
59
            {'status': 'OK',
60
             'dict_type': self.dict_class._meta.verbose_name_plural,
61
             'missing': missing,
62
             'total': total})
63
64
    def post(self, request):
65
        task = self.task_class()
66
67
        res = task.delay()
68
69
        logger.info(
70
            "Start %s task %s from user %s" % (
71
                self.task_name, res.task_id, request.user))
72
73
        return JsonResponse({'status': '%s started' % (self.task_name)})
74
75
76
class AnnotateBreedsView(
77
        AnnotateViewMixin, LoginRequiredMixin, AjaxTemplateView):
78
79
    task_class = AnnotateBreedsTask
80
    task_name = "AnnotateBreeds"
81
    dict_class = DictBreed
82
83
84
class AnnotateCountriesView(
85
        AnnotateViewMixin, LoginRequiredMixin, AjaxTemplateView):
86
87
    task_class = AnnotateCountriesTask
88
    task_name = "AnnotateCountries"
89
    dict_class = DictCountry
90
91
92
class AnnotateSpeciesView(
93
        AnnotateViewMixin, LoginRequiredMixin, AjaxTemplateView):
94
95
    task_class = AnnotateSpeciesTask
96
    task_name = "AnnotateSpecies"
97
    dict_class = DictSpecie
98
99
100
class AnnotateOrganismPartView(
101
        AnnotateViewMixin, LoginRequiredMixin, AjaxTemplateView):
102
103
    task_class = AnnotateOrganismPartTask
104
    task_name = "AnnotateOrganismPart"
105
    dict_class = DictUberon
106
107
108
class AnnotateDevelStageView(
109
        AnnotateViewMixin, LoginRequiredMixin, AjaxTemplateView):
110
111
    task_class = AnnotateDevelStageTask
112
    task_name = "AnnotateDevelStage"
113
    dict_class = DictDevelStage
114
115
116
class AnnotatePhysioStageView(
117
        AnnotateViewMixin, LoginRequiredMixin, AjaxTemplateView):
118
119
    task_class = AnnotatePhysioStageTask
120
    task_name = "AnnotatePhysioStage"
121
    dict_class = DictPhysioStage
122