Skip to content
This repository has been archived by the owner on Nov 10, 2021. It is now read-only.

Commit

Permalink
IRWS pep8.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffFranklin committed Mar 7, 2016
1 parent 863a715 commit 77f161c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 66 deletions.
15 changes: 8 additions & 7 deletions restclients/dao_implementation/irws.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@

logger = logging.getLogger(__name__)

# This seemed like a good number based on a test using a class w/ 300 students.
# The range 10-50 all did well, so this seemed like the most sociable, high performing
# number to choose
# This seemed like a good number based on a test using a class w/ 300
# students. The range 10-50 all did well, so this seemed like the
# most sociable, high performing number to choose
IRWS_MAX_POOL_SIZE = 10


class File(object):
"""
The File DAO implementation returns generally static content. Use this
Expand Down Expand Up @@ -122,12 +123,12 @@ def postURL(self, url, headers, body):
service_name='irws')

_pool = None

@property
def pool(self):
if Live._pool is None:
Live._pool = get_con_pool(settings.RESTCLIENTS_IRWS_HOST,
settings.RESTCLIENTS_IRWS_KEY_FILE,
settings.RESTCLIENTS_IRWS_CERT_FILE,
max_pool_size=IRWS_MAX_POOL_SIZE)
settings.RESTCLIENTS_IRWS_KEY_FILE,
settings.RESTCLIENTS_IRWS_CERT_FILE,
max_pool_size=IRWS_MAX_POOL_SIZE)
return Live._pool

87 changes: 44 additions & 43 deletions restclients/irws.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
import logging
from django.conf import settings
from restclients.dao import IRWS_DAO
from restclients.exceptions import InvalidRegID, InvalidNetID, InvalidEmployeeID
from restclients.exceptions import InvalidIdCardPhotoSize
from restclients.exceptions import InvalidNetID
from restclients.exceptions import DataFailureException
from restclients.exceptions import InvalidIRWSName, InvalidIRWSPerson, IRWSPersonNotFound
from restclients.exceptions import InvalidIRWSName, InvalidIRWSPerson
from restclients.exceptions import IRWSPersonNotFound
from restclients.models.irws import Name, UwhrPerson, PersonIdentity
from StringIO import StringIO
from urllib import urlencode

logger = logging.getLogger(__name__)

Expand All @@ -27,18 +25,21 @@ def __init__(self, actas=None):
self.actas = actas
self._re_regid = re.compile(r'^[A-F0-9]{32}$', re.I)
self._re_personal_netid = re.compile(r'^[a-z][a-z0-9]{0,7}$', re.I)
self._re_admin_netid = re.compile(r'^[a-z]adm_[a-z][a-z0-9]{0,7}$', re.I)
self._re_application_netid = re.compile(r'^a_[a-z0-9\-\_\.$.]{1,18}$', re.I)
self._re_admin_netid = re.compile(
r'^[a-z]adm_[a-z][a-z0-9]{0,7}$', re.I)
self._re_application_netid = re.compile(
r'^a_[a-z0-9\-\_\.$]{1,18}$', re.I)
self._re_employee_id = re.compile(r'^\d{9}$')
""" Consider adding back +, #, and % when irws stops decoding """
self._re_name_part = re.compile(r'^[\w !$&\'*\-,.?^_`{}~#+%]*$')
self._service_name = settings.RESTCLIENTS_IRWS_SERVICE_NAME

def get_identity_by_netid(self, netid):
"""
Returns a restclients.irws.PersonIdentity object for the given netid. If the
netid isn't found, nothing will be returned. If there is an error
communicating with the IRWS, a DataFailureException will be thrown.
Returns a restclients.irws.PersonIdentity object for the given
netid. If the netid isn't found, nothing will be returned.
If there is an error communicating with the IRWS, a
DataFailureException will be thrown.
"""
if not self.valid_uwnetid(netid):
raise InvalidNetID(netid)
Expand Down Expand Up @@ -79,7 +80,8 @@ def put_name_by_netid(self, netid, data):
pd = self.valid_irws_name_from_json(data)
dao = IRWS_DAO()
url = "/%s/v1/name/uwnetid=%s" % (self._service_name, netid.lower())
response = dao.putURL(url, {"Accept": "application/json"}, json.dumps(pd))
response = dao.putURL(url, {"Accept": "application/json"},
json.dumps(pd))

