Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added logging to views and command line functions. Fixes #26. Also ad…
…ded fixture data for command line requester. Added admin flag to Requester model Fixes #24. Also authenticate requests by remote IP address. Fixes #23.
- Loading branch information
Joshua Gomez
committed
May 2, 2012
1 parent
6ef9b1e
commit 825abb4
Showing
8 changed files
with
96 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,4 +1,5 @@ | |||
*.pyc | *.pyc | ||
*.log | |||
ENV | ENV | ||
lids/lids/local_settings.py | lids/lids/local_settings.py | ||
*~ | *~ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,16 +1,19 @@ | |||
from django.core.management.base import BaseCommand, CommandError | from django.core.management.base import BaseCommand, CommandError | ||
from lidapp.models import ID | from lidapp.models import ID | ||
import logging | |||
|
|
||
|
|
||
class Command(BaseCommand): | class Command(BaseCommand): | ||
args = '<identifier>' | args = '<identifier>' | ||
help = 'Get metadata for an identifier' | help = 'Get metadata for an identifier' | ||
|
|
||
def handle(self, *args, **options): | def handle(self, *args, **options): | ||
logger = logging.getLogger('lidapp.actions') | |||
identifier = args[0] | identifier = args[0] | ||
try: | try: | ||
id = ID.objects.get(identifier=identifier) | id = ID.objects.get(identifier=identifier) | ||
except ID.DoesNotExist: | except ID.DoesNotExist: | ||
raise CommandError('Identifier "%s" does not exist' % identifier) | raise CommandError('Identifier "%s" does not exist' % identifier) | ||
|
|
||
logger.info('Action: lookup IP: 127.0.0.1 ID: %s Result:SUCCESS.' % identifier) | |||
self.stdout.write(id.dump_string() + '\n') | self.stdout.write(id.dump_string() + '\n') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
INSERT INTO lidapp_requester (name, ip, date_created, admin) VALUES ('Command Line', '127.0.0.1', NOW(), TRUE); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,32 +1,76 @@ | |||
from django.http import HttpResponse | from django.http import HttpResponse, Http404, HttpResponseForbidden | ||
from django.shortcuts import get_object_or_404, redirect | from django.shortcuts import redirect | ||
from django.utils.timezone import now | |||
from lidapp.models import ID, Minter, Requester | from lidapp.models import ID, Minter, Requester | ||
import logging | |||
import json | import json | ||
|
|
||
logger = logging.getLogger('lidapp.actions') | |||
|
|||
def _ids_to_json(ids): | def _ids_to_json(ids): | ||
return json.dumps([id.dump_dict() for id in ids], indent=2) | return json.dumps([id.dump_dict() for id in ids], indent=2) | ||
|
|
||
def mint(request, minter_name, quantity=1): | def mint(request, minter_name, quantity=1): | ||
minter = get_object_or_404(Minter, name=minter_name) | ip = request.META['REMOTE_ADDR'] | ||
requester = get_object_or_404(Requester, name=request.GET['requester']) | quantity = int(quantity) | ||
try: | |||
requester = Requester.objects.get(ip=ip) | |||
minter = Minter.objects.get(name=minter_name) | |||
except Requester.DoesNotExist: | |||
logger.info('Action: mint %s IP: %s Result:FAILED. IP not recognized' %(quantity, ip)) | |||
raise Http404('You are not permitted to mint IDs from IP address %s' % ip) | |||
except Minter.DoesNotExist: | |||
logger.info('Action: mint %s IP: %s Result:FAILED. Minter %s does not exist' % (quantity, ip, minter_name)) | |||
raise Http404('Minter %s does not exist' % minter_name) | |||
ids = minter.mint(requester=requester, quantity=quantity) | ids = minter.mint(requester=requester, quantity=quantity) | ||
for x in range(quantity): | |||
logger.info('Action: mint %s of %s IP: %s Result:SUCCESS. Minted %s' % (x+1, quantity, ip, ids[x].identifier)) | |||
return HttpResponse(_ids_to_json(ids), content_type='application/json') | return HttpResponse(_ids_to_json(ids), content_type='application/json') | ||
|
|
||
def bind(request, identifier): | def bind(request, identifier): | ||
id = get_object_or_404(ID, identifier=identifier) | ip = request.META['REMOTE_ADDR'] | ||
try: | |||
requester = Requester.objects.get(ip=ip) | |||
id = ID.objects.get(identifier=identifier) | |||
except Requester.DoesNotExist: | |||
logger.info('Action: bind IP: %s ID: %s Result:FAILED. IP not recognized' % (ip, identifier)) | |||
raise Http404('You are not permitted to bind IDs from IP address %s' % ip) | |||
except ID.DoesNotExist: | |||
logger.info('Action: bind IP: %s ID: %s Result:FAILED. Identifier does not exist' % (ip, identifier)) | |||
raise Http404('ID %s does not exist %s' % identifier) | |||
if requester.admin == False and not id.requester.ip == requester.ip: | |||
logger.info('Action: bind IP: %s ID: %s Result:FAILED. Requester not authorized to edit ID' % (ip, identifier)) | |||
return HttpResponseForbidden('You are not authorized to bind data to ID %s from IP address %s' % (identifier, ip)) | |||
kwargs = {} | kwargs = {} | ||
for field in id.bindable_fields: | for field in id.bindable_fields: | ||
if field in request.GET: | if field in request.GET: | ||
kwargs[field] = request.GET[field] | kwargs[field] = request.GET[field] | ||
id.bind(**kwargs) | id.bind(**kwargs) | ||
logger.info('Action: bind IP: %s ID: %s Result:SUCCESS. Data: %s' % (ip, identifier, kwargs)) | |||
return HttpResponse(_ids_to_json([id]), content_type='application/json') | return HttpResponse(_ids_to_json([id]), content_type='application/json') | ||
|
|
||
def lookup(request, identifier): | def lookup(request, identifier): | ||
id = get_object_or_404(ID,identifier=identifier) | ip = request.META['REMOTE_ADDR'] | ||
try: | |||
id = ID.objects.get(identifier=identifier) | |||
except ID.DoesNotExist: | |||
logger.info('Action: lookup IP: %s ID: %s Result:FAILED. Identifier does not exist' % (ip, identifier)) | |||
raise Http404('ID %s does not exist %s' % identifier) | |||
logger.info('Action: lookup IP: %s ID: %s Result:SUCCESS.' % (ip, identifier)) | |||
return HttpResponse(_ids_to_json([id]), content_type='application/json') | return HttpResponse(_ids_to_json([id]), content_type='application/json') | ||
|
|
||
def resolve(request, identifier): | def resolve(request, identifier): | ||
id = get_object_or_404(ID,identifier=identifier) | ip = request.META['REMOTE_ADDR'] | ||
try: | |||
id = ID.objects.get(identifier=identifier) | |||
except ID.DoesNotExist: | |||
logger.info('Action: resolve IP: %s ID: %s Result:FAILED. Identifier does not exist' % (ip, identifier)) | |||
raise Http404('ID %s does not exist' % identifier) | |||
if id.object_url: | if id.object_url: | ||
url = id.object_url if id.object_url.startswith('http://') else 'http://'+id.object_url | url = id.object_url if id.object_url.startswith('http') else 'http://'+id.object_url | ||
logger.info('Action: resolve IP: %s ID: %s Result:SUCCESS.' % (ip, identifier)) | |||
return redirect(url) | return redirect(url) | ||
else: | |||
logger.info('Action: resolve IP: %s ID: %s Result:FAILED. Identifier has not been bound to a url' % (ip, identifier)) | |||
raise Http404('ID %s has not been bound to a url' % identifier) | |||
#TODO: provide a more graceful resolution error page |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters