Skip to content

Commit

Permalink
Merge pull request #334 from Brown-University-Library/admin_error_tes…
Browse files Browse the repository at this point in the history
…ting

merges biography-role admin-validation into main.
  • Loading branch information
JustinUhr authored Mar 27, 2023
2 parents a22c440 + 4fc796c commit 40f9d54
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
54 changes: 47 additions & 7 deletions rome_app/models.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
from collections import OrderedDict
import json
import re

from . import app_settings
from bdrxml import mods
from collections import OrderedDict
from django.conf import settings
from django.http import Http404
from django.core.exceptions import ValidationError
from django.db import models, IntegrityError
from django.http import Http404
from django.urls import reverse
import requests
from eulxml.xmlmap import load_xmlobject_from_string
from bdrxml import mods
from . import app_settings
import requests


logger = app_settings.logger
Expand All @@ -22,15 +24,49 @@ class BdrApiError(RuntimeError):


# Database Models


# validate biography roles against Roles db table
def validate_roles(value):
## check for commas ---------------------------------------------
if "," in value:
raise ValidationError( "Please use semi-colons, not commas, to separate roles." )
## validate actual role-entries ---------------------------------
entered_roles = value.split(";")
entered_roles = [role.strip() for role in entered_roles]
problem_roles = []
roles_objs = Role.objects.all()
roles_db_elements = [role_obj.text for role_obj in roles_objs]
for entered_role in entered_roles:
if entered_role not in roles_db_elements:
problem_roles.append(f'`{entered_role}`')
if len(problem_roles) == 1:
raise ValidationError( f"The following role is not in the `Roles` database-table: {problem_roles[0]}" )
elif len(problem_roles) > 1:
raise ValidationError( "The following roles are not in the `Roles` database-table: %s" % "; ".join(problem_roles) )
else:
return value

class Biography(models.Model):

name = models.CharField(max_length=254, help_text='Enter name as it appears in the book metadata')
trp_id = models.CharField(max_length=15, unique=True, blank=True, help_text='Optional: This value will be auto-generated by the server if the field is left blank or non-unique value is entered')
alternate_names = models.CharField(max_length=254, null=True, blank=True, help_text='Optional: enter alternate names separated by a semi-colon')
alternate_names = models.CharField(
max_length=254,
null=True,
blank=True,
help_text='Optional: enter alternate names separated by a semi-colon',
)
external_id = models.CharField(max_length=254, null=True, blank=True, help_text='Optional: enter Ulan id in the form of a URL; if there is no Ulan id, enter LCCN in the form of a URL')
birth_date = models.CharField(max_length=25, null=True, blank=True, help_text='Optional: enter birth date as yyyy-mm-dd (for sorting and filtering)')
death_date = models.CharField(max_length=25, null=True, blank=True, help_text='Optional: enter death date as yyyy-mm-dd')
roles = models.CharField(max_length=254, null=True, blank=True, help_text='Optional: enter roles, separated by a semi-colon')
roles = models.CharField(
max_length=254,
null=True,
blank=True,
help_text='Optional: enter roles, separated by a semi-colon',
validators =[validate_roles]
)
bio = models.TextField()

class Meta:
Expand Down Expand Up @@ -76,6 +112,8 @@ def save(self, *args, **kwargs):

def __str__(self):
return '%s (%s)' % (self.name, self.trp_id)




class Document(models.Model):
Expand Down Expand Up @@ -167,6 +205,7 @@ def __str__(self):
class Role(models.Model):
text = models.CharField(max_length=50, unique=True)
external_id = models.CharField(max_length=50, blank=True)
# text_aat = models.CharField(max_length=50, blank=True)

def __str__(self):
return self.text
Expand Down Expand Up @@ -210,6 +249,7 @@ def __contains__(self, item):
def search(cls, query="*", rows=6000):
query = f'?q={query}&fq=object_type:{cls.OBJECT_TYPE}&fl=*&fq=discover:BDR_PUBLIC&rows={rows}'
url = f'https://{app_settings.BDR_SERVER}/api/collections/{settings.TTWR_COLLECTION_PID}/{query}'
logger.debug( f'url, ``{url}``' )
r = requests.get(url)
if not r.ok:
raise BdrApiError(f'error from BDR Apis: {r.status_code} {r.text}')
Expand Down
11 changes: 10 additions & 1 deletion rome_app/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pprint

from django.conf import settings
from django.http import HttpResponse, HttpResponseNotFound, HttpResponseServerError, HttpResponseRedirect
from django.forms.formsets import formset_factory
Expand Down Expand Up @@ -98,7 +100,8 @@ def book_list(request):
try:
book_list = Book.search(query="genre_aat:book*"+buonanno)
except Exception as e:
logger.error(f'book_list view error getting book_list data: {e}')
# logger.error(f'book_list view error getting book_list data: {e}')
logger.exception(f'book_list view error getting book_list data: {e}')
return HttpResponse('error loading list of books', status=500)
sort_by = Book.SORT_OPTIONS.get(sort_by, 'title_sort')
book_list=sorted(book_list,key=methodcaller('sort_key', sort_by))
Expand All @@ -124,6 +127,7 @@ def book_list(request):

context['filter_options'] = [("Buonanno", "buonanno"), ("All", "both"), ("Library", "library")]
context['filter']=collection
# logger.debug( f'context, ``{pprint.pformat(context)}``' )
return render(request, 'rome_templates/book_list.html', context)


Expand Down Expand Up @@ -484,15 +488,19 @@ def filter_bios(fq, bio_list):


def biography_list(request):
logger.debug( '\n\nstarting biography_list()' )
fq = request.GET.get('filter', 'all')

bio_list = Biography.objects.all()
logger.debug( f'bio_list, ``{pprint.pformat(bio_list)}``' )

role_set = set()

for bio in bio_list:
if bio.roles:
bio.roles = [role.strip(" ") for role in bio.roles.split(';') if role.strip(" ") != '']
role_set |= set(bio.roles)
logger.debug( f'role_set, ``{pprint.pformat(role_set)}``' )

if fq != 'all':
bio_list = filter_bios(fq, bio_list)
Expand All @@ -518,6 +526,7 @@ def biography_list(request):
context['filter_options'].extend([(x, x) for x in sorted(role_set)])
context['filter'] = fq

logger.debug( f'context, ``{pprint.pformat(context)}``' )
return render(request, 'rome_templates/biography_list.html', context)


Expand Down

0 comments on commit 40f9d54

Please sign in to comment.