if response.status != 200:
raise DataFailureException(url, response.status, response.data)
Expand All @@ -88,9 +90,10 @@ def put_name_by_netid(self, netid, data):

def get_hr_person_by_uri(self, uri):
"""
Returns a restclients.irws.UwhrPerson object for the given uri (from identity).
If the record id isn't found, nothing will be returned. If there is an error
communicating with the IRWS, a DataFailureException will be thrown.
Returns a restclients.irws.UwhrPerson object for the given uri
(from identity). If the record id isn't found, nothing will
be returned. If there is an error communicating with the IRWS,
a DataFailureException will be thrown.
"""
url = "/%s/v1%s" % (self._service_name, uri)
response = IRWS_DAO().getURL(url, {"Accept": "application/json"})
Expand All @@ -109,13 +112,15 @@ def get_hepps_person_by_netid(self, netid):

def get_hr_person_by_netid(self, netid):
"""
Returns a restclients.irws.UwhrPerson object for a given netid. Two round
trips - one to get the identity, and a second to look up a person based on
the 'hepps' or 'uwhr' uri in the payload
Returns a restclients.irws.UwhrPerson object for a given
netid. Two round trips - one to get the identity, and a second
to look up a person based on the 'hepps' or 'uwhr' uri in the
payload
"""
identity = self.get_identity_by_netid(netid)
if not {'hepps', 'uwhr'} & set(identity.identifiers.keys()):
raise IRWSPersonNotFound('netid ' + netid + ' not a hepps/uwhr person')
raise IRWSPersonNotFound(
'netid {} not a hepps/uwhr person'.format(netid))
source = 'uwhr' if 'uwhr' in identity.identifiers.keys() else 'hepps'
return self.get_hr_person_by_uri(identity.identifiers[source])

Expand All @@ -136,23 +141,24 @@ def post_hr_person_by_netid(self, netid, data):
hepps_person = self.valid_hr_person_from_json(data)
identity = self.get_identity_by_netid(netid)
if not {'hepps', 'uwhr'} & set(identity.identifiers.keys()):
raise IRWSPersonNotFound('netid ' + netid + ' not a hepps/uwhr person')
raise IRWSPersonNotFound(
'netid {} not a hepps/uwhr person'.format(netid))
source = 'uwhr' if 'uwhr' in identity.identifiers.keys() else 'hepps'
post_url = '/{}/v1{}'.format(self._service_name,
identity.identifiers[source])
response = IRWS_DAO().postURL(post_url,
{'Accept': 'application/json'},
json.dumps(hepps_person))
identity.identifiers[source])
response = IRWS_DAO().postURL(
post_url, {'Accept': 'application/json'}, json.dumps(hepps_person))
if response.status != 200:
raise DataFailureException(post_url, response.status, response.data)
raise DataFailureException(post_url, response.status,
response.data)

return response.status

def valid_uwnetid(self, netid):
uwnetid = str(netid)
return (self._re_personal_netid.match(uwnetid) != None
or self._re_admin_netid.match(uwnetid) != None
or self._re_application_netid.match(uwnetid) != None)
return (self._re_personal_netid.match(uwnetid) is not None or
self._re_admin_netid.match(uwnetid) is not None or
self._re_application_netid.match(uwnetid) is not None)

def valid_uwregid(self, regid):
return True if self._re_regid.match(str(regid)) else False
Expand Down Expand Up @@ -207,7 +213,7 @@ def valid_hr_person_from_json(self, data):

def valid_name_part(self, name):
return (len(name) <= 64 and
self._re_name_part.match(name) != None)
self._re_name_part.match(name) is not None)

