...
 
Commits (7)
# python imports
from crontab import CronTab
import os
# django imports
from django.core.management.base import BaseCommand, CommandError
from django.conf import settings
class Command(BaseCommand):
help = 'Sets up a CronTab to update the database \
every day at 2am'
def handle(self, *args, **options):
cron = CronTab(user=True)
# first, we grab the route to the python in our venv so bash
# runs the command with the dependencies
pyvenvpath = os.path.join(settings.BASE_DIR, '..', 'venv',
'bin', 'python')
# next, we grab the route to the manage.py file
managepypath = os.path.join(settings.BASE_DIR, 'manage.py')
# next, we grab the route to the output file
cronlogpath = os.path.join(settings.BASE_DIR, 'cronlog.txt')
# then, set up the command we want to run using the venv python
cmd = pyvenvpath + ' ' + managepypath + ' update_data 0 1 ' \
+ '>> ' + cronlogpath + ' ' \
+ '2>&1' # outputs errors as well
# now put the whole command into a job
cjob = cron.new(command=cmd)
# make the job occur every day at 2 in the morning
cjob.hour.on(2)
cjob.day.every(1)
cron.write()
......@@ -2,9 +2,22 @@
from django.core.management.base import BaseCommand, CommandError
from api import parscript
# python imports
from datetime import datetime
class Command(BaseCommand):
help = 'Updates the database from all our sources'
def add_arguments(self, parser):
# accept an optional argument for allowing print statements.
# default to suppressing print statements
parser.add_argument('fdback', type=int, nargs='?', default=1)
parser.add_argument('successmsg', type=int, nargs='?', default=1)
def handle(self, *args, **options):
parscript.load_data()
fdback = options['fdback']
successmsg = options['successmsg']
parscript.load_data(fdback)
# success message will print if requested
if successmsg:
print("Successfully updated " + str(datetime.now()))
......@@ -11,15 +11,18 @@ from bs4 import BeautifulSoup
# Parses the XML from Mason and mines 2 BTC. thanks zac wood
def load_data():
print('hello there')
def load_data(fdback = 1):
if fdback:
print('hello there')
# creates a list of all the entry tags from the xml
soup = BeautifulSoup(
requests.get(
# gets xml from 25live and cleans it up
xmltext = requests.get(
"https://25livepub.collegenet.com/calendars/events_all.xml?html=0"
).text, "lxml"
)
).text
xmltext = cleanup(xmltext)
# creates a list of all the entry tags from the xml
soup = BeautifulSoup(xmltext, "lxml")
entries = soup.findAll('entry')
......@@ -34,7 +37,7 @@ def load_data():
data[4], # start_time
data[5], # end_time
)
print(my_event.insert_event())
print(my_event.insert_event()) if fdback else my_event.insert_event()
def entry_parser(entry):
......@@ -150,4 +153,15 @@ def fix_string(the_time):
return the_time
def cleanup(bigstring):
replacements = [
("\r", ""),
]
for replacement in replacements:
bigstring = bigstring.replace(replacement[0], replacement[1])
return bigstring
# everything in the house is fuzzy, stupid dogs were acting like pollinators, if that's how you even spell it
......@@ -7,4 +7,5 @@ urlpatterns = [
path('rawdata/', views.raw_data, name='rawdata'),
path('event/<int:eventid>', views.event_by_id,
name='event by id'),
path('sort/by-date', views.sort_by_date, name='sort by date'),
]
\ No newline at end of file
......@@ -3,21 +3,51 @@ from django.http import HttpResponse
from .models import Event
import json
from datetime import date
# Create your views here.
def api_welcome(request):
return render(request, 'api/apiwelcome.html')
return render(request, 'api/apiwelcome.html')
def raw_data(request):
eventlist = [
event.make_dict()
for event in Event.objects.all()
]
return HttpResponse(json.dumps(eventlist, ensure_ascii=False),
content_type="application/json")
eventlist = [
event.make_dict()
for event in Event.objects.all()
]
return HttpResponse(json.dumps(eventlist, ensure_ascii=False),
content_type='application/json')
def event_by_id(request, eventid):
event = get_object_or_404(Event, uniqueid=eventid)
return HttpResponse(json.dumps([event.make_dict()], ensure_ascii=False),
content_type="application/json")
\ No newline at end of file
event = get_object_or_404(Event, uniqueid=eventid)
return HttpResponse(json.dumps([event.make_dict()], ensure_ascii=False),
content_type='application/json')
def sort_by_date(request):
sorted_events = Event.objects.all().order_by('start_time')
sorted_json = [] # the full json we'll return
curr_day_dict = {
'date': '',
'events': []
} # a dictionary with just the current day
for event in Event.objects.all().order_by('start_time'):
start_time = event.start_time
curr_date = date(start_time.year, start_time.month, start_time.day)
if curr_day_dict['date'] == str(curr_date):
curr_day_dict['events'].append(event.make_dict())
else:
if curr_day_dict['events'] != []: # is not empty
sorted_json.append(curr_day_dict)
# make the next day and throw this one in
curr_day_dict = {
'date': str(curr_date),
'events': [event.make_dict()]
}
# get the last day
sorted_json.append(curr_day_dict)
return HttpResponse(json.dumps(sorted_json, ensure_ascii=False),
content_type='application/json')
......@@ -122,3 +122,7 @@ USE_TZ = True
# https://docs.djangoproject.com/en/2.1/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'mason_today_web', 'static'),
)
\ No newline at end of file
......@@ -19,7 +19,7 @@ from django.urls import path, include
from . import views
urlpatterns = [
path('', views.homepage, name='homepage'),
path('', views.homepage, name='homepage'),
path('admin/', admin.site.urls),
path('api/', include('api.urls')),
]
......@@ -3,4 +3,4 @@ from django.shortcuts import render
# Create some views
def homepage(request):
return render(request, 'homepage.html')
\ No newline at end of file
return render(request, 'homepage.html')
\ No newline at end of file