Skip to content

Commit

Permalink
WIP changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rezzo committed Feb 6, 2013
1 parent bda18ed commit 85b5605
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 1 deletion.
30 changes: 30 additions & 0 deletions elections/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,33 @@ def create_default_questions(sender, instance, created, **kwargs):
for default_answer in default_question['answers']:
Answer.objects.create(question=question, caption=default_answer)




#class ApiKey(models.Model):
# user = models.OneToOneField(User, related_name='api_key')
# key = models.CharField(max_length=256, blank=True, default='')
# created = models.DateTimeField(default=datetime.datetime.now)
#
# def __unicode__(self):
# return u"%s for %s" % (self.key, self.user)
#
# def save(self, *args, **kwargs):
# if not self.key:
# self.key = self.generate_key()
#
# return super(ApiKey, self).save(*args, **kwargs)
#
# def generate_key(self):
# # Get a random UUID.
# new_uuid = uuid.uuid4()
# # Hmac that beast.
# return hmac.new(str(new_uuid), digestmod=sha1).hexdigest()
#
#
#def create_api_key(sender, **kwargs):
# """
# A signal for hooking up automatic ``ApiKey`` creation.
# """
# if kwargs.get('created') is True:
# ApiKey.objects.create(user=kwargs.get('instance'))
1 change: 1 addition & 0 deletions elections/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from tests import *

from answer import *
from api import *
from background import *
from background_category import *
from candidate import *
Expand Down
49 changes: 49 additions & 0 deletions elections/tests/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from django.test import TestCase
from django.contrib.auth.models import User
from django.db import models
from tastypie.models import ApiKey
from elections.models import Election, Candidate, Category, PersonalData, \
BackgroundCategory, Background, PersonalDataCandidate
#from tastypie.models import create_api_key
from tastypie.test import ResourceTestCase

class CandidateResourceCase(ResourceTestCase):
def setUp(self):
self.user = User.objects.create_user(username='doe',
password='joe',
email='[email protected]')
self.user_api_key = ApiKey.objects.create(user=self.user)
self.not_user = User.objects.create_user(username='joe',
password='joe',
email='[email protected]')
self.not_user_api_key = ApiKey.objects.create(user=self.not_user)
self.election, created = Election.objects.get_or_create(name='BarBaz',
owner=self.user,
slug='barbaz')
self.election2, created = Election.objects.get_or_create(name='BarBaz2',
owner=self.user,
slug='barbaz2')
self.candidate = Candidate.objects.create(name='Bar Baz',
election=self.election)
self.candidate2, created = Candidate.objects.get_or_create(
name='Bar Baz',
election=self.election)
def get_credentials(self):
return self.create_apikey(username=self.user.username, api_key=self.user.api_key)

def test_get_candidates_per_election(self):
url = reverse('api_candidates_list',kwargs={ 'pk':self.election.id })
resp = self.api_client.get(url, format='json', authentication=self.get_credentials())
self.assertValidJSONResponse(resp)

self.assertEqual(len(self.deserialize(resp)['objects']), 2)

#print self.user.api_key
#print self.election.id


## . tengo api key ------ SI
## . tengo id de eleccion --- SI
## . GET http://127.0.0.1:8000/api/v1/candidates/?election=3&api_key=204db7bcfafb2deb7506b89eb3b9b715b09905c9
## . devuelve lista de candidatos
## . deberia devolver: id, name, slug, photo, personal_data??
5 changes: 4 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ pep8==0.6.1
python-openid==2.2.5
sorl-thumbnail==11.09.1
wsgiref==0.1.2
pil==1.1.7
#pil==1.1.7
#django-tastypie==0.9.11
-e git+https://github.com/toastdriven/django-tastypie.git#egg=django-tastypie

2 changes: 2 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@
'django_extensions',
# Django-registration
'registration',
# Tastypie, RESTful APIs for Django:
'tastypie',
)

# A sample logging configuration. The only tangible logging
Expand Down

0 comments on commit 85b5605

Please sign in to comment.