mirror of
https://github.com/nikdoof/test-auth.git
synced 2025-12-14 06:42:16 +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)
|
||||
|
||||
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'
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user