Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added active flag to Minter model and added exception/catching for mi…

…nt methods on inactive minters, Fixes #27
  • Loading branch information...
commit 189c674dfefa6d82231760430fa92903ff891a1d 1 parent 825abb4
Joshua Gomez authored
View
1  lids/lidapp/management/commands/import.py
@@ -64,6 +64,7 @@ def handle(self, *args, **options):
template='unkown',
minter_type=id_type,
date_created=now(),
+ active=False,
description='A legacy place holder created on import. Not for current use')
# get requester object - create legacy requester if necessary
requester_name = row['requester_name'] if row['requester_name'] else 'legacy'
View
24 lids/lidapp/management/commands/mint.py
@@ -23,26 +23,26 @@ class Command(BaseCommand):
)
def handle(self, *args, **options):
- logger = logging.getLogger('lidapp.actions')
try:
+ logger = logging.getLogger('lidapp.actions')
minter_name = args[0]
minter = Minter.objects.get(name=minter_name)
requester = Requester.objects.get(ip='127.0.0.1')
+ ids = minter.mint(requester=requester, quantity=options['quantity'])
+ for x in range(options['quantity']):
+ logger.info('Action: mint %s of %s IP: 127.0.0.1 Result:SUCCESS. Minted %s' % (x+1, options['quantity'], ids[x].identifier))
+ if not options['verbose']:
+ output = '\n'.join([id.identifier for id in ids])
+ self.stdout.write(output+'\n')
+ else:
+ for id in ids:
+ self.stdout.write(id.dump_string()+'\n')
except IndexError:
raise CommandError('Please specify a minter')
except Minter.DoesNotExist:
raise CommandError('Minter "%s" does not exist' % minter_name)
except Requester.DoesNotExist:
requester = Requester.objects.create(name='Command Line', date_created=now(),admin=True, ip='127.0.0.1')
+ except Minter.InactiveMinter:
+ raise CommandError('Minter "%s" is inactive' % minter_name)
- ids = minter.mint(requester=requester, quantity=options['quantity'])
-
- for x in range(options['quantity']):
- logger.info('Action: mint %s of %s IP: 127.0.0.1 Result:SUCCESS. Minted %s' % (x+1, options['quantity'], ids[x].identifier))
-
- if not options['verbose']:
- output = '\n'.join([id.identifier for id in ids])
- self.stdout.write(output+'\n')
- else:
- for id in ids:
- self.stdout.write(id.dump_string()+'\n')
View
13 lids/lidapp/models.py
@@ -25,7 +25,8 @@ class Minter(models.Model):
template = models.CharField(max_length=25, blank=True)
minter_type = models.CharField(max_length=1, choices=settings.ID_TYPES)
date_created = models.DateTimeField()
- description = models.TextField()
+ active = models.BooleanField(default=True)
+ description = models.TextField(blank=True)
def __unicode__(self):
return self.name
@@ -45,6 +46,8 @@ def _id_exists(self, identifier):
return True if len(res) > 0 else False
def mint(self, requester, quantity=1):
+ if self.active == False:
+ raise self.InactiveMinter(self.name)
ids = []
for i in range(int(quantity)):
identifier = self._generate_id()
@@ -55,6 +58,14 @@ def mint(self, requester, quantity=1):
ids.append(id)
return ids
+ class InactiveMinter(Exception):
+
+ def __init__(self, name):
+ self.name = name
+
+ def __repr__(self):
+ return 'Minter "%s" is no longer allowed to mint new idetifiers.' % self.name
+
class ID(models.Model):
View
2  lids/lidapp/sql/requester.sql
@@ -1 +1 @@
-INSERT INTO lidapp_requester (name, ip, date_created, admin) VALUES ('Command Line', '127.0.0.1', NOW(), TRUE);
+INSERT INTO lidapp_requester (name, ip, date_created, admin, organization, description) VALUES ('Command Line', '127.0.0.1', NOW(), TRUE, '', 'system-generated command line user');
View
36 lids/lidapp/views.py
@@ -16,16 +16,19 @@ def mint(request, minter_name, quantity=1):
try:
requester = Requester.objects.get(ip=ip)
minter = Minter.objects.get(name=minter_name)
+ 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')
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)
- 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')
+ except Minter.InactiveMinter:
+ logger.info('Action: mint %s IP: %s Result:FAILED. Minter %s is inactive' % (quantity, ip, minter_name))
+ raise Http404('Minter %s is inactive and cannot mint new identifiers' % minter_name)
def bind(request, identifier):
ip = request.META['REMOTE_ADDR']
@@ -53,24 +56,25 @@ def lookup(request, identifier):
ip = request.META['REMOTE_ADDR']
try:
id = ID.objects.get(identifier=identifier)
+ logger.info('Action: lookup IP: %s ID: %s Result:SUCCESS.' % (ip, identifier))
+ return HttpResponse(_ids_to_json([id]), content_type='application/json')
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')
-
+
def resolve(request, identifier):
- ip = request.META['REMOTE_ADDR']
try:
+ ip = request.META['REMOTE_ADDR']
id = ID.objects.get(identifier=identifier)
+ if 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)
+ 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
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:
- 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)
- 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
+
Please sign in to comment.
Something went wrong with that request. Please try again.