Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Juan camilo #11

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
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

![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)



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**
Expand All @@ -17,4 +47,4 @@ pipenv run python {command}
Example:
```bash
pipenv run python manage.py makemigrations
```
```
31 changes: 31 additions & 0 deletions measurements/logic/measurements_logic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@


from ..models import Variable
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):
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)
measurement.value =new_plc["value"]
measurement.unit =new_plc["unit"]
measurement.place =new_plc["place"]
measurement.save()
return measurement

def delete_measurements(var_pk):
measurement = get_measurement(var_pk)
measurement.delete()
return measurement
7 changes: 7 additions & 0 deletions measurements/urls.py
Original file line number Diff line number Diff line change
@@ -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('<int:pk>', views.measurement_view, name='measurement_view'),
]
37 changes: 37 additions & 0 deletions measurements/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
from django.shortcuts import render
from .logic import measurements_logic as ml
from django.http import HttpResponse
from django.core import serializers
import json
from django.views.decorators.csrf import csrf_exempt


@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_measurement(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):
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')
if request.method =='DELETE':
measurement_dto = ml.delete_measurements(pk)
measurement = serializers.serialize('json',[measurement_dto,])
return HttpResponse(measurement,'application/json')
8 changes: 4 additions & 4 deletions monitoring/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
"NAME": "<nombrebd>",
"USER": "<usuario>",
"PASSWORD": "<clave>",
"NAME": "arquitectura",
"USER": "camilo",
"PASSWORD": "Camilo15052004",
"HOST": "localhost",
"PORT": "",
"PORT": "5432",
}
}

Expand Down
3 changes: 2 additions & 1 deletion monitoring/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home),
path('home/', views.home),
path('variables/', include('variables.urls')),
path('measurements/', include('measurements.urls')),
]
2 changes: 1 addition & 1 deletion monitoring/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.http import HttpResponse

def home(request):
return HttpResponse("Hello world! Django views")
return HttpResponse("Hello world! Django 222")