Blame view

src/dashboard/views.py 9.95 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
from django.forms import modelformset_factory
2c61f856   theopuhl   Url change to pat...
10
from dashboard.forms import ConfigForm, UserForm
e564e13d   theopuhl   Add merging file
11
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
ba1d1f2b   Quentin Durand   implémentation Fr...
17
18
19
import json
from django.views.decorators.csrf import requires_csrf_token

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

76dfa189   Unknown   Adding devices lo...
22
23


94082e77   haribo   Date: 03/06/2016
24
@login_required
6dc0b213   theophile.puhl@epitech.eu   Création du dashb...
25
def index(request):
2c61f856   theopuhl   Url change to pat...
26
27
28
    if request.user.is_authenticated:
        return render(request, 'dashboard/index.html', {'level': request.user.user_level.priority, 'base_template' : "base.html"})                              # return the initial view (the dashboard's one)
    return render(request, 'dashboard/index.html', {'level': request.user.user_level.priority, 'base_template' : "base_unlogged.html"})                              # return the initial view (the dashboard's one)
6dc0b213   theophile.puhl@epitech.eu   Création du dashb...
29

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

76dfa189   Unknown   Adding devices lo...
34
def retrieve_env(request):
5f148e7d   Unknown   Update to django2...
35
36
37
38
39
40
    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
2c61f856   theopuhl   Url change to pat...
41
        return render(request, 'dashboard/observation_status_env.html', )
5f148e7d   Unknown   Update to django2...
42
43
    except WeatherWatch.DoesNotExist:
            raise Http404("No WeatherWatch matches the given query.")
76dfa189   Unknown   Adding devices lo...
44

76dfa189   Unknown   Adding devices lo...
45
46
def retrieve_env_navbar(request):
    if request.is_ajax():
5f148e7d   Unknown   Update to django2...
47
48
49
50
51
52
        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...
53

