adding command to store blog posts locally
This commit is contained in:
49
rowers/management/commands/getblogposts.py
Normal file
49
rowers/management/commands/getblogposts.py
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
#!/srv/venv/bin/python
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
|
||||||
|
import requests
|
||||||
|
import datetime
|
||||||
|
import arrow
|
||||||
|
|
||||||
|
PY3K = sys.version_info >= (3,0)
|
||||||
|
|
||||||
|
from django.core.management.base import BaseCommand
|
||||||
|
from rowers.models import BlogPost
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
def handle(self, *args, **options):
|
||||||
|
blogs_json = []
|
||||||
|
try:
|
||||||
|
response = requests.get(
|
||||||
|
'https://analytics.rowsandall.com/wp-json/wp/v2/posts?per_page=3')
|
||||||
|
if response.status_code == 200:
|
||||||
|
blogs_json = response.json()
|
||||||
|
else:
|
||||||
|
blogs_json = []
|
||||||
|
except ConnectionError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if blogs_json:
|
||||||
|
result = BlogPost.objects.all().delete()
|
||||||
|
|
||||||
|
for postdata in blogs_json[0:3]:
|
||||||
|
title = postdata['title']['rendered']
|
||||||
|
link = postdata['link']
|
||||||
|
datetime = postdata['date']
|
||||||
|
datetime = arrow.get(datetime).datetime
|
||||||
|
date = datetime.date()
|
||||||
|
|
||||||
|
|
||||||
|
blogpost = BlogPost(
|
||||||
|
link=link,
|
||||||
|
date=date,
|
||||||
|
title=title
|
||||||
|
)
|
||||||
|
|
||||||
|
blogpost.save()
|
||||||
|
|
||||||
|
self.stdout.write(self.style.SUCCESS(
|
||||||
|
'Successfully processed blog posts'))
|
||||||
|
|
||||||
@@ -3707,3 +3707,7 @@ class PlannedSessionCommentForm(ModelForm):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class BlogPost(models.Model):
|
||||||
|
title = models.TextField(max_length=300)
|
||||||
|
link = models.TextField(max_length=300)
|
||||||
|
date = models.DateField()
|
||||||
|
|||||||
@@ -103,7 +103,7 @@ from rowers.models import (
|
|||||||
Team,TeamForm,TeamInviteForm,TeamInvite,TeamRequest,
|
Team,TeamForm,TeamInviteForm,TeamInvite,TeamRequest,
|
||||||
WorkoutComment,WorkoutCommentForm,RowerExportForm,
|
WorkoutComment,WorkoutCommentForm,RowerExportForm,
|
||||||
CalcAgePerformance,
|
CalcAgePerformance,
|
||||||
PowerTimeFitnessMetric,
|
PowerTimeFitnessMetric,BlogPost,
|
||||||
PlannedSessionForm,
|
PlannedSessionForm,
|
||||||
PlannedSessionFormSmall,GeoCourseEditForm,VirtualRace,
|
PlannedSessionFormSmall,GeoCourseEditForm,VirtualRace,
|
||||||
VirtualRaceForm,VirtualRaceResultForm,RowerImportExportForm,
|
VirtualRaceForm,VirtualRaceResultForm,RowerImportExportForm,
|
||||||
@@ -801,8 +801,23 @@ def get_thumbnails(request,id):
|
|||||||
|
|
||||||
return JSONResponse(charts)
|
return JSONResponse(charts)
|
||||||
|
|
||||||
|
|
||||||
def get_blog_posts(request):
|
def get_blog_posts(request):
|
||||||
|
blogposts = BlogPost.objects.all().order_by("-date")
|
||||||
|
|
||||||
|
jsondata = []
|
||||||
|
|
||||||
|
if blogposts:
|
||||||
|
for blogpost in blogposts[0:3]:
|
||||||
|
thedict = {
|
||||||
|
'title':blogpost.title,
|
||||||
|
'link':blogpost.link,
|
||||||
|
}
|
||||||
|
|
||||||
|
jsondata.append(thedict)
|
||||||
|
|
||||||
|
return JSONResponse(jsondata)
|
||||||
|
|
||||||
|
def get_blog_posts_old(request):
|
||||||
try:
|
try:
|
||||||
response = requests.get(
|
response = requests.get(
|
||||||
'https://analytics.rowsandall.com/wp-json/wp/v2/posts?per_page=3')
|
'https://analytics.rowsandall.com/wp-json/wp/v2/posts?per_page=3')
|
||||||
|
|||||||
Reference in New Issue
Block a user