Commits

Łukasz Langa  committed 07703d1

rental library

  • Participants
  • Parent commits 368b1e4

Comments (0)

Files changed (7)

         'django-tastypie>=0.9.11',
         'gunicorn',
         'setproctitle',
+        'slumber',
+        'bpython',
         'configparser',
         ],
     classifiers = [

File src/soa_library/rental/__init__.py

Empty file added.

File src/soa_library/rental/models.py

+from datetime import datetime
+import json
+
+from celery.task import task
+from django.conf import settings
+from django.db import models as db
+import slumber
+
+BOOK_REPOSITORY = settings.BOOK_REPOSITORY
+
+class Customer(db.Model):
+    first_name = db.CharField(max_length=100)
+    last_name = db.CharField(max_length=100)
+
+    class Meta:
+        ordering = ['last_name', 'first_name']
+
+    def __unicode__(self):
+        return "{} {}".format(self.first_name, self.last_name)
+
+
+class Rental(db.Model):
+    book = db.URLField(unique=True)
+    book_cache = db.TextField(null=True, blank=True)
+    customer = db.ForeignKey(Customer)
+    rental_dt = db.DateTimeField(default=datetime.now)
+    return_dt = db.DateTimeField(default=None, blank=True, null=True)
+
+    class Meta:
+        ordering = ['rental_dt', 'customer']
+
+    def __unicode__(self):
+        return '{}: {}'.format(self.customer, self.book)
+
+    @staticmethod
+    @task()
+    def rent(customer_id, book_id):
+        url = BOOK_REPOSITORY['url']
+        creds = dict(BOOK_REPOSITORY)
+        del creds['url']
+        api = slumber.API(url)
+        book = api.book(book_id).get(**creds)
+        customer = Customer.objects.using('rental').get(pk=customer_id)
+        new_rental = Rental(book=book['resource_uri'], book_cache=json.dumps(book),
+            customer=customer)
+        new_rental.save(using='rental')
+        return new_rental.id

File src/soa_library/rental/tests.py

+"""
+This file demonstrates writing tests using the unittest module. These will pass
+when you run "manage.py test".
+
+Replace this with more appropriate tests for your application.
+"""
+
+from django.test import TestCase
+
+
+class SimpleTest(TestCase):
+    def test_basic_addition(self):
+        """
+        Tests that 1 + 1 always equals 2.
+        """
+        self.assertEqual(1 + 1, 2)

File src/soa_library/rental/views.py

+# Create your views here.

File src/soa_library/settings.py

     'gunicorn',
     'tastypie',
     'soa_library.books',
+    'soa_library.rental',
 )
 TEMPLATE_CONTEXT_PROCESSORS = (
     'django.contrib.auth.context_processors.auth',
         'HOST': '',
         'PORT': '',
         'OPTIONS': {'timeout': 30},
-    }
+    },
+    'rental': {
+        'ENGINE': 'django.db.backends.sqlite3',
+        'NAME': CURRENT_DIR + 'rental.db',
+        'USER': '',
+        'PASSWORD': '',
+        'HOST': '',
+        'PORT': '',
+        'OPTIONS': {'timeout': 30},
+    },
 }
 LOGGING['handlers']['file']['filename'] = CURRENT_DIR + 'runtime.log'
 BROKER_URL = "redis://localhost:6379/2"
 DEFAULT_SAVE_PRIORITY = 0
 API_LIMIT_PER_PAGE = 50
+BOOK_REPOSITORY = None 
 
 #
 # programmatic stuff that needs to be in

File src/soa_library/urls.py

     # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
 
     url(r'^admin/', include(admin.site.urls)),
-    url(r'^api/v0.1/book/schema', 'soa_library.books.views.dummy_view'),
     url(r'^api/', include(v01_api.urls)),
 )