diff --git a/core/models.py b/core/models.py index 065e6941..43531687 100755 --- a/core/models.py +++ b/core/models.py @@ -285,11 +285,13 @@ class Campaign(models.Model): return may_launch - def update_status(self, ignore_deadline_for_success=False, send_notice=False): + def update_status(self, ignore_deadline_for_success=False, send_notice=False, process_transactions=False): """Updates the campaign's status. returns true if updated. Computes UNSUCCESSFUL only after the deadline has passed Computes SUCCESSFUL only after the deadline has passed if ignore_deadline_for_success is TRUE -- otherwise looks just at amount of pledges accumulated by default, send_notice is False so that we have to explicitly send specify delivery of successful_campaign notice + + if process_transactions is True, also execute or cancel associated transactions """ if not self.status=='ACTIVE': @@ -301,6 +303,10 @@ class Campaign(models.Model): action.save() if send_notice: successful_campaign.send(sender=None,campaign=self) + if process_transactions: + p = PaymentManager() + results = p.execute_campaign(self) + # should be more sophisticated in whether to return True -- look at all the transactions return True elif self.deadline < now() and self.current_total < self.target: self.status = 'UNSUCCESSFUL' @@ -309,7 +315,11 @@ class Campaign(models.Model): action.save() if send_notice: regluit.core.signals.unsuccessful_campaign.send(sender=None,campaign=self) - return True + if process_transactions: + p = PaymentManager() + results = p.cancel_campaign(self) + # should be more sophisticated in whether to return True -- look at all the transactions + return True else: return False @@ -481,15 +491,11 @@ class Campaign(models.Model): def rightsholder(self): """returns the name of the rights holder for an active or initialized campaign""" try: - # BUGBUG: why should the RH be dependent on the status of a campaign? - # for the moment, I'll extend this logic to include 'SUCCESSFUL' campaigns too - if self.status in ('ACTIVE', 'INITIALIZED', 'SUCCESSFUL'): - q = Q(status='ACTIVE') | Q(status='INITIALIZED') - rh = self.work.claim.filter(q)[0].rights_holder.rights_holder_name - return rh + q = Q(status='ACTIVE') | Q(status='INITIALIZED') + rh = self.work.claim.filter(q)[0].rights_holder.rights_holder_name + return rh except: - pass - return '' + return '' @property def license_url(self): diff --git a/core/tasks.py b/core/tasks.py index 07e49388..fcdb8d25 100644 --- a/core/tasks.py +++ b/core/tasks.py @@ -81,7 +81,7 @@ def send_mail_task(subject, message, from_email, recipient_list, @task def update_active_campaign_status(): """update the status of all active campaigns -- presumed to be run at midnight Eastern time""" - return [c.update_status(send_notice=True) for c in Campaign.objects.filter(status='Active') ] + return [c.update_status(send_notice=True, ignore_deadline_for_success=True, process_transactions=True) for c in Campaign.objects.filter(status='Active') ] @task def emit_notifications(): diff --git a/core/tests.py b/core/tests.py index ef71e897..16058183 100755 --- a/core/tests.py +++ b/core/tests.py @@ -5,6 +5,7 @@ from regluit.utils.localdatetime import now, date_today from django.test import TestCase from django.test.client import Client from django.utils import unittest +from django.test.utils import override_settings from django.db import IntegrityError from django.contrib.auth.models import User from django.conf import settings @@ -24,7 +25,7 @@ from regluit.core import tasks from celery.task.sets import TaskSet from celery.task import chord -from time import sleep +from time import sleep, mktime from math import factorial from urlparse import parse_qs, urlparse @@ -649,3 +650,46 @@ class DownloadPageTest(TestCase): response = anon_client.get("/work/%s/download/" % w.id) self.assertContains(response, "http://example.com", count=4) self.assertContains(response, "http://example2.com", count=3) + + +class LocaldatetimeTest(TestCase): + @override_settings(LOCALDATETIME_NOW=None) + def test_LOCALDATETIME_NOW_none(self): + + try: + localdatetime.now + except NameError: + from regluit.utils import localdatetime + else: + reload(localdatetime) + + self.assertAlmostEqual(mktime(datetime.now().timetuple()), mktime(localdatetime.now().timetuple()), 1.0) + + @override_settings(LOCALDATETIME_NOW=lambda : datetime.now() + timedelta(365)) + def test_LOCALDATETIME_NOW_year_ahead(self): + + try: + localdatetime.now + except NameError: + from regluit.utils import localdatetime + else: + reload(localdatetime) + + self.assertAlmostEqual(mktime((datetime.now() + timedelta(365)).timetuple()), mktime(localdatetime.now().timetuple()), 1.0) + + def test_no_time_override(self): + + from regluit.utils import localdatetime + self.assertAlmostEqual(mktime(datetime.now().timetuple()), mktime(localdatetime.now().timetuple()), 1.0) + + def tearDown(self): + # restore localdatetime.now() to what's in the settings file + try: + localdatetime.now + except NameError: + from regluit.utils import localdatetime + else: + reload(localdatetime) + + + diff --git a/frontend/templates/base.html b/frontend/templates/base.html index b1473b34..e50fb398 100644 --- a/frontend/templates/base.html +++ b/frontend/templates/base.html @@ -140,7 +140,7 @@ {% block news %}
- We unglued one book. Amazon shut us down. Now we're back. Help us unglue five more books!
+ We unglued one book. Amazon shut us down. Now we're back. Help us unglue four more books!
{% endblock %} diff --git a/utils/localdatetime.py b/utils/localdatetime.py index a1f5667d..42529adc 100644 --- a/utils/localdatetime.py +++ b/utils/localdatetime.py @@ -46,22 +46,20 @@ from django.conf import settings # for Django 1.4 should switch to django.utils.timezone.now() # see https://code.djangoproject.com/browser/django/trunk/django/utils/timezone.py?rev=17642#L232 -# if there is a LOCALDATETIME_NOW in the Django settings, use that to be _now - -if hasattr(settings, 'LOCALDATETIME_NOW') and settings.LOCALDATETIME_NOW is not None: - _now = settings.LOCALDATETIME_NOW -else: - try: - _now = django.utils.timezone.now - except AttributeError, e: - _now = datetime.datetime.now +def now(): + if hasattr(settings, 'LOCALDATETIME_NOW') and settings.LOCALDATETIME_NOW is not None: + return settings.LOCALDATETIME_NOW() + else: + try: + return django.utils.timezone.now() + except AttributeError, e: + return datetime.datetime.now() -now = lambda: _now() - # provide a replacement for datetime.date.today() # this will be timezone naive -- is that what we really want? -date_today = lambda: _now().date() +def date_today(): + return now().date() # borrow a lot of the routines/code that will be in Django 1.4+ django.utils.timezone # https://code.djangoproject.com/browser/django/trunk/django/utils/timezone.py