def _hr_person_from_json(self, data):
"""
Expand All @@ -217,31 +223,27 @@ def _hr_person_from_json(self, data):
person = UwhrPerson()
person.validid = person_data['validid']
person.regid = person_data['regid']
if 'studentid' in person_data: person.studentid = person_data['studentid']
if 'studentid' in person_data:
person.studentid = person_data['studentid']

person.fname = person_data['fname']
person.lname = person_data['lname']

person.category_code = person_data['category_code']
person.category_name = person_data['category_name']
if 'wp_publish' in person_data: person.wp_publish = person_data['wp_publish']
else: person.wp_publish = 'N' # default to no

setattr(person, 'wp_publish',
person_data.get('wp_publish', 'N'))
return person

def _name_from_json(self, data):
nd = json.loads(data)['name'][0]
remap = {'formal_sname': 'formal_lname',
'display_sname': 'display_lname'}
nd = {k if k not in remap else remap[k]: v for k, v in nd.items()}
name = Name()
name.validid = nd['validid']
if 'formal_cname' in nd: name.formal_cname = nd['formal_cname']
if 'formal_fname' in nd: name.formal_fname = nd['formal_fname']
if 'formal_sname' in nd: name.formal_lname = nd['formal_sname']
if 'formal_privacy' in nd: name.formal_privacy = nd['formal_privacy']
if 'display_cname' in nd: name.display_cname = nd['display_cname']
if 'display_fname' in nd: name.display_fname = nd['display_fname']
if 'display_mname' in nd: name.display_mname = nd['display_mname']
if 'display_sname' in nd: name.display_lname = nd['display_sname']
if 'display_privacy' in nd: name.display_privacy = nd['display_privacy']
for attribute in nd.keys():
setattr(name, attribute, nd[attribute])
return name

def _identity_from_json(self, data):
Expand All @@ -251,4 +253,3 @@ def _identity_from_json(self, data):
ident.regid = idj['regid']
ident.identifiers = copy.deepcopy(idj['identifiers'])
return ident

23 changes: 7 additions & 16 deletions restclients/models/irws.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@
from django.template import Context, loader
from base64 import b64encode, b64decode
from datetime import datetime
from restclients.models.base import RestClientsModel
from restclients.util.date_formator import abbr_week_month_day_str
from restclients.exceptions import InvalidCanvasIndependentStudyCourse
from restclients.exceptions import InvalidCanvasSection


# IRWS Person Identity
class PersonIdentity(models.Model):
class PersonIdentity(RestClientsModel):
regid = models.CharField(max_length=32)

def __init__(self, *args, **kwargs):
super(PersonIdentity, self).__init__(*args, **kwargs)
self.identifiers = {}


# IRWS Name
class Name(models.Model):
class Name(RestClientsModel):
validid = models.CharField(max_length=32, unique=True)
formal_cname = models.CharField(max_length=255)
formal_fname = models.CharField(max_length=255)
Expand All @@ -30,7 +31,6 @@ class Name(models.Model):
display_lname = models.CharField(max_length=255)
display_privacy = models.CharField(max_length=32)


def json_data(self):
return {"formal_cname": self.formal_cname,
"formal_fname": self.formal_fname,
Expand All @@ -46,19 +46,15 @@ def json_data(self):
def __eq__(self, other):
return self.uwregid == other.uwregid

class Meta:
app_label = "restclients"


# IRWS Uwhr Person
class UwhrPerson(models.Model):
class UwhrPerson(RestClientsModel):
validid = models.CharField(max_length=32, unique=True)
regid = models.CharField(max_length=32,
db_index=True,
unique=True)
db_index=True,
unique=True)
studentid = models.CharField(max_length=32)


fname = models.CharField(max_length=255)
lname = models.CharField(max_length=255)
category_code = models.CharField(max_length=4)
Expand Down Expand Up @@ -118,8 +114,3 @@ def json_data(self):

def __eq__(self, other):
return self.uwregid == other.uwregid

class Meta:
app_label = "restclients"


0 comments on commit 77f161c

Please sign in to comment.