mirror of
https://github.com/nikdoof/test-auth.git
synced 2025-12-20 21:29:26 +00:00
Abstract out update command instead of being embedded in save, added cron
This commit is contained in:
27
reddit/cron.py
Normal file
27
reddit/cron.py
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import time
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from django_cron import cronScheduler, Job
|
||||||
|
from reddit.models import RedditAccount
|
||||||
|
|
||||||
|
class UpdateAPIs(Job):
|
||||||
|
"""
|
||||||
|
Updates all Reddit API elements in the database
|
||||||
|
"""
|
||||||
|
|
||||||
|
# run every 24 hours
|
||||||
|
run_every = 86400
|
||||||
|
|
||||||
|
@property
|
||||||
|
def _logger(self):
|
||||||
|
if not hasattr(self, '__logger'):
|
||||||
|
self.__logger = logging.getLogger(__name__)
|
||||||
|
return self.__logger
|
||||||
|
|
||||||
|
def job(self):
|
||||||
|
for acc in RedditAccount.objects.all():
|
||||||
|
acc.api_update()
|
||||||
|
acc.save()
|
||||||
|
time.sleep(2)
|
||||||
|
|
||||||
|
cronScheduler.register(UpdateAPIs)
|
||||||
@@ -21,7 +21,7 @@ class RedditAccount(models.Model):
|
|||||||
|
|
||||||
last_update = models.DateTimeField("Last Update from API", blank=False)
|
last_update = models.DateTimeField("Last Update from API", blank=False)
|
||||||
|
|
||||||
def save(self):
|
def api_update(self):
|
||||||
try:
|
try:
|
||||||
jsondoc = json.load(urllib.urlopen("http://reddit.com/user/%s/about.json" % self.username))
|
jsondoc = json.load(urllib.urlopen("http://reddit.com/user/%s/about.json" % self.username))
|
||||||
except:
|
except:
|
||||||
@@ -36,8 +36,6 @@ class RedditAccount(models.Model):
|
|||||||
|
|
||||||
self.last_update = datetime.now()
|
self.last_update = datetime.now()
|
||||||
|
|
||||||
return models.Model.save(self)
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
app_label = 'reddit'
|
app_label = 'reddit'
|
||||||
ordering = ['username']
|
ordering = ['username']
|
||||||
|
|||||||
@@ -135,6 +135,7 @@ def reddit_add(request):
|
|||||||
|
|
||||||
acc.user = request.user
|
acc.user = request.user
|
||||||
acc.username = form.cleaned_data['username']
|
acc.username = form.cleaned_data['username']
|
||||||
|
acc.api_update()
|
||||||
|
|
||||||
acc.save()
|
acc.save()
|
||||||
return HttpResponseRedirect(reverse('sso.views.profile')) # Redirect after POST
|
return HttpResponseRedirect(reverse('sso.views.profile')) # Redirect after POST
|
||||||
|
|||||||
Reference in New Issue
Block a user