Abstract out update command instead of being embedded in save, added cron

This commit is contained in:
2010-03-02 23:08:19 +00:00
parent b04f943bc5
commit ed258069fd
3 changed files with 29 additions and 3 deletions

27
reddit/cron.py Normal file
View 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)

View File

@@ -21,7 +21,7 @@ class RedditAccount(models.Model):
last_update = models.DateTimeField("Last Update from API", blank=False)
def save(self):
def api_update(self):
try:
jsondoc = json.load(urllib.urlopen("http://reddit.com/user/%s/about.json" % self.username))
except:
@@ -35,8 +35,6 @@ class RedditAccount(models.Model):
self.date_created = datetime.fromtimestamp(data['created_utc'])
self.last_update = datetime.now()
return models.Model.save(self)
class Meta:
app_label = 'reddit'

View File

@@ -135,6 +135,7 @@ def reddit_add(request):
acc.user = request.user
acc.username = form.cleaned_data['username']
acc.api_update()
acc.save()
return HttpResponseRedirect(reverse('sso.views.profile')) # Redirect after POST