@@ -1,144 +1,149 | |||
|
1 | 1 | {% extends "base.html" %} |
|
2 | 2 | {% load bootstrap3 %} |
|
3 | 3 | {% load static %} |
|
4 | 4 | {% load main_tags %} |
|
5 | 5 | {% block extra-head %} |
|
6 | 6 | <link href="{% static 'css/bootstrap-datetimepicker.min.css' %}" media="screen" rel="stylesheet"> |
|
7 | 7 | {% endblock %} |
|
8 | 8 | |
|
9 | 9 | {% block camp-active %}active{% endblock %} |
|
10 | 10 | |
|
11 | 11 | {% block content-title %}{{title}}{% endblock %} |
|
12 | 12 | {% block content-suptitle %}{{suptitle}}{% endblock %} |
|
13 | 13 | |
|
14 | 14 | {% block content %} |
|
15 | 15 | |
|
16 | 16 | <!-- Not Empty--> |
|
17 | 17 | {% if details %} |
|
18 | 18 | |
|
19 | 19 | <form class="form" method="post" action=""> |
|
20 | 20 | {% csrf_token %} |
|
21 | 21 | |
|
22 | 22 | {% bootstrap_form form layout='horizontal' size='medium' %} |
|
23 | 23 | <div style="clear: both;"></div> |
|
24 | 24 | <br> |
|
25 | 25 | <!-- For deep search --> |
|
26 | 26 | {% if search_button == True %} |
|
27 | 27 | <button id="button-1" type="button" class="btn btn-primary pull-right">{{button}}</button> |
|
28 | 28 | {% endif %} |
|
29 | 29 | <br> |
|
30 | 30 | <br> |
|
31 | 31 | </form> |
|
32 | 32 | <br> |
|
33 | 33 | <br> |
|
34 | 34 | |
|
35 | 35 | {% endif %} |
|
36 | 36 | |
|
37 | 37 | <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true" > |
|
38 | 38 | |
|
39 | 39 | {% for location in locations %} |
|
40 | 40 | |
|
41 | 41 | <div class="panel panel-default"> |
|
42 | 42 | <div class="panel-heading" role="tab" id="headingTwo"> |
|
43 | 43 | <h4 class="panel-title"> |
|
44 | 44 | |
|
45 | 45 | <a class="collapsed" role="button" data-toggle="collapse" href="#collapseTwo-{{ location.id }}" aria-expanded="false" aria-controls="collapseTwo"> |
|
46 | 46 | {{location.name}}: Experiment List |
|
47 | 47 | <span> |
|
48 | 48 | </span> |
|
49 | 49 | </a> |
|
50 | 50 | |
|
51 | 51 | <button type="button" name="bt_play" class="btn btn-primary pull-right btn-xs" data-url="{% url 'url_radar_play' campaign.id location.id %}" style="margin-left: 10px"> |
|
52 | 52 | <span class="glyphicon glyphicon-play" aria-hidden="true"></span> |
|
53 | 53 | </button> |
|
54 | 54 | <button type="button" name="bt_stop" class="btn btn-primary pull-right btn-xs" data-url="{% url 'url_radar_stop' campaign.id location.id %}" aria-label="Left Align" style="margin-left: 10px"> |
|
55 | 55 | <span class="glyphicon glyphicon-stop" aria-hidden="true"></span> |
|
56 | 56 | </button> |
|
57 |
<button type="button" name="bt_refresh" class="btn btn-primary pull-right btn-xs" data-url="{% url 'url_radar_ |
|
|
57 | <button type="button" name="bt_refresh" class="btn btn-primary pull-right btn-xs" data-url="{% url 'url_radar_refresh' campaign.id location.id %}" aria-label="Left Align" style="margin-left: 10px"> | |
|
58 | 58 | <span class="glyphicon glyphicon-refresh" aria-hidden="true"></span> |
|
59 | 59 | </button> |
|
60 | 60 | </h4> |
|
61 | 61 | </div> |
|
62 | 62 | |
|
63 | 63 | <div id="collapseTwo-{{ location.id }}" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingTwo"> |
|
64 | 64 | <div class="panel-body"> |
|
65 | 65 | <table class="table table-hover"> |
|
66 | 66 | <tr> |
|
67 | 67 | <th>#</th> |
|
68 | 68 | {% for header in experiment_keys %} |
|
69 | 69 | <th>{{ header|title }}</th> |
|
70 | 70 | {% endfor%} |
|
71 | 71 | </tr> |
|
72 | 72 | |
|
73 | 73 | {% for item in experiments %} |
|
74 | 74 | {% if location.name in item.location.name %} |
|
75 | 75 | |
|
76 | 76 | <tr class="clickable-row" data-href="{% url 'url_experiment' item.id %}" > |
|
77 | 77 | <td>{{ forloop.counter }}</td> |
|
78 | 78 | {% for key in experiment_keys %} |
|
79 | 79 | {% if 'status' in key %} |
|
80 | 80 | <td class="text-{{item.status_color}}">{{ item|value:key }}</td> |
|
81 | 81 | {% else %} |
|
82 | 82 | <td>{{ item|value:key }}</td> |
|
83 | 83 | {% endif %} |
|
84 | 84 | {% endfor %} |
|
85 | 85 | </tr> |
|
86 | 86 | {% endif %} |
|
87 | 87 | {% endfor %} |
|
88 | 88 | </table> |
|
89 | 89 | </div> |
|
90 | 90 | </div> |
|
91 | 91 | </div> |
|
92 | 92 | {% endfor %} |
|
93 | 93 | </div> |
|
94 | 94 | |
|
95 | 95 | |
|
96 | 96 | |
|
97 | 97 | {% endblock %} |
|
98 | 98 | |
|
99 | 99 | |
|
100 | 100 | |
|
101 | 101 | {% block extra-js%} |
|
102 | 102 | <script type="text/javascript"> |
|
103 | 103 | |
|
104 | 104 | //--------For PLAY Button------- |
|
105 | 105 | $("#accordion").on("click", "button[name=bt_play]", function(){ |
|
106 | 106 | //alert($(this).data('url')); |
|
107 | 107 | document.location = $(this).data('url'); |
|
108 | 108 | }); |
|
109 | 109 | |
|
110 | 110 | //--------For STOP Button------- |
|
111 | 111 | $("#accordion").on("click", "button[name=bt_stop]", function(){ |
|
112 | 112 | //alert($(this).data('url')); |
|
113 | 113 | document.location = $(this).data('url'); |
|
114 | 114 | }); |
|
115 | 115 | |
|
116 | //--------For REFRESH Button------- | |
|
117 | $("#accordion").on("click", "button[name=bt_refresh]", function(){ | |
|
118 | document.location = $(this).data('url'); | |
|
119 | }); | |
|
120 | ||
|
116 | 121 | $(".clickable-row").click(function() { |
|
117 | 122 | document.location = $(this).data("href"); |
|
118 | 123 | }); |
|
119 | 124 | |
|
120 | 125 | {% if search_button == True %} |
|
121 | 126 | $(document).ready(function() { |
|
122 | 127 | $("#id_campaign").change(function() { |
|
123 | 128 | var id_camp = document.getElementById("id_campaign").value; |
|
124 | 129 | //alert(id_camp); |
|
125 | 130 | document.location = "{% url 'url_operation'%}"+String(id_camp); |
|
126 | 131 | }); |
|
127 | 132 | }); |
|
128 | 133 | {% else %} |
|
129 | 134 | $(document).ready(function() { |
|
130 | 135 | $("#id_campaign").change(function() { |
|
131 | 136 | var id_camp = document.getElementById("id_campaign").value; |
|
132 | 137 | //alert(id_camp); |
|
133 | 138 | document.location = "{% url 'url_operation_search'%}"+String(id_camp); |
|
134 | 139 | }); |
|
135 | 140 | }); |
|
136 | 141 | {% endif %} |
|
137 | 142 | |
|
138 | 143 | $("#button-1").click(function() { |
|
139 | 144 | document.location = "{% url 'url_operation_search' %}"; |
|
140 | 145 | }); |
|
141 | 146 | |
|
142 | 147 | |
|
143 | 148 | </script> |
|
144 | 149 | {% endblock %} No newline at end of file |
@@ -1,50 +1,51 | |||
|
1 | 1 | from django.conf.urls import url |
|
2 | 2 | |
|
3 | 3 | urlpatterns = ( |
|
4 | 4 | url(r'^location/new/$', 'apps.main.views.location_new', name='url_add_location'), |
|
5 | 5 | url(r'^location/$', 'apps.main.views.locations', name='url_locations'), |
|
6 | 6 | url(r'^location/(?P<id_loc>-?\d+)/$', 'apps.main.views.location', name='url_location'), |
|
7 | 7 | url(r'^location/(?P<id_loc>-?\d+)/edit/$', 'apps.main.views.location_edit', name='url_edit_location'), |
|
8 | 8 | url(r'^location/(?P<id_loc>-?\d+)/delete/$', 'apps.main.views.location_delete', name='url_delete_location'), |
|
9 | 9 | |
|
10 | 10 | url(r'^device/new/$', 'apps.main.views.device_new', name='url_add_device'), |
|
11 | 11 | url(r'^device/$', 'apps.main.views.devices', name='url_devices'), |
|
12 | 12 | url(r'^device/(?P<id_dev>-?\d+)/$', 'apps.main.views.device', name='url_device'), |
|
13 | 13 | url(r'^device/(?P<id_dev>-?\d+)/edit/$', 'apps.main.views.device_edit', name='url_edit_device'), |
|
14 | 14 | url(r'^device/(?P<id_dev>-?\d+)/delete/$', 'apps.main.views.device_delete', name='url_delete_device'), |
|
15 | 15 | |
|
16 | 16 | url(r'^campaign/new/$', 'apps.main.views.campaign_new', name='url_add_campaign'), |
|
17 | 17 | url(r'^campaign/$', 'apps.main.views.campaigns', name='url_campaigns'), |
|
18 | 18 | url(r'^campaign/(?P<id_camp>-?\d+)/$', 'apps.main.views.campaign', name='url_campaign'), |
|
19 | 19 | url(r'^campaign/(?P<id_camp>-?\d+)/edit/$', 'apps.main.views.campaign_edit', name='url_edit_campaign'), |
|
20 | 20 | url(r'^campaign/(?P<id_camp>-?\d+)/delete/$', 'apps.main.views.campaign_delete', name='url_delete_campaign'), |
|
21 | 21 | |
|
22 | 22 | url(r'^experiment/new/$', 'apps.main.views.experiment_new', name='url_add_experiment'), |
|
23 | 23 | url(r'^experiment/$', 'apps.main.views.experiments', name='url_experiments'), |
|
24 | 24 | url(r'^experiment/(?P<id_exp>-?\d+)/$', 'apps.main.views.experiment', name='url_experiment'), |
|
25 | 25 | url(r'^experiment/(?P<id_exp>-?\d+)/edit/$', 'apps.main.views.experiment_edit', name='url_edit_experiment'), |
|
26 | 26 | url(r'^experiment/(?P<id_exp>-?\d+)/delete/$', 'apps.main.views.experiment_delete', name='url_delete_experiment'), |
|
27 | 27 | |
|
28 | 28 | url(r'^experiment/(?P<id_exp>-?\d+)/new_dev_conf/$', 'apps.main.views.dev_conf_new', name='url_add_dev_conf'), |
|
29 | 29 | url(r'^experiment/(?P<id_exp>-?\d+)/new_dev_conf/(?P<id_dev>-?\d+)/$', 'apps.main.views.dev_conf_new', name='url_add_dev_conf'), |
|
30 | 30 | url(r'^dev_conf/$', 'apps.main.views.dev_confs', name='url_dev_confs'), |
|
31 | 31 | url(r'^dev_conf/(?P<id_conf>-?\d+)/$', 'apps.main.views.dev_conf', name='url_dev_conf'), |
|
32 | 32 | url(r'^dev_conf/(?P<id_conf>-?\d+)/edit/$', 'apps.main.views.dev_conf_edit', name='url_edit_dev_conf'), |
|
33 | 33 | url(r'^dev_conf/(?P<id_conf>-?\d+)/delete/$', 'apps.main.views.dev_conf_delete', name='url_delete_dev_conf'), |
|
34 | 34 | |
|
35 | 35 | url(r'^dev_conf/(?P<id_conf>-?\d+)/write/$', 'apps.main.views.dev_conf_write', name='url_write_dev_conf'), |
|
36 | 36 | url(r'^dev_conf/(?P<id_conf>-?\d+)/read/$', 'apps.main.views.dev_conf_read', name='url_read_dev_conf'), |
|
37 | 37 | url(r'^dev_conf/(?P<id_conf>-?\d+)/import/$', 'apps.main.views.dev_conf_import', name='url_import_dev_conf'), |
|
38 | 38 | url(r'^dev_conf/(?P<id_conf>-?\d+)/export/$', 'apps.main.views.dev_conf_export', name='url_export_dev_conf'), |
|
39 | 39 | url(r'^dev_conf/(?P<id_conf>-?\d+)/start/$', 'apps.main.views.dev_conf_start', name='url_start_dev_conf'), |
|
40 | 40 | url(r'^dev_conf/(?P<id_conf>-?\d+)/stop/$', 'apps.main.views.dev_conf_stop', name='url_stop_dev_conf'), |
|
41 | 41 | url(r'^dev_conf/(?P<id_conf>-?\d+)/status/$', 'apps.main.views.dev_conf_status', name='url_status_dev_conf'), |
|
42 | 42 | |
|
43 | 43 | url(r'^operation/$', 'apps.main.views.operation', name='url_operation'), |
|
44 | 44 | url(r'^operation/(?P<id_camp>-?\d+)/$', 'apps.main.views.operation', name='url_operation'), |
|
45 | 45 | url(r'^operation/search/$', 'apps.main.views.operation_search', name='url_operation_search'), |
|
46 | 46 | url(r'^operation/search/(?P<id_camp>-?\d+)/$', 'apps.main.views.operation_search', name='url_operation_search'), |
|
47 | 47 | url(r'^operation/(?P<id_camp>-?\d+)/radar/(?P<id_radar>-?\d+)/play/$', 'apps.main.views.radar_play', name='url_radar_play'), |
|
48 | 48 | url(r'^operation/(?P<id_camp>-?\d+)/radar/(?P<id_radar>-?\d+)/stop/$', 'apps.main.views.radar_stop', name='url_radar_stop'), |
|
49 | url(r'^operation/(?P<id_camp>-?\d+)/radar/(?P<id_radar>-?\d+)/refresh/$', 'apps.main.views.radar_refresh', name='url_radar_refresh'), | |
|
49 | 50 | |
|
50 | 51 | ) |
@@ -1,961 +1,962 | |||
|
1 | 1 | from django.shortcuts import render, redirect, get_object_or_404, HttpResponse |
|
2 | 2 | from django.http import HttpResponseRedirect |
|
3 | 3 | from django.core.urlresolvers import reverse |
|
4 | 4 | from django.contrib import messages |
|
5 | from datetime import datetime | |
|
5 | 6 | |
|
6 | 7 | from .forms import CampaignForm, ExperimentForm, DeviceForm, ConfigurationForm, LocationForm, UploadFileForm, DownloadFileForm, OperationForm, NewForm |
|
7 | 8 | from .forms import OperationSearchForm |
|
8 | 9 | from apps.cgs.forms import CGSConfigurationForm |
|
9 | 10 | from apps.jars.forms import JARSConfigurationForm |
|
10 | 11 | from apps.usrp.forms import USRPConfigurationForm |
|
11 | 12 | from apps.abs.forms import ABSConfigurationForm |
|
12 | 13 | from apps.rc.forms import RCConfigurationForm |
|
13 | 14 | from apps.dds.forms import DDSConfigurationForm |
|
14 | 15 | |
|
15 | 16 | from .models import Campaign, Experiment, Device, Configuration, Location, RunningExperiment |
|
16 | 17 | from apps.cgs.models import CGSConfiguration |
|
17 | 18 | from apps.jars.models import JARSConfiguration |
|
18 | 19 | from apps.usrp.models import USRPConfiguration |
|
19 | 20 | from apps.abs.models import ABSConfiguration |
|
20 | 21 | from apps.rc.models import RCConfiguration |
|
21 | 22 | from apps.dds.models import DDSConfiguration |
|
22 | 23 | |
|
23 | 24 | # Create your views here. |
|
24 | 25 | |
|
25 | 26 | CONF_FORMS = { |
|
26 | 27 | 'rc': RCConfigurationForm, |
|
27 | 28 | 'dds': DDSConfigurationForm, |
|
28 | 29 | 'jars': JARSConfigurationForm, |
|
29 | 30 | 'cgs': CGSConfigurationForm, |
|
30 | 31 | 'abs': ABSConfigurationForm, |
|
31 | 32 | 'usrp': USRPConfigurationForm, |
|
32 | 33 | } |
|
33 | 34 | |
|
34 | 35 | CONF_MODELS = { |
|
35 | 36 | 'rc': RCConfiguration, |
|
36 | 37 | 'dds': DDSConfiguration, |
|
37 | 38 | 'jars': JARSConfiguration, |
|
38 | 39 | 'cgs': CGSConfiguration, |
|
39 | 40 | 'abs': ABSConfiguration, |
|
40 | 41 | 'usrp': USRPConfiguration, |
|
41 | 42 | } |
|
42 | 43 | |
|
43 | 44 | def index(request): |
|
44 | 45 | kwargs = {} |
|
45 | 46 | |
|
46 | 47 | return render(request, 'index.html', kwargs) |
|
47 | 48 | |
|
48 | 49 | def locations(request): |
|
49 | 50 | |
|
50 | 51 | locations = Location.objects.all().order_by('name') |
|
51 | 52 | |
|
52 | 53 | keys = ['id', 'name', 'description'] |
|
53 | 54 | |
|
54 | 55 | kwargs = {} |
|
55 | 56 | kwargs['location_keys'] = keys[1:] |
|
56 | 57 | kwargs['locations'] = locations |
|
57 | 58 | kwargs['title'] = 'Location' |
|
58 | 59 | kwargs['suptitle'] = 'List' |
|
59 | 60 | kwargs['button'] = 'New Location' |
|
60 | 61 | |
|
61 | 62 | return render(request, 'locations.html', kwargs) |
|
62 | 63 | |
|
63 | 64 | def location(request, id_loc): |
|
64 | 65 | |
|
65 | 66 | location = get_object_or_404(Location, pk=id_loc) |
|
66 | 67 | |
|
67 | 68 | kwargs = {} |
|
68 | 69 | kwargs['location'] = location |
|
69 | 70 | kwargs['location_keys'] = ['name', 'description'] |
|
70 | 71 | |
|
71 | 72 | kwargs['title'] = 'Location' |
|
72 | 73 | kwargs['suptitle'] = 'Details' |
|
73 | 74 | |
|
74 | 75 | return render(request, 'location.html', kwargs) |
|
75 | 76 | |
|
76 | #def location_play(request, id_camp, id_loc): | |
|
77 | ||
|
78 | # campaign = get_object_or_404(Campaign, pk=id_camp) | |
|
79 | # print campaign | |
|
80 | # location = get_object_or_404(Location, pk=id_loc) | |
|
81 | # experiments = Experiment.objects.filter(location__pk=location.id).filter(campaign__pk=campaign.id) | |
|
82 | # locations = Location.objects.filter(pk=id_loc) | |
|
83 | ||
|
84 | # if request.method=='GET': | |
|
85 | # form = OperationForm(initial={'campaign': campaign.id}) | |
|
86 | ||
|
87 | # kwargs = {} | |
|
88 | #---Campaign | |
|
89 | # kwargs['campaign'] = campaign | |
|
90 | # kwargs['campaign_keys'] = ['name', 'start_date', 'end_date', 'tags', 'description'] | |
|
91 | #---Experiment | |
|
92 | # keys = ['id', 'name', 'start_time', 'end_time'] | |
|
93 | # kwargs['experiment_keys'] = keys[1:] | |
|
94 | # kwargs['experiments'] = experiments | |
|
95 | #---Radar | |
|
96 | # kwargs['location'] = location | |
|
97 | #---Else | |
|
98 | # kwargs['title'] = 'Campaign' | |
|
99 | # kwargs['suptitle'] = campaign.name | |
|
100 | # kwargs['form'] = form | |
|
101 | # kwargs['button'] = 'Search' | |
|
102 | ||
|
103 | # return render(request, 'operation_play.html', kwargs) | |
|
104 | 77 | |
|
105 | 78 | def location_new(request): |
|
106 | 79 | |
|
107 | 80 | if request.method == 'GET': |
|
108 | 81 | form = LocationForm() |
|
109 | 82 | |
|
110 | 83 | if request.method == 'POST': |
|
111 | 84 | form = LocationForm(request.POST) |
|
112 | 85 | |
|
113 | 86 | if form.is_valid(): |
|
114 | 87 | form.save() |
|
115 | 88 | return redirect('url_locations') |
|
116 | 89 | |
|
117 | 90 | kwargs = {} |
|
118 | 91 | kwargs['form'] = form |
|
119 | 92 | kwargs['title'] = 'Location' |
|
120 | 93 | kwargs['suptitle'] = 'New' |
|
121 | 94 | kwargs['button'] = 'Create' |
|
122 | 95 | |
|
123 | 96 | return render(request, 'location_edit.html', kwargs) |
|
124 | 97 | |
|
125 | 98 | def location_edit(request, id_loc): |
|
126 | 99 | |
|
127 | 100 | location = get_object_or_404(Location, pk=id_loc) |
|
128 | 101 | |
|
129 | 102 | if request.method=='GET': |
|
130 | 103 | form = LocationForm(instance=location) |
|
131 | 104 | |
|
132 | 105 | if request.method=='POST': |
|
133 | 106 | form = LocationForm(request.POST, instance=location) |
|
134 | 107 | |
|
135 | 108 | if form.is_valid(): |
|
136 | 109 | form.save() |
|
137 | 110 | return redirect('url_locations') |
|
138 | 111 | |
|
139 | 112 | kwargs = {} |
|
140 | 113 | kwargs['form'] = form |
|
141 | 114 | kwargs['title'] = 'Location' |
|
142 | 115 | kwargs['suptitle'] = 'Edit' |
|
143 | 116 | kwargs['button'] = 'Update' |
|
144 | 117 | |
|
145 | 118 | return render(request, 'location_edit.html', kwargs) |
|
146 | 119 | |
|
147 | 120 | def location_delete(request, id_loc): |
|
148 | 121 | |
|
149 | 122 | location = get_object_or_404(Location, pk=id_loc) |
|
150 | 123 | |
|
151 | 124 | if request.method=='POST': |
|
152 | 125 | |
|
153 | 126 | if request.user.is_staff: |
|
154 | 127 | location.delete() |
|
155 | 128 | return redirect('url_locations') |
|
156 | 129 | |
|
157 | 130 | return HttpResponse("Not enough permission to delete this object") |
|
158 | 131 | |
|
159 | 132 | kwargs = {'object':location, 'loc_active':'active', |
|
160 | 133 | 'url_cancel':'url_location', 'id_item':id_loc} |
|
161 | 134 | |
|
162 | 135 | return render(request, 'item_delete.html', kwargs) |
|
163 | 136 | |
|
164 | 137 | def devices(request): |
|
165 | 138 | |
|
166 | 139 | devices = Device.objects.all().order_by('device_type__name') |
|
167 | 140 | |
|
168 | 141 | # keys = ['id', 'device_type__name', 'name', 'ip_address'] |
|
169 | 142 | keys = ['id', 'name', 'ip_address', 'port_address', 'device_type'] |
|
170 | 143 | |
|
171 | 144 | kwargs = {} |
|
172 | 145 | kwargs['device_keys'] = keys[1:] |
|
173 | 146 | kwargs['devices'] = devices#.values(*keys) |
|
174 | 147 | kwargs['title'] = 'Device' |
|
175 | 148 | kwargs['suptitle'] = 'List' |
|
176 | 149 | kwargs['button'] = 'New Device' |
|
177 | 150 | |
|
178 | 151 | return render(request, 'devices.html', kwargs) |
|
179 | 152 | |
|
180 | 153 | def device(request, id_dev): |
|
181 | 154 | |
|
182 | 155 | device = get_object_or_404(Device, pk=id_dev) |
|
183 | 156 | |
|
184 | 157 | kwargs = {} |
|
185 | 158 | kwargs['device'] = device |
|
186 | 159 | kwargs['device_keys'] = ['device_type', 'name', 'ip_address', 'port_address', 'description'] |
|
187 | 160 | |
|
188 | 161 | kwargs['title'] = 'Device' |
|
189 | 162 | kwargs['suptitle'] = 'Details' |
|
190 | 163 | |
|
191 | 164 | return render(request, 'device.html', kwargs) |
|
192 | 165 | |
|
193 | 166 | def device_new(request): |
|
194 | 167 | |
|
195 | 168 | if request.method == 'GET': |
|
196 | 169 | form = DeviceForm() |
|
197 | 170 | |
|
198 | 171 | if request.method == 'POST': |
|
199 | 172 | form = DeviceForm(request.POST) |
|
200 | 173 | |
|
201 | 174 | if form.is_valid(): |
|
202 | 175 | form.save() |
|
203 | 176 | return redirect('url_devices') |
|
204 | 177 | |
|
205 | 178 | kwargs = {} |
|
206 | 179 | kwargs['form'] = form |
|
207 | 180 | kwargs['title'] = 'Device' |
|
208 | 181 | kwargs['suptitle'] = 'New' |
|
209 | 182 | kwargs['button'] = 'Create' |
|
210 | 183 | |
|
211 | 184 | return render(request, 'device_edit.html', kwargs) |
|
212 | 185 | |
|
213 | 186 | def device_edit(request, id_dev): |
|
214 | 187 | |
|
215 | 188 | device = get_object_or_404(Device, pk=id_dev) |
|
216 | 189 | |
|
217 | 190 | if request.method=='GET': |
|
218 | 191 | form = DeviceForm(instance=device) |
|
219 | 192 | |
|
220 | 193 | if request.method=='POST': |
|
221 | 194 | form = DeviceForm(request.POST, instance=device) |
|
222 | 195 | |
|
223 | 196 | if form.is_valid(): |
|
224 | 197 | form.save() |
|
225 | 198 | return redirect('url_devices') |
|
226 | 199 | |
|
227 | 200 | kwargs = {} |
|
228 | 201 | kwargs['form'] = form |
|
229 | 202 | kwargs['title'] = 'Device' |
|
230 | 203 | kwargs['suptitle'] = 'Edit' |
|
231 | 204 | kwargs['button'] = 'Update' |
|
232 | 205 | |
|
233 | 206 | return render(request, 'device_edit.html', kwargs) |
|
234 | 207 | |
|
235 | 208 | def device_delete(request, id_dev): |
|
236 | 209 | |
|
237 | 210 | device = get_object_or_404(Device, pk=id_dev) |
|
238 | 211 | |
|
239 | 212 | if request.method=='POST': |
|
240 | 213 | |
|
241 | 214 | if request.user.is_staff: |
|
242 | 215 | device.delete() |
|
243 | 216 | return redirect('url_devices') |
|
244 | 217 | |
|
245 | 218 | return HttpResponse("Not enough permission to delete this object") |
|
246 | 219 | |
|
247 | 220 | kwargs = {'object':device, 'dev_active':'active', |
|
248 | 221 | 'url_cancel':'url_device', 'id_item':id_dev} |
|
249 | 222 | |
|
250 | 223 | return render(request, 'item_delete.html', kwargs) |
|
251 | 224 | |
|
252 | 225 | def campaigns(request): |
|
253 | 226 | |
|
254 | 227 | campaigns = Campaign.objects.all().order_by('start_date') |
|
255 | 228 | |
|
256 | 229 | keys = ['id', 'name', 'start_date', 'end_date'] |
|
257 | 230 | |
|
258 | 231 | kwargs = {} |
|
259 | 232 | kwargs['campaign_keys'] = keys[1:] |
|
260 | 233 | kwargs['campaigns'] = campaigns#.values(*keys) |
|
261 | 234 | kwargs['title'] = 'Campaign' |
|
262 | 235 | kwargs['suptitle'] = 'List' |
|
263 | 236 | kwargs['button'] = 'New Campaign' |
|
264 | 237 | |
|
265 | 238 | return render(request, 'campaigns.html', kwargs) |
|
266 | 239 | |
|
267 | 240 | def campaign(request, id_camp): |
|
268 | 241 | |
|
269 | 242 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
270 | 243 | experiments = Experiment.objects.filter(campaign=campaign) |
|
271 | 244 | |
|
272 | 245 | form = CampaignForm(instance=campaign) |
|
273 | 246 | |
|
274 | 247 | kwargs = {} |
|
275 | 248 | kwargs['campaign'] = campaign |
|
276 | 249 | kwargs['campaign_keys'] = ['name', 'start_date', 'end_date', 'tags', 'description'] |
|
277 | 250 | |
|
278 | 251 | keys = ['id', 'name', 'start_time', 'end_time'] |
|
279 | 252 | |
|
280 | 253 | kwargs['experiment_keys'] = keys[1:] |
|
281 | 254 | kwargs['experiments'] = experiments.values(*keys) |
|
282 | 255 | |
|
283 | 256 | kwargs['title'] = 'Campaign' |
|
284 | 257 | kwargs['suptitle'] = 'Details' |
|
285 | 258 | |
|
286 | 259 | kwargs['form'] = form |
|
287 | 260 | kwargs['button'] = 'Add Experiment' |
|
288 | 261 | |
|
289 | 262 | return render(request, 'campaign.html', kwargs) |
|
290 | 263 | |
|
291 | 264 | def campaign_new(request): |
|
292 | 265 | |
|
293 | 266 | kwargs = {} |
|
294 | 267 | |
|
295 | 268 | if request.method == 'GET': |
|
296 | 269 | |
|
297 | 270 | if 'template' in request.GET: |
|
298 | 271 | if request.GET['template']=='0': |
|
299 | 272 | form = NewForm(initial={'create_from':2}, |
|
300 | 273 | template_choices=Campaign.objects.filter(template=True).values_list('id', 'name')) |
|
301 | 274 | else: |
|
302 | 275 | kwargs['button'] = 'Create' |
|
303 | 276 | kwargs['experiments'] = Configuration.objects.filter(experiment=request.GET['template']) |
|
304 | 277 | kwargs['experiment_keys'] = ['name', 'start_time', 'end_time'] |
|
305 | 278 | form = CampaignForm(instance=Campaign.objects.get(pk=request.GET['template']), |
|
306 | 279 | initial={'template':False}) |
|
307 | 280 | elif 'blank' in request.GET: |
|
308 | 281 | kwargs['button'] = 'Create' |
|
309 | 282 | form = CampaignForm() |
|
310 | 283 | else: |
|
311 | 284 | form = NewForm() |
|
312 | 285 | |
|
313 | 286 | if request.method == 'POST': |
|
314 | 287 | kwargs['button'] = 'Create' |
|
315 | 288 | post = request.POST.copy() |
|
316 | 289 | experiments = [] |
|
317 | 290 | |
|
318 | 291 | for id_exp in post.getlist('experiments'): |
|
319 | 292 | exp = Experiment.objects.get(pk=id_exp) |
|
320 | 293 | new_exp = exp.clone(template=False) |
|
321 | 294 | experiments.append(new_exp) |
|
322 | 295 | |
|
323 | 296 | post.setlist('experiments', []) |
|
324 | 297 | |
|
325 | 298 | form = CampaignForm(post) |
|
326 | 299 | |
|
327 | 300 | if form.is_valid(): |
|
328 | 301 | campaign = form.save() |
|
329 | 302 | for exp in experiments: |
|
330 | 303 | campaign.experiments.add(exp) |
|
331 | 304 | campaign.save() |
|
332 | 305 | return redirect('url_campaign', id_camp=campaign.id) |
|
333 | 306 | |
|
334 | 307 | kwargs['form'] = form |
|
335 | 308 | kwargs['title'] = 'Campaign' |
|
336 | 309 | kwargs['suptitle'] = 'New' |
|
337 | 310 | |
|
338 | 311 | return render(request, 'campaign_edit.html', kwargs) |
|
339 | 312 | |
|
340 | 313 | def campaign_edit(request, id_camp): |
|
341 | 314 | |
|
342 | 315 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
343 | 316 | |
|
344 | 317 | if request.method=='GET': |
|
345 | 318 | form = CampaignForm(instance=campaign) |
|
346 | 319 | |
|
347 | 320 | if request.method=='POST': |
|
348 | 321 | form = CampaignForm(request.POST, instance=campaign) |
|
349 | 322 | |
|
350 | 323 | if form.is_valid(): |
|
351 | 324 | form.save() |
|
352 | 325 | return redirect('url_campaign', id_camp=id_camp) |
|
353 | 326 | |
|
354 | 327 | kwargs = {} |
|
355 | 328 | kwargs['form'] = form |
|
356 | 329 | kwargs['title'] = 'Campaign' |
|
357 | 330 | kwargs['suptitle'] = 'Edit' |
|
358 | 331 | kwargs['button'] = 'Update' |
|
359 | 332 | |
|
360 | 333 | return render(request, 'campaign_edit.html', kwargs) |
|
361 | 334 | |
|
362 | 335 | def campaign_delete(request, id_camp): |
|
363 | 336 | |
|
364 | 337 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
365 | 338 | |
|
366 | 339 | if request.method=='POST': |
|
367 | 340 | if request.user.is_staff: |
|
368 | 341 | |
|
369 | 342 | for exp in campaign.experiments.all(): |
|
370 | 343 | for conf in Configuration.objects.filter(experiment=exp): |
|
371 | 344 | conf.delete() |
|
372 | 345 | exp.delete() |
|
373 | 346 | campaign.delete() |
|
374 | 347 | |
|
375 | 348 | return redirect('url_campaigns') |
|
376 | 349 | |
|
377 | 350 | return HttpResponse("Not enough permission to delete this object") |
|
378 | 351 | |
|
379 | 352 | kwargs = {'object':campaign, 'camp_active':'active', |
|
380 | 353 | 'url_cancel':'url_campaign', 'id_item':id_camp} |
|
381 | 354 | |
|
382 | 355 | return render(request, 'item_delete.html', kwargs) |
|
383 | 356 | |
|
384 | 357 | def experiments(request): |
|
385 | 358 | |
|
386 | 359 | experiment_list = Experiment.objects.all() |
|
387 | 360 | |
|
388 | 361 | keys = ['id', 'name', 'start_time', 'end_time'] |
|
389 | 362 | |
|
390 | 363 | kwargs = {} |
|
391 | 364 | |
|
392 | 365 | kwargs['experiment_keys'] = keys[1:] |
|
393 | 366 | kwargs['experiments'] = experiment_list |
|
394 | 367 | |
|
395 | 368 | kwargs['title'] = 'Experiment' |
|
396 | 369 | kwargs['suptitle'] = 'List' |
|
397 | 370 | kwargs['button'] = 'New Experiment' |
|
398 | 371 | |
|
399 | 372 | return render(request, 'experiments.html', kwargs) |
|
400 | 373 | |
|
401 | 374 | def experiment(request, id_exp): |
|
402 | 375 | |
|
403 | 376 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
404 | 377 | |
|
405 | 378 | configurations = Configuration.objects.filter(experiment=experiment, type=0) |
|
406 | 379 | |
|
407 | 380 | kwargs = {} |
|
408 | 381 | |
|
409 | 382 | exp_keys = ['id', 'location', 'name', 'start_time', 'end_time'] |
|
410 | 383 | conf_keys = ['id', 'device__name', 'device__device_type', 'device__ip_address', 'device__port_address'] |
|
411 | 384 | |
|
412 | 385 | conf_labels = ['id', 'device__name', 'device_type', 'ip_address', 'port_address'] |
|
413 | 386 | |
|
414 | 387 | kwargs['experiment_keys'] = exp_keys[1:] |
|
415 | 388 | kwargs['experiment'] = experiment |
|
416 | 389 | |
|
417 | 390 | kwargs['configuration_labels'] = conf_labels[1:] |
|
418 | 391 | kwargs['configuration_keys'] = conf_keys[1:] |
|
419 | 392 | kwargs['configurations'] = configurations #.values(*conf_keys) |
|
420 | 393 | |
|
421 | 394 | kwargs['title'] = 'Experiment' |
|
422 | 395 | kwargs['suptitle'] = 'Details' |
|
423 | 396 | |
|
424 | 397 | kwargs['button'] = 'Add Configuration' |
|
425 | 398 | |
|
426 | 399 | ###### SIDEBAR ###### |
|
427 | 400 | kwargs.update(sidebar(experiment=experiment)) |
|
428 | 401 | |
|
429 | 402 | return render(request, 'experiment.html', kwargs) |
|
430 | 403 | |
|
431 | 404 | |
|
432 | 405 | def experiment_new(request, id_camp=None): |
|
433 | 406 | |
|
434 | 407 | kwargs = {} |
|
435 | 408 | |
|
436 | 409 | if request.method == 'GET': |
|
437 | 410 | if 'template' in request.GET: |
|
438 | 411 | if request.GET['template']=='0': |
|
439 | 412 | form = NewForm(initial={'create_from':2}, |
|
440 | 413 | template_choices=Experiment.objects.filter(template=True).values_list('id', 'name')) |
|
441 | 414 | else: |
|
442 | 415 | kwargs['button'] = 'Create' |
|
443 | 416 | kwargs['configurations'] = Configuration.objects.filter(experiment=request.GET['template']) |
|
444 | 417 | kwargs['configuration_keys'] = ['name', 'device__name', 'device__ip_address', 'device__port_address'] |
|
445 | 418 | form = ExperimentForm(instance=Experiment.objects.get(pk=request.GET['template']), |
|
446 | 419 | initial={'template':False}) |
|
447 | 420 | elif 'blank' in request.GET: |
|
448 | 421 | kwargs['button'] = 'Create' |
|
449 | 422 | form = ExperimentForm() |
|
450 | 423 | else: |
|
451 | 424 | form = NewForm() |
|
452 | 425 | |
|
453 | 426 | if request.method == 'POST': |
|
454 | 427 | form = ExperimentForm(request.POST) |
|
455 | 428 | if form.is_valid(): |
|
456 | 429 | experiment = form.save() |
|
457 | 430 | |
|
458 | 431 | if 'template' in request.GET: |
|
459 | 432 | configurations = Configuration.objects.filter(experiment=request.GET['template'], type=0) |
|
460 | 433 | for conf in configurations: |
|
461 | 434 | conf.clone(experiment=experiment, template=False) |
|
462 | 435 | |
|
463 | 436 | return redirect('url_experiment', id_exp=experiment.id) |
|
464 | 437 | |
|
465 | 438 | kwargs['form'] = form |
|
466 | 439 | kwargs['title'] = 'Experiment' |
|
467 | 440 | kwargs['suptitle'] = 'New' |
|
468 | 441 | |
|
469 | 442 | return render(request, 'experiment_edit.html', kwargs) |
|
470 | 443 | |
|
471 | 444 | def experiment_edit(request, id_exp): |
|
472 | 445 | |
|
473 | 446 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
474 | 447 | |
|
475 | 448 | if request.method == 'GET': |
|
476 | 449 | form = ExperimentForm(instance=experiment) |
|
477 | 450 | |
|
478 | 451 | if request.method=='POST': |
|
479 | 452 | form = ExperimentForm(request.POST, instance=experiment) |
|
480 | 453 | |
|
481 | 454 | if form.is_valid(): |
|
482 | 455 | experiment = form.save() |
|
483 | 456 | return redirect('url_experiment', id_exp=experiment.id) |
|
484 | 457 | |
|
485 | 458 | kwargs = {} |
|
486 | 459 | kwargs['form'] = form |
|
487 | 460 | kwargs['title'] = 'Experiment' |
|
488 | 461 | kwargs['suptitle'] = 'Edit' |
|
489 | 462 | kwargs['button'] = 'Update' |
|
490 | 463 | |
|
491 | 464 | return render(request, 'experiment_edit.html', kwargs) |
|
492 | 465 | |
|
493 | 466 | def experiment_delete(request, id_exp): |
|
494 | 467 | |
|
495 | 468 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
496 | 469 | |
|
497 | 470 | if request.method=='POST': |
|
498 | 471 | if request.user.is_staff: |
|
499 | 472 | experiment.delete() |
|
500 | 473 | return redirect('url_experiments') |
|
501 | 474 | |
|
502 | 475 | return HttpResponse("Not enough permission to delete this object") |
|
503 | 476 | |
|
504 | 477 | kwargs = {'object':experiment, 'exp_active':'active', |
|
505 | 478 | 'url_cancel':'url_experiment', 'id_item':id_exp} |
|
506 | 479 | |
|
507 | 480 | return render(request, 'item_delete.html', kwargs) |
|
508 | 481 | |
|
509 | 482 | def dev_confs(request): |
|
510 | 483 | |
|
511 | 484 | configurations = Configuration.objects.all().order_by('type', 'device__device_type', 'experiment') |
|
512 | 485 | |
|
513 | 486 | # keys = ['id', 'device__device_type__name', 'device__name', 'experiment__campaign__name', 'experiment__name'] |
|
514 | 487 | |
|
515 | 488 | keys = ['id', 'device', 'experiment', 'type', 'programmed_date'] |
|
516 | 489 | |
|
517 | 490 | kwargs = {} |
|
518 | 491 | |
|
519 | 492 | kwargs['configuration_keys'] = keys[1:] |
|
520 | 493 | kwargs['configurations'] = configurations#.values(*keys) |
|
521 | 494 | |
|
522 | 495 | kwargs['title'] = 'Configuration' |
|
523 | 496 | kwargs['suptitle'] = 'List' |
|
524 | 497 | kwargs['button'] = 'New Configuration' |
|
525 | 498 | |
|
526 | 499 | return render(request, 'dev_confs.html', kwargs) |
|
527 | 500 | |
|
528 | 501 | def dev_conf(request, id_conf): |
|
529 | 502 | |
|
530 | 503 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
531 | 504 | |
|
532 | 505 | return redirect(conf.get_absolute_url()) |
|
533 | 506 | |
|
534 | 507 | |
|
535 | 508 | def dev_conf_new(request, id_exp=0, id_dev=0): |
|
536 | 509 | |
|
537 | 510 | initial = {} |
|
538 | 511 | |
|
539 | 512 | if id_exp<>0: |
|
540 | 513 | initial['experiment'] = id_exp |
|
541 | 514 | |
|
542 | 515 | if id_dev<>0: |
|
543 | 516 | initial['device'] = id_dev |
|
544 | 517 | |
|
545 | 518 | if request.method == 'GET': |
|
546 | 519 | if id_dev==0: |
|
547 | 520 | form = ConfigurationForm(initial=initial) |
|
548 | 521 | else: |
|
549 | 522 | device = Device.objects.get(pk=id_dev) |
|
550 | 523 | DevConfForm = CONF_FORMS[device.device_type.name] |
|
551 | 524 | |
|
552 | 525 | form = DevConfForm(initial=initial) |
|
553 | 526 | |
|
554 | 527 | if request.method == 'POST': |
|
555 | 528 | |
|
556 | 529 | device = Device.objects.get(pk=request.POST['device']) |
|
557 | 530 | DevConfForm = CONF_FORMS[device.device_type.name] |
|
558 | 531 | |
|
559 | 532 | form = DevConfForm(request.POST) |
|
560 | 533 | |
|
561 | 534 | if form.is_valid(): |
|
562 | 535 | dev_conf = form.save() |
|
563 | 536 | |
|
564 | 537 | return redirect('url_dev_confs') |
|
565 | 538 | |
|
566 | 539 | kwargs = {} |
|
567 | 540 | kwargs['id_exp'] = id_exp |
|
568 | 541 | kwargs['form'] = form |
|
569 | 542 | kwargs['title'] = 'Configuration' |
|
570 | 543 | kwargs['suptitle'] = 'New' |
|
571 | 544 | kwargs['button'] = 'Create' |
|
572 | 545 | |
|
573 | 546 | return render(request, 'dev_conf_edit.html', kwargs) |
|
574 | 547 | |
|
575 | 548 | def dev_conf_edit(request, id_conf): |
|
576 | 549 | |
|
577 | 550 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
578 | 551 | |
|
579 | 552 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
580 | 553 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
581 | 554 | |
|
582 | 555 | dev_conf = DevConfModel.objects.get(pk=id_conf) |
|
583 | 556 | |
|
584 | 557 | if request.method=='GET': |
|
585 | 558 | form = DevConfForm(instance=dev_conf) |
|
586 | 559 | |
|
587 | 560 | if request.method=='POST': |
|
588 | 561 | form = DevConfForm(request.POST, instance=dev_conf) |
|
589 | 562 | |
|
590 | 563 | if form.is_valid(): |
|
591 | 564 | form.save() |
|
592 | 565 | return redirect('url_dev_conf', id_conf=id_conf) |
|
593 | 566 | |
|
594 | 567 | kwargs = {} |
|
595 | 568 | kwargs['form'] = form |
|
596 | 569 | kwargs['title'] = 'Device Configuration' |
|
597 | 570 | kwargs['suptitle'] = 'Edit' |
|
598 | 571 | kwargs['button'] = 'Update' |
|
599 | 572 | |
|
600 | 573 | ###### SIDEBAR ###### |
|
601 | 574 | kwargs.update(sidebar(conf=conf)) |
|
602 | 575 | |
|
603 | 576 | return render(request, '%s_conf_edit.html' %conf.device.device_type.name, kwargs) |
|
604 | 577 | |
|
605 | 578 | def dev_conf_start(request, id_conf): |
|
606 | 579 | |
|
607 | 580 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
608 | 581 | |
|
609 | 582 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
610 | 583 | |
|
611 | 584 | conf = DevConfModel.objects.get(pk=id_conf) |
|
612 | 585 | |
|
613 | 586 | if conf.start_device(): |
|
614 | 587 | messages.success(request, conf.message) |
|
615 | 588 | else: |
|
616 | 589 | messages.error(request, conf.message) |
|
617 | 590 | |
|
618 | 591 | conf.status_device() |
|
619 | 592 | |
|
620 | 593 | return redirect(conf.get_absolute_url()) |
|
621 | 594 | |
|
622 | 595 | def dev_conf_stop(request, id_conf): |
|
623 | 596 | |
|
624 | 597 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
625 | 598 | |
|
626 | 599 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
627 | 600 | |
|
628 | 601 | conf = DevConfModel.objects.get(pk=id_conf) |
|
629 | 602 | |
|
630 | 603 | if conf.stop_device(): |
|
631 | 604 | messages.success(request, conf.message) |
|
632 | 605 | else: |
|
633 | 606 | messages.error(request, conf.message) |
|
634 | 607 | |
|
635 | 608 | conf.status_device() |
|
636 | 609 | |
|
637 | 610 | return redirect(conf.get_absolute_url()) |
|
638 | 611 | |
|
639 | 612 | def dev_conf_status(request, id_conf): |
|
640 | 613 | |
|
641 | 614 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
642 | 615 | |
|
643 | 616 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
644 | 617 | |
|
645 | 618 | conf = DevConfModel.objects.get(pk=id_conf) |
|
646 | 619 | |
|
647 | 620 | if conf.status_device(): |
|
648 | 621 | messages.success(request, conf.message) |
|
649 | 622 | else: |
|
650 | 623 | messages.error(request, conf.message) |
|
651 | 624 | |
|
652 | 625 | return redirect(conf.get_absolute_url()) |
|
653 | 626 | |
|
654 | 627 | |
|
655 | 628 | def dev_conf_write(request, id_conf): |
|
656 | 629 | |
|
657 | 630 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
658 | 631 | |
|
659 | 632 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
660 | 633 | |
|
661 | 634 | conf = DevConfModel.objects.get(pk=id_conf) |
|
662 | 635 | |
|
663 | 636 | answer = conf.write_device() |
|
664 | 637 | conf.status_device() |
|
665 | 638 | |
|
666 | 639 | if answer: |
|
667 | 640 | messages.success(request, conf.message) |
|
668 | 641 | |
|
669 | 642 | #Creating a historical configuration |
|
670 | 643 | conf.clone(type=0, template=False) |
|
671 | 644 | |
|
672 | 645 | #Original configuration |
|
673 | 646 | conf = DevConfModel.objects.get(pk=id_conf) |
|
674 | 647 | else: |
|
675 | 648 | messages.error(request, conf.message) |
|
676 | 649 | |
|
677 | 650 | return redirect(conf.get_absolute_url()) |
|
678 | 651 | |
|
679 | 652 | def dev_conf_read(request, id_conf): |
|
680 | 653 | |
|
681 | 654 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
682 | 655 | |
|
683 | 656 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
684 | 657 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
685 | 658 | |
|
686 | 659 | conf = DevConfModel.objects.get(pk=id_conf) |
|
687 | 660 | |
|
688 | 661 | if request.method=='GET': |
|
689 | 662 | |
|
690 | 663 | parms = conf.read_device() |
|
691 | 664 | conf.status_device() |
|
692 | 665 | |
|
693 | 666 | if not parms: |
|
694 | 667 | messages.error(request, conf.message) |
|
695 | 668 | return redirect(conf.get_absolute_url()) |
|
696 | 669 | |
|
697 | 670 | form = DevConfForm(initial=parms, instance=conf) |
|
698 | 671 | |
|
699 | 672 | if request.method=='POST': |
|
700 | 673 | form = DevConfForm(request.POST, instance=conf) |
|
701 | 674 | |
|
702 | 675 | if form.is_valid(): |
|
703 | 676 | form.save() |
|
704 | 677 | return redirect(conf.get_absolute_url()) |
|
705 | 678 | |
|
706 | 679 | messages.error(request, "Parameters could not be saved") |
|
707 | 680 | |
|
708 | 681 | kwargs = {} |
|
709 | 682 | kwargs['id_dev'] = conf.id |
|
710 | 683 | kwargs['form'] = form |
|
711 | 684 | kwargs['title'] = 'Device Configuration' |
|
712 | 685 | kwargs['suptitle'] = 'Parameters read from device' |
|
713 | 686 | kwargs['button'] = 'Save' |
|
714 | 687 | |
|
715 | 688 | ###### SIDEBAR ###### |
|
716 | 689 | kwargs.update(sidebar(conf=conf)) |
|
717 | 690 | |
|
718 | 691 | return render(request, '%s_conf_edit.html' %conf.device.device_type.name, kwargs) |
|
719 | 692 | |
|
720 | 693 | def dev_conf_import(request, id_conf): |
|
721 | 694 | |
|
722 | 695 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
723 | 696 | |
|
724 | 697 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
725 | 698 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
726 | 699 | conf = DevConfModel.objects.get(pk=id_conf) |
|
727 | 700 | |
|
728 | 701 | if request.method == 'GET': |
|
729 | 702 | file_form = UploadFileForm() |
|
730 | 703 | |
|
731 | 704 | if request.method == 'POST': |
|
732 | 705 | file_form = UploadFileForm(request.POST, request.FILES) |
|
733 | 706 | |
|
734 | 707 | if file_form.is_valid(): |
|
735 | 708 | |
|
736 | 709 | parms = conf.import_from_file(request.FILES['file']) |
|
737 | 710 | |
|
738 | 711 | if parms: |
|
739 | 712 | messages.success(request, "Parameters imported from: '%s'." %request.FILES['file'].name) |
|
740 | 713 | form = DevConfForm(initial=parms, instance=conf) |
|
741 | 714 | |
|
742 | 715 | kwargs = {} |
|
743 | 716 | kwargs['id_dev'] = conf.id |
|
744 | 717 | kwargs['form'] = form |
|
745 | 718 | kwargs['title'] = 'Device Configuration' |
|
746 | 719 | kwargs['suptitle'] = 'Parameters imported' |
|
747 | 720 | kwargs['button'] = 'Save' |
|
748 | 721 | kwargs['action'] = conf.get_absolute_url_edit() |
|
749 | 722 | kwargs['previous'] = conf.get_absolute_url() |
|
750 | 723 | |
|
751 | 724 | ###### SIDEBAR ###### |
|
752 | 725 | kwargs.update(sidebar(conf=conf)) |
|
753 | 726 | |
|
754 | 727 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) |
|
755 | 728 | |
|
756 | 729 | messages.error(request, "Could not import parameters from file") |
|
757 | 730 | |
|
758 | 731 | kwargs = {} |
|
759 | 732 | kwargs['id_dev'] = conf.id |
|
760 | 733 | kwargs['title'] = 'Device Configuration' |
|
761 | 734 | kwargs['form'] = file_form |
|
762 | 735 | kwargs['suptitle'] = 'Importing file' |
|
763 | 736 | kwargs['button'] = 'Import' |
|
764 | 737 | |
|
765 | 738 | kwargs.update(sidebar(conf=conf)) |
|
766 | 739 | |
|
767 | 740 | return render(request, 'dev_conf_import.html', kwargs) |
|
768 | 741 | |
|
769 | 742 | def dev_conf_export(request, id_conf): |
|
770 | 743 | |
|
771 | 744 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
772 | 745 | |
|
773 | 746 | DevConfModel = CONF_MODELS[conf.device.device_type.name] |
|
774 | 747 | |
|
775 | 748 | conf = DevConfModel.objects.get(pk=id_conf) |
|
776 | 749 | |
|
777 | 750 | if request.method == 'GET': |
|
778 | 751 | file_form = DownloadFileForm(conf.device.device_type.name) |
|
779 | 752 | |
|
780 | 753 | if request.method == 'POST': |
|
781 | 754 | file_form = DownloadFileForm(conf.device.device_type.name, request.POST) |
|
782 | 755 | |
|
783 | 756 | if file_form.is_valid(): |
|
784 | 757 | fields = conf.export_to_file(format = file_form.cleaned_data['format']) |
|
785 | 758 | |
|
786 | 759 | response = HttpResponse(content_type=fields['content_type']) |
|
787 | 760 | response['Content-Disposition'] = 'attachment; filename="%s"' %fields['filename'] |
|
788 | 761 | response.write(fields['content']) |
|
789 | 762 | |
|
790 | 763 | return response |
|
791 | 764 | |
|
792 | 765 | messages.error(request, "Could not export parameters") |
|
793 | 766 | |
|
794 | 767 | kwargs = {} |
|
795 | 768 | kwargs['id_dev'] = conf.id |
|
796 | 769 | kwargs['title'] = 'Device Configuration' |
|
797 | 770 | kwargs['form'] = file_form |
|
798 | 771 | kwargs['suptitle'] = 'Exporting file' |
|
799 | 772 | kwargs['button'] = 'Export' |
|
800 | 773 | |
|
801 | 774 | return render(request, 'dev_conf_export.html', kwargs) |
|
802 | 775 | |
|
803 | 776 | def dev_conf_delete(request, id_conf): |
|
804 | 777 | |
|
805 | 778 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
806 | 779 | |
|
807 | 780 | if request.method=='POST': |
|
808 | 781 | if request.user.is_staff: |
|
809 | 782 | id_exp = conf.experiment.id |
|
810 | 783 | conf.delete() |
|
811 | 784 | return redirect('url_experiment', id_exp=id_exp) |
|
812 | 785 | |
|
813 | 786 | return HttpResponse("Not enough permission to delete this object") |
|
814 | 787 | |
|
815 | 788 | kwargs = {'object':conf, 'conf_active':'active', |
|
816 | 789 | 'url_cancel':'url_dev_conf', 'id_item':id_conf} |
|
817 | 790 | |
|
818 | 791 | return render(request, 'item_delete.html', kwargs) |
|
819 | 792 | |
|
820 | 793 | |
|
821 | 794 | def sidebar(**kwargs): |
|
822 | 795 | |
|
823 | 796 | side_data = {} |
|
824 | 797 | |
|
825 | 798 | conf = kwargs.get('conf', None) |
|
826 | 799 | experiment = kwargs.get('experiment', None) |
|
827 | 800 | |
|
828 | 801 | if not experiment: |
|
829 | 802 | experiment = conf.experiment |
|
830 | 803 | |
|
831 | 804 | if experiment: |
|
832 | 805 | side_data['experiment'] = experiment |
|
833 | 806 | campaign = experiment.campaign_set.all() |
|
834 | 807 | if campaign: |
|
835 | 808 | side_data['campaign'] = campaign[0] |
|
836 | 809 | experiments = campaign[0].experiments.all() |
|
837 | 810 | else: |
|
838 | 811 | experiments = [experiment] |
|
839 | 812 | configurations = experiment.configuration_set.filter(type=0) |
|
840 | 813 | side_data['side_experiments'] = experiments |
|
841 | 814 | side_data['side_configurations'] = configurations |
|
842 | 815 | |
|
843 | 816 | return side_data |
|
844 | 817 | |
|
845 | 818 | |
|
846 | 819 | def operation(request, id_camp=None): |
|
847 | 820 | |
|
848 | 821 | if not id_camp: |
|
849 | 822 | campaigns = Campaign.objects.all().order_by('-start_date') |
|
850 | 823 | |
|
851 | 824 | if not campaigns: |
|
852 | 825 | kwargs = {} |
|
853 | 826 | kwargs['title'] = 'No Campaigns' |
|
854 | 827 | kwargs['suptitle'] = 'Empty' |
|
855 | 828 | return render(request, 'operation.html', kwargs) |
|
856 | 829 | |
|
857 | 830 | id_camp = campaigns[0].id |
|
858 | 831 | |
|
859 | 832 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
860 | 833 | |
|
861 | 834 | if request.method=='GET': |
|
862 | 835 | form = OperationForm(initial={'campaign': campaign.id}, length = 5) |
|
863 | 836 | |
|
864 | 837 | if request.method=='POST': |
|
865 | 838 | form = OperationForm(request.POST, initial={'campaign':campaign.id}, length = 5) |
|
866 | 839 | |
|
867 | 840 | if form.is_valid(): |
|
868 | 841 | return redirect('url_operation', id_camp=campaign.id) |
|
869 | 842 | #locations = Location.objects.filter(experiment__campaign__pk = campaign.id).distinct() |
|
870 | 843 | experiments = Experiment.objects.filter(campaign__pk=campaign.id) |
|
871 | for exs in experiments: | |
|
872 | exs.get_status() | |
|
844 | #for exs in experiments: | |
|
845 | # exs.get_status() | |
|
873 | 846 | locations= Location.objects.filter(experiment=experiments).distinct() |
|
874 | 847 | #experiments = [Experiment.objects.filter(location__pk=location.id).filter(campaign__pk=campaign.id) for location in locations] |
|
875 | 848 | kwargs = {} |
|
876 | 849 | #---Campaign |
|
877 | 850 | kwargs['campaign'] = campaign |
|
878 | 851 | kwargs['campaign_keys'] = ['name', 'start_date', 'end_date', 'tags', 'description'] |
|
879 | 852 | #---Experiment |
|
880 | 853 | keys = ['id', 'name', 'start_time', 'end_time', 'status'] |
|
881 | 854 | kwargs['experiment_keys'] = keys[1:] |
|
882 | 855 | kwargs['experiments'] = experiments |
|
883 | 856 | #---Radar |
|
884 | 857 | kwargs['locations'] = locations |
|
885 | 858 | #---Else |
|
886 | 859 | kwargs['title'] = 'Campaign' |
|
887 | 860 | kwargs['suptitle'] = campaign.name |
|
888 | 861 | kwargs['form'] = form |
|
889 | 862 | kwargs['button'] = 'Search' |
|
890 | 863 | kwargs['details'] = True |
|
891 | 864 | kwargs['search_button'] = True |
|
892 | 865 | |
|
893 | 866 | return render(request, 'operation.html', kwargs) |
|
894 | 867 | |
|
895 | 868 | def operation_search(request, id_camp=None): |
|
896 | 869 | |
|
897 | 870 | |
|
898 | 871 | if not id_camp: |
|
899 | 872 | campaigns = Campaign.objects.all().order_by('-start_date') |
|
900 | 873 | |
|
901 | 874 | if not campaigns: |
|
902 | 875 | return render(request, 'operation.html', {}) |
|
903 | 876 | |
|
904 | 877 | id_camp = campaigns[0].id |
|
905 | 878 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
906 | 879 | |
|
907 | 880 | if request.method=='GET': |
|
908 | 881 | form = OperationSearchForm(initial={'campaign': campaign.id}) |
|
909 | 882 | |
|
910 | 883 | if request.method=='POST': |
|
911 | 884 | form = OperationSearchForm(request.POST, initial={'campaign':campaign.id}) |
|
912 | 885 | |
|
913 | 886 | if form.is_valid(): |
|
914 | 887 | return redirect('url_operation', id_camp=campaign.id) |
|
915 | 888 | |
|
916 | 889 | #locations = Location.objects.filter(experiment__campaign__pk = campaign.id).distinct() |
|
917 | 890 | experiments = Experiment.objects.filter(campaign__pk=campaign.id) |
|
918 | for exs in experiments: | |
|
919 | exs.get_status() | |
|
891 | #for exs in experiments: | |
|
892 | # exs.get_status() | |
|
920 | 893 | locations= Location.objects.filter(experiment=experiments).distinct() |
|
921 | 894 | form = OperationSearchForm(initial={'campaign': campaign.id}) |
|
922 | 895 | |
|
923 | 896 | kwargs = {} |
|
924 | 897 | #---Campaign |
|
925 | 898 | kwargs['campaign'] = campaign |
|
926 | 899 | kwargs['campaign_keys'] = ['name', 'start_date', 'end_date', 'tags', 'description'] |
|
927 | 900 | #---Experiment |
|
928 | 901 | keys = ['id', 'name', 'start_time', 'end_time', 'status'] |
|
929 | 902 | kwargs['experiment_keys'] = keys[1:] |
|
930 | 903 | kwargs['experiments'] = experiments |
|
931 | 904 | #---Radar |
|
932 | 905 | kwargs['locations'] = locations |
|
933 | 906 | #---Else |
|
934 | 907 | kwargs['title'] = 'Campaign' |
|
935 | 908 | kwargs['suptitle'] = campaign.name |
|
936 | 909 | kwargs['form'] = form |
|
937 | 910 | kwargs['button'] = 'Select' |
|
938 | 911 | kwargs['details'] = True |
|
939 | 912 | kwargs['search_button'] = False |
|
940 | 913 | |
|
941 | 914 | return render(request, 'operation.html', kwargs) |
|
942 | 915 | |
|
943 | 916 | |
|
944 | 917 | def radar_play(request, id_camp, id_radar): |
|
918 | campaign = get_object_or_404(Campaign, pk = id_camp) | |
|
919 | radar = get_object_or_404(Location, pk = id_radar) | |
|
920 | experiments = Experiment.objects.filter(campaign=campaign).filter(location=radar) | |
|
921 | current_time = datetime.today() | |
|
922 | #exp = RunningExperiment( | |
|
923 | # radar = purchase_request.user_id, | |
|
924 | # running_experiment = purchase_request, | |
|
925 | # status = , | |
|
926 | # ) | |
|
927 | #new_pos.append(exp) | |
|
928 | #exp.save() | |
|
945 | 929 | |
|
946 | 930 | route = request.META['HTTP_REFERER'] |
|
947 | 931 | route = str(route) |
|
948 | 932 | if 'search' in route: |
|
949 | 933 | return HttpResponseRedirect(reverse('url_operation_search', args=[id_camp])) |
|
950 | 934 | else: |
|
951 | 935 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
952 | 936 | |
|
937 | ||
|
953 | 938 | def radar_stop(request, id_camp, id_radar): |
|
954 | 939 | |
|
955 | 940 | route = request.META['HTTP_REFERER'] |
|
956 | 941 | route = str(route) |
|
957 | 942 | if 'search' in route: |
|
958 | 943 | return HttpResponseRedirect(reverse('url_operation_search', args=[id_camp])) |
|
959 | 944 | else: |
|
960 | 945 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
961 | 946 | |
|
947 | ||
|
948 | def radar_refresh(request, id_camp, id_radar): | |
|
949 | ||
|
950 | campaign = get_object_or_404(Campaign, pk = id_camp) | |
|
951 | radar = get_object_or_404(Location, pk = id_radar) | |
|
952 | experiments = Experiment.objects.filter(campaign=campaign).filter(location=radar) | |
|
953 | for exs in experiments: | |
|
954 | exs.get_status() | |
|
955 | ||
|
956 | route = request.META['HTTP_REFERER'] | |
|
957 | route = str(route) | |
|
958 | if 'search' in route: | |
|
959 | return HttpResponseRedirect(reverse('url_operation_search', args=[id_camp])) | |
|
960 | else: | |
|
961 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) | |
|
962 |
General Comments 0
You need to be logged in to leave comments.
Login now