Blame view

src/dashboard/views.py 6.84 KB
76dfa189   Unknown   Adding devices lo...
1
from django.http import HttpResponse
81847ba2   haribo   Dashboard buttons...
2
from django.shortcuts import render, redirect
5f148e7d   Unknown   Update to django2...
3

94082e77   haribo   Date: 03/06/2016
4
from django.contrib.auth.decorators import login_required
76dfa189   Unknown   Adding devices lo...
5
import datetime
e564e13d   theopuhl   Add merging file
6
from common.models import Log, WeatherWatch, SiteWatch, ScientificProgram, Config, PyrosUser
76dfa189   Unknown   Adding devices lo...
7
from django.core import serializers
bca9a283   Jeremy   Reworked the sche...
8
import utils.Logger as l
e564e13d   theopuhl   Add merging file
9
10
11
from django.forms import modelformset_factory
from dashboard.forms import ConfigForm, UsersForm
from dashboard.decorator import superuser_only
e31b2208   theophile.puhl@epitech.eu   Severals Changes ...
12
13
14
from django.views.generic.edit import UpdateView
from django.shortcuts import get_object_or_404
from django.utils.decorators import method_decorator
5f148e7d   Unknown   Update to django2...
15
16
from django.urls import reverse_lazy, reverse
from django.http import Http404
e31b2208   theophile.puhl@epitech.eu   Severals Changes ...
17

6c2793c2   jeremy   Update
18
log = l.setupLogger("dashboard", "dashboard")
6bba7ccd   Jeremy   Fix Django 1.10.2...
19

76dfa189   Unknown   Adding devices lo...
20
21


94082e77   haribo   Date: 03/06/2016
22
@login_required
6dc0b213   theophile.puhl@epitech.eu   Création du dashb...
23
def index(request):
e31b2208   theophile.puhl@epitech.eu   Severals Changes ...
24
    return render(request, 'dashboard/index.html')                              # return the initial view (the dashboard's one)
6dc0b213   theophile.puhl@epitech.eu   Création du dashb...
25

76dfa189   Unknown   Adding devices lo...
26
27
28
29
30
31
#@login_required
#def observation_status(request):
 #   return render(request, 'dashboard/observation_status.html')

@login_required
def retrieve_env(request):
5f148e7d   Unknown   Update to django2...
32
33
34
35
36
37
38
39
40
41
    try:
        weather_status = WeatherWatch.objects.latest('updated')
        t = datetime.datetime.now() + datetime.timedelta(hours=-7) #temporary method to demonstrate the day/night display
        isDay = False
        if t.hour > 5 and t.hour < 20:
            isDay = True
        return render(request, 'dashboard/observation_status_env.html', locals())
    except WeatherWatch.DoesNotExist:
            raise Http404("No WeatherWatch matches the given query.")
    
76dfa189   Unknown   Adding devices lo...
42
43
44
45

@login_required
def retrieve_env_navbar(request):
    if request.is_ajax():
5f148e7d   Unknown   Update to django2...
46
47
48
49
50
51
        try:
            weather_status = WeatherWatch.objects.latest('updated')
            weather = serializers.serialize('json', [weather_status])
            return HttpResponse(weather, content_type="application/json")
        except WeatherWatch.DoesNotExist:
            raise Http404("No WeatherWatch matches the given query.")
76dfa189   Unknown   Adding devices lo...
52

