Commit b9164254 authored by Mario Chirinos's avatar Mario Chirinos

urls

parent e9959300
from django.core.management.base import BaseCommand, CommandError
from catalog.models import News, Publisher, Topic
from django.db.models import Q
import os
import json
import datetime
from django.utils import timezone
import dateutil.parser
class Command(BaseCommand):
help = 'Update database'
def add_arguments(self, parser):
parser.add_argument('basedir', nargs=1, type=str)
def handle(self, *args, **options):
os.chdir(options['basedir'][0])
publisherList = os.listdir(options['basedir'][0])
for p in publisherList:
print p
os.chdir(p)
publisher = Publisher.objects.all().filter(shortName=p)[0]
news = News.objects.all().filter(publisher=publisher.id).order_by("-date")
# print publisher.id
minYear = 0
lastDate = datetime.datetime(1950,1,1)
# print news.count()
if news.count()>0:
minYear = news[0].date.year
lastDate = news[0].date
yearList = [ int(y) for y in os.listdir('.')]
# print "LastDate"+str(lastDate)
print yearList
for y in yearList:
if y >=minYear:
os.chdir(str(y))
print os.getcwd()
filesList = os.listdir(".")
for f in filesList:
# print "file: " + f
fileDate = datetime.datetime.strptime(f[:f.find(".")], "%Y-%m-%d").date()
if fileDate >= lastDate.date():
# print "fileDate: " + str(fileDate)
with open(f) as data_file:
try:
data = json.load(data_file)
for d in data:
# if d['date'].find("+") >=0:
# date =d['date'][:d['date'].find("+")].replace("T", " ")
# newsDate = datetime.datetime.strptime(date, "%Y-%m-%d %H:%M:%S")
# else:
# date = d['date']
# newsDate = datetime.datetime.strptime(date, "%Y-%m-%d")
# print d['date']
# print d['title']
newsDate = dateutil.parser.parse(d['date'])
# newsDate = timezone.make_aware(newsDate, timezone.get_current_timezone())
# print "newsDate:"
# print newsDate
if News.objects.all().filter(Q(publisher=publisher.id)&Q(title=d['title'])&Q(date__gte=newsDate)).count() == 0:
# print "no news match"
if d['title'] == None:
d['title'] = "Sin Titulo"
if len(d['title']) >= 512:
d['title'] = d['title'][:500]
print p + " "+ str(newsDate) + ": " + d['title']
# print type(d['topic'])
news = News()
news.publisher = publisher
# print "title"
news.title = d['title']
# print "text"
news.text = d['text']
# print "url"
news.url = d['url']
# print "date"
news.date = newsDate
news.save()
# print "TOPIC: " + str(d['topic'])
# print type(d['topic'])
if d['topic'] == "" or d['topic'] == None or d['topic'] ==[]:
d['topic'] = "Sin Tema"
if type(d['topic'])=="list":
for t in d['topic']:
topic, created = Topic.objects.all().get_or_create(name=t)
news.topic.add(topic)
else:
topic, created = Topic.objects.all().get_or_create(name=d['topic'])
news.topic.add(topic)
# print news.date
# print d['date']
# #news.save()
# except (RuntimeError, TypeError, NameError, ValueError) as e:
except ValueError as e:
print e
# print e.NameError
print "Error: " + os.getcwd()+"/"+f
print type(d['topic'])
# print "TOPIC: " + str(d['topic'])
data_file.close()
os.chdir("..")
os.chdir("..")
#from django.conf.urls import url #from django.conf.urls import url
from django.urls import re_path as url from django.urls import path as url
from . import views from . import views
from django.views.i18n import JavaScriptCatalog from django.views.i18n import JavaScriptCatalog
......
# -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals
from django.shortcuts import render from django.shortcuts import render
...@@ -260,12 +259,12 @@ def publisherList(request, type="all"): ...@@ -260,12 +259,12 @@ def publisherList(request, type="all"):
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def newsList(request, publisherShortName="all"): def newsList(request, publisherShortName="all"):
form = SearchForm(request.GET) form = SearchForm(request.GET)
subscriptions = [ (r.id,r.name) for r in request.user.profile.subscriptions.all()] subscriptions = [ (r.id,r.name) for r in request.user.profile.subscriptions.all()]
form.fields['publishers'].choices= subscriptions form.fields['publishers'].choices= subscriptions
return render(request,'new/newsList.html',{"form":form}) return render(request,'new/newsList.html',{"form":form})
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def newsDetails(request, newsId): def newsDetails(request, newsId):
form = SearchForm(request.GET) form = SearchForm(request.GET)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment