From 4d6f73ac63d3dcaefa7aaf22b71311663464852f Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Fri, 25 Jun 2021 14:25:14 +0200 Subject: [PATCH] Finish version --- app/api/serializers.py | 2 +- app/api/views.py | 7 ++++--- app/web/forms.py | 11 +++++++++++ app/web/templates/pages/add-news.html | 21 +++++++++++---------- app/web/views.py | 21 +++++++++++++++++++-- db.sqlite3 | Bin 135168 -> 135168 bytes static/js/home.js | 14 +++++++++++++- 7 files changed, 59 insertions(+), 17 deletions(-) create mode 100644 app/web/forms.py diff --git a/app/api/serializers.py b/app/api/serializers.py index 692267a..efe9779 100644 --- a/app/api/serializers.py +++ b/app/api/serializers.py @@ -5,5 +5,5 @@ from rest_framework import serializers class NewsSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = News - fields = ['title', 'url', 'votes', 'created'] + fields = ['id', 'title', 'url', 'votes', 'created'] diff --git a/app/api/views.py b/app/api/views.py index 396fb91..3518582 100644 --- a/app/api/views.py +++ b/app/api/views.py @@ -2,6 +2,7 @@ from app.api.models import News from rest_framework import viewsets, mixins from app.api.serializers import NewsSerializer from rest_framework.response import Response +import json class NewsViewSet( mixins.UpdateModelMixin, @@ -19,11 +20,11 @@ class NewsViewSet( mixins.UpdateModelMixin, return Response(serializer.data) - def put(self, request, pk=None): + def put(self, request): # Update - my_news = News.objects.get(pk) + my_news = News.objects.get(pk=request.data['id']) my_news.votes = my_news.votes + 1 my_news.save() # Devolvemos la noticia modificada - serializer = News(my_news) + serializer = NewsSerializer(my_news) return Response(serializer.data) \ No newline at end of file diff --git a/app/web/forms.py b/app/web/forms.py new file mode 100644 index 0000000..2d443c3 --- /dev/null +++ b/app/web/forms.py @@ -0,0 +1,11 @@ +from django import forms + +class NewsForm(forms.Form): + title = forms.CharField( + label='Titulo', + required=True + ) + url = forms.URLField( + label='Ruta', + required=True + ) \ No newline at end of file diff --git a/app/web/templates/pages/add-news.html b/app/web/templates/pages/add-news.html index 566549b..0ddfb99 100644 --- a/app/web/templates/pages/add-news.html +++ b/app/web/templates/pages/add-news.html @@ -1,10 +1,11 @@ - - - - - Title - - - - - \ No newline at end of file +{% extends 'layouts/base.html' %} +{% block title %}AƱadir nueva noticia{% endblock %} +{% block main %} +
+ {% csrf_token %} + {{ news_form.as_p }} +

+ +

+
+{% endblock %} \ No newline at end of file diff --git a/app/web/views.py b/app/web/views.py index df08f64..f67e432 100644 --- a/app/web/views.py +++ b/app/web/views.py @@ -1,4 +1,6 @@ -from django.shortcuts import render +from django.shortcuts import render, redirect, reverse +from app.api.models import News +from app.web.forms import NewsForm def home(request): @@ -6,4 +8,19 @@ def home(request): def add_news(request): - return render(request, 'pages/add-news.html', {}) \ No newline at end of file + mi_form = NewsForm() + # Validamos el formulario + if request.method == 'POST': + mi_form = NewsForm(request.POST) + # Muestro los errores + if mi_form.is_valid(): + # Guardo + mi_new_news = News() + mi_new_news.title = mi_form.cleaned_data.get('title') + mi_new_news.url = mi_form.cleaned_data.get('url') + mi_new_news.save() + # Redirecciono a Home + return redirect(reverse('list')) + return render(request, 'pages/add-news.html', { + 'news_form': mi_form + }) \ No newline at end of file diff --git a/db.sqlite3 b/db.sqlite3 index 9198ecd1a84133a201d6247ba522f09c324e040d..d043ee7ab8dd908bba66f76f4eace2513e77f469 100644 GIT binary patch delta 301 zcmZozz|pXPV}dlJ|3n#QM*ocoQTmJ=&Aaut@78B@TOiKQ&cMO*o`F|`UypAC?>pXY z{O9;*^Ss~Kc%8?|oP&))TAR_aB(=B%2r^1a3as??b2Ez)^^)^*S&a;g40R36bd5|E z3=OSJjI0cd^$ZOyOe_r=xf$6R5`pCOn){4d(jst`Mj(|2rdFngdPe3(=9U)Qx7}q7 zWMpArU{IgfsLYz0_hfM;n`28Jg%>7+V?| KnJ;_5XaE3fO-@e$ delta 100 zcmV-q0Gt1Spa_7V2#^~ADv=yR0V=UzS}y?wgS#)cyDtGopb-E700j-U01tBy2M(|e z?hUrFfus$$w%q|y0S9UV1^``A2$wbA0ZW&G-vJs4feHZ!0AYa(2$wP40cw|=-2ob= G-~llB0v`VW diff --git a/static/js/home.js b/static/js/home.js index 0ad076e..af2da3e 100644 --- a/static/js/home.js +++ b/static/js/home.js @@ -16,6 +16,18 @@ new Vue({ .then((json) => { this.news = json; }); - } + }, + votar: function (id) { + fetch('/api/v1/news/', { + headers: { + 'Content-type': 'application/json' + }, + method: 'PUT', + body: JSON.stringify({ id: id }) + }) + .then((json) => { + this.getNews(); + }); + } } }) \ No newline at end of file