mirror of
https://github.com/nikdoof/test-auth.git
synced 2025-12-14 14:52:15 +00:00
Added more error detection to the Reddit app, also moved is_valid to the model
This commit is contained in:
@@ -10,27 +10,6 @@ class RedditAccountAdmin(admin.ModelAdmin):
|
||||
|
||||
fields = ('user', 'username', 'validated')
|
||||
|
||||
#form = RedditAccountForm
|
||||
|
||||
def is_valid(self, obj):
|
||||
if not obj.date_created:
|
||||
return False
|
||||
|
||||
# Account 3 months old?
|
||||
if (date.today() - obj.date_created.date()).days >= 90:
|
||||
return True
|
||||
|
||||
# Account created after 9/2/10 and before 13/2/10
|
||||
if obj.date_created.date() >= date(2010, 2, 9) and obj.date_created.date() <= date(2010, 2, 13):
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
is_valid.short_description = 'Dreddit Eligible'
|
||||
is_valid.boolean = True
|
||||
|
||||
|
||||
def save_model(self, request, obj, form, change):
|
||||
if not obj.pk:
|
||||
obj.api_update()
|
||||
|
||||
@@ -2,7 +2,7 @@ from django.db import models
|
||||
from django.contrib.auth.models import User
|
||||
import simplejson as json
|
||||
import urllib
|
||||
from datetime import datetime
|
||||
from datetime import datetime, date
|
||||
from reddit.api import Comment
|
||||
|
||||
|
||||
@@ -64,6 +64,21 @@ class RedditAccount(models.Model):
|
||||
|
||||
return posts
|
||||
|
||||
@property
|
||||
def is_valid(self):
|
||||
if not self.date_created:
|
||||
return False
|
||||
|
||||
# Account 3 months old?
|
||||
if (date.today() - self.date_created.date()).days >= 90:
|
||||
return True
|
||||
|
||||
# Account created after 9/2/10 and before 13/2/10
|
||||
if self.date_created.date() >= date(2010, 2, 9) and self.date_created.date() <= date(2010, 2, 13):
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
class Meta:
|
||||
app_label = 'reddit'
|
||||
|
||||
@@ -1,23 +1,47 @@
|
||||
from urllib2 import HTTPError, URLError
|
||||
from celery.task import Task
|
||||
from celery.decorators import task
|
||||
from reddit.models import RedditAccount
|
||||
from reddit.api import Inbox
|
||||
from reddit.api import Inbox, LoginError
|
||||
from django.conf import settings
|
||||
|
||||
@task(ignore_result=True)
|
||||
def send_reddit_message(to, subject, message):
|
||||
ib = Inbox(username=settings.REDDIT_USER, password=settings.REDDIT_PASSWORD)
|
||||
ib.send(to, subject, message)
|
||||
class send_reddit_message(Task):
|
||||
|
||||
default_retry_delay = 5 * 60 # retry in 5 minutes
|
||||
ignore_result = True
|
||||
|
||||
def run(self, to, subject, message, **kwargs):
|
||||
logger = self.get_logger(**kwargs)
|
||||
|
||||
logger.info("Sending Reddit message to %s" % to)
|
||||
ib = Inbox(username=settings.REDDIT_USER, password=settings.REDDIT_PASSWORD)
|
||||
try:
|
||||
ib.send(to, subject, message)
|
||||
except (HTTPError, URLError), exc:
|
||||
logger.error("Error sending message, queueing for retry")
|
||||
self.retry([to, subject, message], kwargs=kwargs, exc=exc)
|
||||
except LoginError, exc:
|
||||
logger.error("Error logging into Reddit")
|
||||
|
||||
|
||||
@task(ignore_result=True)
|
||||
def process_validations():
|
||||
log = process_validations.get_logger()
|
||||
inbox = Inbox(settings.REDDIT_USER, settings.REDDIT_PASSWORD)
|
||||
logger = process_validations.get_logger()
|
||||
try:
|
||||
inbox = Inbox(settings.REDDIT_USER, settings.REDDIT_PASSWORD)
|
||||
except (HTTPError, URLError), exc:
|
||||
logger.error("Error with Reddit, aborting.")
|
||||
return
|
||||
except LoginError, exc:
|
||||
logger.error("Error logging into Reddit")
|
||||
return
|
||||
|
||||
for msg in inbox:
|
||||
if not msg.was_comment:
|
||||
try:
|
||||
acc = RedditAccount.objects.get(username__iexact=msg.author)
|
||||
if not acc.validated and msg.subject == "Validation: %s" % acc.user.username:
|
||||
log.info("Validated %s" % acc.user.username)
|
||||
logger.info("Validated %s" % acc.user.username)
|
||||
acc.validated = True
|
||||
acc.save()
|
||||
except RedditAccount.DoesNotExist:
|
||||
|
||||
Reference in New Issue
Block a user