From 41b29b3d8fa82fdc17452138774bad9661a41d0c Mon Sep 17 00:00:00 2001 From: "j.burbanon" Date: Thu, 10 Feb 2022 23:12:15 -0500 Subject: [PATCH 1/7] hola From fe3d249f6a9022f60441da7e1ac3c398e16b4c01 Mon Sep 17 00:00:00 2001 From: "j.burbanon" Date: Sun, 13 Feb 2022 16:42:44 -0500 Subject: [PATCH 2/7] Cambios Measurements --- measurements/logic/measurements_logic.py | 17 ++++++++++++++ measurements/urls.py | 7 ++++++ measurements/views.py | 30 ++++++++++++++++++++++++ monitoring/settings.py | 8 +++---- monitoring/urls.py | 2 +- 5 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 measurements/logic/measurements_logic.py create mode 100644 measurements/urls.py diff --git a/measurements/logic/measurements_logic.py b/measurements/logic/measurements_logic.py new file mode 100644 index 0000000..cbc275b --- /dev/null +++ b/measurements/logic/measurements_logic.py @@ -0,0 +1,17 @@ +import measurements +from ..models import Measurement +def get_measurement(var_pk): + measurement = Measurement.objects.get(pk=var_pk) + return measurement + +def get_measurements(): + measurements = Measurement.objects.all() + return measurements +def create_measurement(var): + measurement = Measurement(variable=var["variable"],value=var["value"],unit=var["unit"],place=var["place"],dateTime=var["dateTime"]) + measurement.save() +def update_measurement(var_pk,new_var): + measurement = get_measurement(var_pk) + measurement.unit =new_var["unit"] + measurement.save() + return measurement \ No newline at end of file diff --git a/measurements/urls.py b/measurements/urls.py new file mode 100644 index 0000000..df6f5ca --- /dev/null +++ b/measurements/urls.py @@ -0,0 +1,7 @@ +from django.contrib import admin +from django.urls import path +from . import views +urlpatterns = [ + path('', views.measurements_view, name='measurements_view'), + path('', views.measurement_view, name='measurement_view'), +] \ No newline at end of file diff --git a/measurements/views.py b/measurements/views.py index 91ea44a..21667d7 100644 --- a/measurements/views.py +++ b/measurements/views.py @@ -1,3 +1,33 @@ from django.shortcuts import render +from .logic import measurements_logic as ml +from django.http import HttpResponse +from django.core import serializers +import json + +@csrf_exempt +def measurements_view(request): + if request.method == 'GET': + id = request.GET.get("id",None) + if id: + measurement_dto = ml.get_measurement(id) + measurement =serializers.serialize('json',[measurement_dto,]) + return HttpResponse(measurement, 'application/json') + else: + measurements_dto = ml.get_measurements() + measurements = serializers.serialize('json',measurements_dto) + return HttpResponse(measurements,'application/json') + if request.method == 'POST': + measurement_dto = ml.create_measurements(json.loads(request.body)) + measurement = serializers.serialize('json', [measurement_dto,]) + return HttpResponse(measurement,'application/json') # Create your views here. +def measurement_view(request,pk): + if request.method =='GET': + measurement_dto = ml.get_measurement(pk) + measurement = serializers.serialize('json',[measurement_dto,]) + return HttpResponse(measurement,'application/json') + if request.method =='PUT': + measurement_dto = ml.update_measurement(pk,json.loads[request.body]) + measurement =serializers.serialize('json',[measurement_dto,]) + return HttpResponse(measurement,'application/json') \ No newline at end of file diff --git a/monitoring/settings.py b/monitoring/settings.py index e9f59b8..ec749a7 100644 --- a/monitoring/settings.py +++ b/monitoring/settings.py @@ -78,11 +78,11 @@ DATABASES = { "default": { "ENGINE": "django.db.backends.postgresql_psycopg2", - "NAME": "", - "USER": "", - "PASSWORD": "", + "NAME": "arquitectura", + "USER": "camilo", + "PASSWORD": "Camilo15052004", "HOST": "localhost", - "PORT": "", + "PORT": "5432", } } diff --git a/monitoring/urls.py b/monitoring/urls.py index f095723..f6469cf 100644 --- a/monitoring/urls.py +++ b/monitoring/urls.py @@ -20,6 +20,6 @@ urlpatterns = [ path('admin/', admin.site.urls), - path('', views.home), + path('home/', views.home), path('variables/', include('variables.urls')), ] From 959bf3e1cb92cf55daba0d4cdae4cffb18b9942f Mon Sep 17 00:00:00 2001 From: "j.burbanon" Date: Sun, 13 Feb 2022 17:40:28 -0500 Subject: [PATCH 3/7] Funciona GET --- measurements/views.py | 2 ++ monitoring/urls.py | 1 + monitoring/views.py | 2 +- 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/measurements/views.py b/measurements/views.py index 21667d7..d611211 100644 --- a/measurements/views.py +++ b/measurements/views.py @@ -3,6 +3,7 @@ from django.http import HttpResponse from django.core import serializers import json +from django.views.decorators.csrf import csrf_exempt @csrf_exempt @@ -22,6 +23,7 @@ def measurements_view(request): measurement = serializers.serialize('json', [measurement_dto,]) return HttpResponse(measurement,'application/json') # Create your views here. +@csrf_exempt def measurement_view(request,pk): if request.method =='GET': measurement_dto = ml.get_measurement(pk) diff --git a/monitoring/urls.py b/monitoring/urls.py index f6469cf..a024ec3 100644 --- a/monitoring/urls.py +++ b/monitoring/urls.py @@ -22,4 +22,5 @@ path('admin/', admin.site.urls), path('home/', views.home), path('variables/', include('variables.urls')), + path('measurements/', include('measurements.urls')), ] diff --git a/monitoring/views.py b/monitoring/views.py index 4baea3f..cffaa0f 100644 --- a/monitoring/views.py +++ b/monitoring/views.py @@ -1,4 +1,4 @@ from django.http import HttpResponse def home(request): - return HttpResponse("Hello world! Django views") \ No newline at end of file + return HttpResponse("Hello world! Django 222") \ No newline at end of file From 77a43632bf676d0a49d25000ca315474c5d7e98f Mon Sep 17 00:00:00 2001 From: "j.burbanon" Date: Sun, 13 Feb 2022 19:44:38 -0500 Subject: [PATCH 4/7] Funciona DELETE y UPDATE --- measurements/logic/measurements_logic.py | 16 ++++++++++++---- measurements/views.py | 9 +++++++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/measurements/logic/measurements_logic.py b/measurements/logic/measurements_logic.py index cbc275b..1a3aded 100644 --- a/measurements/logic/measurements_logic.py +++ b/measurements/logic/measurements_logic.py @@ -1,4 +1,4 @@ -import measurements + from ..models import Measurement def get_measurement(var_pk): measurement = Measurement.objects.get(pk=var_pk) @@ -10,8 +10,16 @@ def get_measurements(): def create_measurement(var): measurement = Measurement(variable=var["variable"],value=var["value"],unit=var["unit"],place=var["place"],dateTime=var["dateTime"]) measurement.save() -def update_measurement(var_pk,new_var): + +def update_measurement(var_pk,new_plc): measurement = get_measurement(var_pk) - measurement.unit =new_var["unit"] + measurement.value =new_plc["value"] + measurement.unit =new_plc["unit"] + measurement.place =new_plc["place"] measurement.save() - return measurement \ No newline at end of file + return measurement + +def delete_measurements(var_pk): + measurement = get_measurement(var_pk) + measurement.delete() + return measurement diff --git a/measurements/views.py b/measurements/views.py index d611211..38cb13e 100644 --- a/measurements/views.py +++ b/measurements/views.py @@ -22,6 +22,7 @@ def measurements_view(request): measurement_dto = ml.create_measurements(json.loads(request.body)) measurement = serializers.serialize('json', [measurement_dto,]) return HttpResponse(measurement,'application/json') + # Create your views here. @csrf_exempt def measurement_view(request,pk): @@ -30,6 +31,10 @@ def measurement_view(request,pk): measurement = serializers.serialize('json',[measurement_dto,]) return HttpResponse(measurement,'application/json') if request.method =='PUT': - measurement_dto = ml.update_measurement(pk,json.loads[request.body]) + measurement_dto = ml.update_measurement(pk,json.loads(request.body)) measurement =serializers.serialize('json',[measurement_dto,]) - return HttpResponse(measurement,'application/json') \ No newline at end of file + return HttpResponse(measurement,'application/json') + if request.method =='DELETE': + measurement_dto = ml.delete_measurements(pk) + measurement = serializers.serialize('json',[measurement_dto,]) + return HttpResponse(measurement,'application/json') From 22a1df0aedc97f04cb9067117b207040a96bbd75 Mon Sep 17 00:00:00 2001 From: "j.burbanon" Date: Sun, 13 Feb 2022 23:08:58 -0500 Subject: [PATCH 5/7] Create --- measurements/logic/measurements_logic.py | 8 +++++++- measurements/views.py | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/measurements/logic/measurements_logic.py b/measurements/logic/measurements_logic.py index 1a3aded..4f73610 100644 --- a/measurements/logic/measurements_logic.py +++ b/measurements/logic/measurements_logic.py @@ -1,5 +1,8 @@ + +from ..models import Variable from ..models import Measurement + def get_measurement(var_pk): measurement = Measurement.objects.get(pk=var_pk) return measurement @@ -7,9 +10,12 @@ def get_measurement(var_pk): def get_measurements(): measurements = Measurement.objects.all() return measurements + def create_measurement(var): - measurement = Measurement(variable=var["variable"],value=var["value"],unit=var["unit"],place=var["place"],dateTime=var["dateTime"]) + foreign_key=Variable.objects.get(pk=var["variable"]) + measurement = Measurement(variable=foreign_key, value=var["value"], unit=var["unit"], place=var["place"], dateTime=var["date"]) measurement.save() + return measurement def update_measurement(var_pk,new_plc): measurement = get_measurement(var_pk) diff --git a/measurements/views.py b/measurements/views.py index 38cb13e..4efcfdc 100644 --- a/measurements/views.py +++ b/measurements/views.py @@ -19,9 +19,9 @@ def measurements_view(request): measurements = serializers.serialize('json',measurements_dto) return HttpResponse(measurements,'application/json') if request.method == 'POST': - measurement_dto = ml.create_measurements(json.loads(request.body)) + measurement_dto = ml.create_measurement(json.loads(request.body)) measurement = serializers.serialize('json', [measurement_dto,]) - return HttpResponse(measurement,'application/json') + return HttpResponse(measurement, 'application/json') # Create your views here. @csrf_exempt From 34cfa88fe6a139cbb3c4cac80d7582daab93a455 Mon Sep 17 00:00:00 2001 From: JuanCamiloEduardo <69642359+JuanCamiloEduardo@users.noreply.github.com> Date: Sun, 13 Feb 2022 22:47:37 -0600 Subject: [PATCH 6/7] Update README.md --- README.md | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 81ef390..3a23f86 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,31 @@ +GET ALL +![Captura de pantalla (503)](https://user-images.githubusercontent.com/69642359/153801631-92601fb2-de7e-472e-af3f-ff715480fbfd.png) + + + +GET BY ID + +![Captura de pantalla (504)](https://user-images.githubusercontent.com/69642359/153801640-00ba949e-a89a-41a2-b926-008a56188b6d.png) + + +CREATE + + + + +UPDATE +![Captura de pantalla (506)](https://user-images.githubusercontent.com/69642359/153801655-9b1f3643-f867-4461-aeda-3b80e07029a1.png) +![Captura de pantalla (507)](https://user-images.githubusercontent.com/69642359/153801669-738e429d-b327-4bfa-9939-e99228ef8e73.png) + + + +DELETE + +![Captura de pantalla (508)](https://user-images.githubusercontent.com/69642359/153801680-da13e6a3-558a-450a-b22d-a5f0424fcd09.png) +![Captura de pantalla (509)](https://user-images.githubusercontent.com/69642359/153801686-7ffcfdc7-821f-447f-8dfe-c4597f5ff2de.png) + + + # Monitoring Views Workshop * if you want a easy installation use **pipenv\*** else use **requirements.txt** @@ -17,4 +45,4 @@ pipenv run python {command} Example: ```bash pipenv run python manage.py makemigrations -``` \ No newline at end of file +``` From 51c635191b2c8961ba0a751386690e9e3c009710 Mon Sep 17 00:00:00 2001 From: JuanCamiloEduardo <69642359+JuanCamiloEduardo@users.noreply.github.com> Date: Sun, 13 Feb 2022 22:56:26 -0600 Subject: [PATCH 7/7] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 3a23f86..ef23824 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,8 @@ GET BY ID CREATE +![Captura de pantalla (510)](https://user-images.githubusercontent.com/69642359/153802478-77a4b1b7-7e7f-437a-a43d-14c19bf10452.png) +![Captura de pantalla (511)](https://user-images.githubusercontent.com/69642359/153802549-b7e69562-d831-4bb2-8ca7-39813ee9e656.png)