From 848da34b74b0c10e8035b33f994e383a240295e1 Mon Sep 17 00:00:00 2001 From: shx Date: Thu, 7 Feb 2019 17:35:59 -0200 Subject: [PATCH] app missingpeople --- apps/missingpeople/__init__.py | 0 apps/missingpeople/admin.py | 5 ++ apps/missingpeople/apps.py | 5 ++ apps/missingpeople/migrations/0001_initial.py | 22 ++++++ apps/missingpeople/migrations/__init__.py | 0 apps/missingpeople/models.py | 10 +++ apps/missingpeople/tests.py | 3 + apps/missingpeople/urls.py | 9 +++ apps/missingpeople/views.py | 67 +++++++++++++++++++ core/settings.py | 3 +- core/urls.py | 7 +- 11 files changed, 128 insertions(+), 3 deletions(-) create mode 100644 apps/missingpeople/__init__.py create mode 100644 apps/missingpeople/admin.py create mode 100644 apps/missingpeople/apps.py create mode 100644 apps/missingpeople/migrations/0001_initial.py create mode 100644 apps/missingpeople/migrations/__init__.py create mode 100644 apps/missingpeople/models.py create mode 100644 apps/missingpeople/tests.py create mode 100644 apps/missingpeople/urls.py create mode 100644 apps/missingpeople/views.py diff --git a/apps/missingpeople/__init__.py b/apps/missingpeople/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/missingpeople/admin.py b/apps/missingpeople/admin.py new file mode 100644 index 0000000..0857ac7 --- /dev/null +++ b/apps/missingpeople/admin.py @@ -0,0 +1,5 @@ +from django.contrib import admin +from .models import People +# Register your models here. + +admin.site.register(People) \ No newline at end of file diff --git a/apps/missingpeople/apps.py b/apps/missingpeople/apps.py new file mode 100644 index 0000000..615f367 --- /dev/null +++ b/apps/missingpeople/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class MissingpeopleConfig(AppConfig): + name = 'missingpeople' diff --git a/apps/missingpeople/migrations/0001_initial.py b/apps/missingpeople/migrations/0001_initial.py new file mode 100644 index 0000000..d819024 --- /dev/null +++ b/apps/missingpeople/migrations/0001_initial.py @@ -0,0 +1,22 @@ +# Generated by Django 2.1.5 on 2019-02-06 06:31 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='People', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('publicdate', models.CharField(max_length=999)), + ('name', models.CharField(max_length=999)), + ], + ), + ] diff --git a/apps/missingpeople/migrations/__init__.py b/apps/missingpeople/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/missingpeople/models.py b/apps/missingpeople/models.py new file mode 100644 index 0000000..1602ee3 --- /dev/null +++ b/apps/missingpeople/models.py @@ -0,0 +1,10 @@ +from django.db import models + +# Create your models here. + +class People(models.Model): + publicdate = models.CharField(max_length=999) + name = models.CharField(max_length=999) + + def __str__(self): + return self.name \ No newline at end of file diff --git a/apps/missingpeople/tests.py b/apps/missingpeople/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/apps/missingpeople/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/apps/missingpeople/urls.py b/apps/missingpeople/urls.py new file mode 100644 index 0000000..c903b55 --- /dev/null +++ b/apps/missingpeople/urls.py @@ -0,0 +1,9 @@ +from django.urls import path +from apps.missingpeople.views import missingpeople, createcsv + +app_name = 'missingpeople' + +urlpatterns = [ + path('missingpeople', missingpeople, name='missingpeople'), + path('createcsv', createcsv, name='createcsv'), +] \ No newline at end of file diff --git a/apps/missingpeople/views.py b/apps/missingpeople/views.py new file mode 100644 index 0000000..470de97 --- /dev/null +++ b/apps/missingpeople/views.py @@ -0,0 +1,67 @@ +import datetime +from bs4 import BeautifulSoup +import requests +from django.db.models import Model +import csv +from django.http import HttpResponse +from .models import People +from django.shortcuts import render + +# Create your views here. + +url = 'http://brumadinho.vale.com/listagem-pessoas-sem-contato.html' + +#Esta view tem a funcao de ler os nomes das vitimas com beautifulsoap e inseri o nome e as informacoes da ultima atualizacao no banco de dados +def missingpeople(request): + page = requests.get(url) + if page.status_code == 200: + soup = BeautifulSoup(page.text, 'html.parser') + missinglist = soup.find_all('li') + lastupdate = soup.find_all('p') + try: + p = People.objects.latest('id') + except People.DoesNotExist: + p = None + if p == None: + i=0 + for person in missinglist: + p = People(publicdate=lastupdate[3].text, name=person.text) + p.save() + i += 1 + html = missinglist + return HttpResponse(html) + else: + if lastupdate[3].text == p.publicdate: + return HttpResponse(People.objects.all().values_list('name', flat=True)) + else: + #inseri as pessoas uma a uma para fazer a contagem + i = 0 + for person in missinglist: + p = People(publicdate=lastupdate[3].text, name=person.text) + p.save() + i += 1 + html = missinglist + #print('Pessoas Desaparecidas: ', i) + return HttpResponse(html) + else: + return HttpResponse('

Page was found

') + +def createcsv(request): + page = requests.get(url) + if page.status_code == 200: + soup = BeautifulSoup(page.text, 'html.parser') + missinglist = soup.find_all('li') + response = HttpResponse(content_type='text/csv') + response['Content-Disposition'] = 'attachment; filename="somefilename.csv"' + writer = csv.writer(response) + + i = 0 + for person in missinglist: + writer.writerow(person.text) + #p = People(publicdate=lastupdate[3].text, name=person.text) + #p.save() + i += 1 + + return HttpResponse(response) + else: + return HttpResponse('

Page was found

') \ No newline at end of file diff --git a/core/settings.py b/core/settings.py index e9a154d..0f27290 100644 --- a/core/settings.py +++ b/core/settings.py @@ -18,7 +18,7 @@ # Application definition INSTALLED_APPS = [ - # 'django.contrib.admin', + 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', @@ -26,6 +26,7 @@ 'django.contrib.staticfiles', 'apps.api', 'apps.map', + 'apps.missingpeople', ] MIDDLEWARE = [ diff --git a/core/urls.py b/core/urls.py index b2a5dbc..8b10a94 100644 --- a/core/urls.py +++ b/core/urls.py @@ -1,7 +1,10 @@ +from django.contrib import admin from django.urls import path, include urlpatterns = [ - # path('admin/', admin.site.urls), + path('admin/', admin.site.urls), path('api/', include('apps.api.urls', namespace='api')), - path('', include('apps.map.urls', namespace='map')) + path('', include('apps.map.urls', namespace='map')), + path('', include('apps.missingpeople.urls', namespace='missingpeople')) + ]