6dc0b213   theophile.puhl@epitech.eu   Création du dashb...
53
@login_required
62229d51   haribo   began dashboard (...
54
def users(request):
e564e13d   theopuhl   Add merging file
55
56
    instance = PyrosUser.objects.order_by("-id")
    return render(request, 'dashboard/users_management.html', {'instance': instance})                     # return the initial view (the users management's one) 
62229d51   haribo   began dashboard (...
57

94082e77   haribo   Date: 03/06/2016
58
@login_required
62229d51   haribo   began dashboard (...
59
def routines(request):
5f148e7d   Unknown   Update to django2...
60
    url_ = reverse('admin:common_request_changelist')
81847ba2   haribo   Dashboard buttons...
61
    return redirect(url_)
62229d51   haribo   began dashboard (...
62

77816f10   haribo   Workflow implemen...
63

94082e77   haribo   Date: 03/06/2016
64
@login_required
62229d51   haribo   began dashboard (...
65
def weather(request):
e31b2208   theophile.puhl@epitech.eu   Severals Changes ...
66
67
68
69
    return render(request, 'dashboard/reload_weather.html')                                                                     # return the needed html file

@login_required
def weather_current(request):
661d7672   theopuhl   Unittest Fix
70
71
72
73
74
75
76
77
78
79
80
81
    try:
        if (len(Config.objects.all()) == 1):
            monitoring = int(int(Config.objects.get(id=1).row_data_save_frequency) / 5)
        else:
            monitoring = 60
        if (len(WeatherWatch.objects.all()) > 0):
            weather_info = WeatherWatch.objects.order_by("-id")[:monitoring]                                                            # Use 300 seconds by default with an iteration every 5 seconds                                                                                     # Get the number of data available
        else:                                                                                                                           
            weather_info = None
        return render(request, 'dashboard/current_weather.html', {'weather_info' : weather_info, 'iteration' : monitoring})
    except Config.DoesNotExist:
        return render(request, 'dashboard/current_weather.html', {'weather_info' : None, 'iteration' : 60})
62229d51   haribo   began dashboard (...
82

77816f10   haribo   Workflow implemen...
83

94082e77   haribo   Date: 03/06/2016
84
@login_required
62229d51   haribo   began dashboard (...
85
def site(request):
e31b2208   theophile.puhl@epitech.eu   Severals Changes ...
86
87
88
89
    return render(request, 'dashboard/reload_site.html')                        # return the needed html file

@login_required
def site_current(request):
661d7672   theopuhl   Unittest Fix
90
91
92
93
94
95
96
97
98
99
100
101
    try:
        if (len(Config.objects.all()) == 1):
            monitoring = int(int(Config.objects.get(id=1).row_data_save_frequency) / 5)
        else:
            monitoring = 60
        if (len(SiteWatch.objects.all()) > 0):
            site_info = SiteWatch.objects.order_by("-id")[:monitoring]                                                               
        else:                                                                                                                           
            site_info = None
        return render(request, 'dashboard/current_site.html', {'site_info' : site_info, 'iteration' : monitoring})
    except Config.DoesNotExist:
        return render(request, 'dashboard/current_site.html', {'site_info' : None, 'iteration' : 60})
e31b2208   theophile.puhl@epitech.eu   Severals Changes ...
102
103
104
105
106
107
108
109
110
111

@login_required
def proposal(request):
    if (len(ScientificProgram.objects.all()) > 0):                              # checking if the observatory table is empty
        proposal_info = ScientificProgram.objects.order_by("-id")[:100]         # Sorting Weather table
        nb_info_proposal = len(proposal_info)                                   # Get the number of data available

    else:                                                                       # if empty set everything to 0 / None (variables are checked in src/templates/scheduler/current_weather.html)
        proposal_info = None
        nb_info_proposal = 0
62229d51   haribo   began dashboard (...
112

e31b2208   theophile.puhl@epitech.eu   Severals Changes ...
113
114
115
    return render(request, 'dashboard/proposal.html', {'proposal_info' : proposal_info, 'nb_info_proposal' : nb_info_proposal})

@login_required
e564e13d   theopuhl   Add merging file
116
@superuser_only
e31b2208   theophile.puhl@epitech.eu   Severals Changes ...
117
118
119
120
121
122
123
124
def configUpdate(request):
    instance = get_object_or_404(Config, id=1)
    form = ConfigForm(request.POST or None, instance=instance)
    if form.is_valid():
        form.save()
        return redirect('../user_manager/profile')
    return render(request, 'dashboard/configuration.html', {'form': form}) 
    
77816f10   haribo   Workflow implemen...
125

94082e77   haribo   Date: 03/06/2016
126
@login_required
62229d51   haribo   began dashboard (...
127
def devices(request):
5f148e7d   Unknown   Update to django2...
128
    url_ = reverse('admin:common_device_changelist')
81847ba2   haribo   Dashboard buttons...
129
    return redirect(url_)
62229d51   haribo   began dashboard (...
130

94082e77   haribo   Date: 03/06/2016
131
@login_required
62229d51   haribo   began dashboard (...
132
133
def system(request):
    return render(request, 'dashboard/system.html')
61e60444   Jeremy   Pushing on dev
134

94082e77   haribo   Date: 03/06/2016
135
@login_required
77816f10   haribo   Workflow implemen...
136
def system_retrieve_logs(request):
94082e77   haribo   Date: 03/06/2016
137
    '''
e39312f1   Jeremy   Fix bug on schedu...
138
        Called by the dashboard system page with ajax request every seconds, to get the logs and print them
94082e77   haribo   Date: 03/06/2016
139
    '''
77816f10   haribo   Workflow implemen...
140
    if request.is_ajax():
77816f10   haribo   Workflow implemen...
141
142
143
144
145
        alert_logs = Log.objects.filter(agent='Alert manager')
        scheduler_logs = Log.objects.filter(agent='Scheduler')
        majordome_logs = Log.objects.filter(agent='Majordome')
        obs_logs = Log.objects.filter(agent='Observation manager')
        analyzer_logs = Log.objects.filter(agent='Analyzer')
bca9a283   Jeremy   Reworked the sche...
146
        monitoring_logs = Log.objects.filter(agent='Monitoring')
77816f10   haribo   Workflow implemen...
147
        return render(request, 'dashboard/system_logs.html', locals())
77816f10   haribo   Workflow implemen...
148
149


94082e77   haribo   Date: 03/06/2016
150
@login_required
62229d51   haribo   began dashboard (...
151
def schedule(request):
5f148e7d   Unknown   Update to django2...
152
    url_ = reverse('admin:common_schedule_changelist')
81847ba2   haribo   Dashboard buttons...
153
    return redirect(url_)
62229d51   haribo   began dashboard (...
154

77816f10   haribo   Workflow implemen...
155

94082e77   haribo   Date: 03/06/2016
156
@login_required
62229d51   haribo   began dashboard (...
157
def quotas(request):
5f148e7d   Unknown   Update to django2...
158
    url_ = reverse('admin:common_pyrosuser_changelist')
81847ba2   haribo   Dashboard buttons...
159
    return redirect(url_)
e31b2208   theophile.puhl@epitech.eu   Severals Changes ...
160

e564e13d   theopuhl   Add merging file
161
@login_required
e31b2208   theophile.puhl@epitech.eu   Severals Changes ...
162
def change_globalMode(request):
661d7672   theopuhl   Unittest Fix
163
164
165
166
167
168
169
    try :
        config = get_object_or_404(Config, id=1)
        config.global_mode = not config.global_mode
        config.save()
        return redirect('index')
    except Config.DoesNotExist:
        return redirect('index')