6dc0b213   theophile.puhl@epitech.eu   Création du dashb...
54
@login_required
62229d51   haribo   began dashboard (...
55
def users(request):
e564e13d   theopuhl   Add merging file
56
57
    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 (...
58

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

62229d51   haribo   began dashboard (...
64
def weather(request):
2c61f856   theopuhl   Url change to pat...
65
66
67
    if request.user.is_authenticated:
        return render(request, 'dashboard/reload_weather.html', {'base_template' : "base.html"})                                                                     # return the needed html file
    return render(request, 'dashboard/reload_weather.html', {'base_template' : "base_unlogged.html"})
e31b2208   theophile.puhl@epitech.eu   Severals Changes ...
68

e31b2208   theophile.puhl@epitech.eu   Severals Changes ...
69
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

62229d51   haribo   began dashboard (...
83
def site(request):
2c61f856   theopuhl   Url change to pat...
84
85
86
    if request.user.is_authenticated:
        return render(request, 'dashboard/reload_site.html', {'base_template' : "base.html"})                        # return the needed html file
    return render(request, 'dashboard/reload_site.html', {'base_template' : "base_unlogged.html"})                        # return the needed html file
e31b2208   theophile.puhl@epitech.eu   Severals Changes ...
87

e31b2208   theophile.puhl@epitech.eu   Severals Changes ...
88
def site_current(request):
661d7672   theopuhl   Unittest Fix
89
90
91
92
93
94
95
96
97
98
99
100
    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 ...
101
102
103
104
105
106
107
108
109
110

@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 (...
111

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

@login_required
e564e13d   theopuhl   Add merging file
115
@superuser_only
e31b2208   theophile.puhl@epitech.eu   Severals Changes ...
116
117
118
119
120
121
122
123
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...
124

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

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

94082e77   haribo   Date: 03/06/2016
134
@login_required
77816f10   haribo   Workflow implemen...
135
def system_retrieve_logs(request):
94082e77   haribo   Date: 03/06/2016
136
    '''
e39312f1   Jeremy   Fix bug on schedu...
137
        Called by the dashboard system page with ajax request every seconds, to get the logs and print them
94082e77   haribo   Date: 03/06/2016
138
    '''
77816f10   haribo   Workflow implemen...
139
    if request.is_ajax():
77816f10   haribo   Workflow implemen...
140
141
142
143
144
        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...
145
        monitoring_logs = Log.objects.filter(agent='Monitoring')
77816f10   haribo   Workflow implemen...
146
        return render(request, 'dashboard/system_logs.html', locals())
77816f10   haribo   Workflow implemen...
147
148


62229d51   haribo   began dashboard (...
149
def schedule(request):
5f148e7d   Unknown   Update to django2...
150
    url_ = reverse('admin:common_schedule_changelist')
81847ba2   haribo   Dashboard buttons...
151
    return redirect(url_)
62229d51   haribo   began dashboard (...
152

77816f10   haribo   Workflow implemen...
153

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

e564e13d   theopuhl   Add merging file
159
@login_required
e31b2208   theophile.puhl@epitech.eu   Severals Changes ...
160
def change_globalMode(request):
661d7672   theopuhl   Unittest Fix
161
162
163
164
    try :
        config = get_object_or_404(Config, id=1)
        config.global_mode = not config.global_mode
        config.save()
2c61f856   theopuhl   Url change to pat...
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
        return redirect('states')
    except Config.DoesNotExist:
        return redirect('states')

@login_required
def change_ack(request):
    try :
        config = get_object_or_404(Config, id=1)
        config.ack = not config.ack
        config.save()
        return redirect('states')
    except Config.DoesNotExist:
        return redirect('states')

@login_required
def change_bypass(request):
    try :
        config = get_object_or_404(Config, id=1)
        config.bypass = not config.bypass
        config.save()
        return redirect('states')
661d7672   theopuhl   Unittest Fix
186
    except Config.DoesNotExist:
2c61f856   theopuhl   Url change to pat...
187
188
189
190
191
192
193
194
195
196
197
        return redirect('states')

@login_required
def change_activate(request, pk):
    try :
        user = get_object_or_404(PyrosUser, pk=pk)
        user.is_active = not user.is_active
        user.save()
        return redirect('user-detail', pk=pk)
    except PyrosUser.DoesNotExist:
        return redirect('user-detail', pk=pk)
ba1d1f2b   Quentin Durand   implémentation Fr...
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218

@login_required
def send_command_to_telescope(request):
    return render(request, "dashboard/send_command_telescope.html")

@login_required
def submit_command_to_telescope(request):
    import json
    if request.method == 'POST':
        param = request.POST.get("comande")
        return HttpResponse(json.dumps({'message': "Command send OK"}))

@login_required
def submit_command_to_telescope_expert(request):
    import os
    if request.method == 'POST':
        param = request.POST.get("commande_expert")
        if param:
            os.system("echo " + param + " >> contenu")
            return HttpResponse(json.dumps({'message': "Command send OK"}))
        return HttpResponse(json.dumps({'message': "Missing command data"}))
2c61f856   theopuhl   Url change to pat...
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
    return redirect('submit_command_to_telescope')

@login_required
def user_detail_view(request,pk):
    try:
        user_id=PyrosUser.objects.get(pk=pk)
        current_user = request.user
    except PyrosUser.DoesNotExist:
        raise Http404("User does not exist")
    return render(request, 'dashboard/user_detail.html', context={'user' : user_id, 'current_user' : current_user, 'level': request.user.user_level.priority})

@login_required
def user_detail_edit(request,pk):
    edit = get_object_or_404(PyrosUser, pk=pk)
    form = UserForm(request.POST or None, instance=edit)
    if form.is_valid():
        form.save()
        return redirect('user-detail', pk=pk)
    return render(request, 'dashboard/user_detail_edit.html', {'form': form})

@login_required
def operator_state(request):
    instance = get_object_or_404(Config, id=1)
    return render(request, 'dashboard/operator_state.html', {'config' : instance})