Skip to content

Commit

Permalink
Merge pull request #13 from ptressel/profile
Browse files Browse the repository at this point in the history
Provide a convenience function for retrieving a json list of basic person info.
  • Loading branch information
flavour committed Mar 26, 2012
2 parents 8d5c8f6 + dd69ca5 commit f97cc2b
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 12 deletions.
48 changes: 48 additions & 0 deletions controllers/pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,5 +667,53 @@ def load_search():
redirect(URL(r=request, c=prefix, f=function, args=["search"],vars=var))
return

# -----------------------------------------------------------------------------
def profile():
"""
Package up the fields needed to display brief person profiles.
This is specific to the SSF instance at the moment, and is used to
provide profile info for display on the Wordpress site.
@ToDo: Add server authentication?
@ToDo: Use sync instead?
@ToDo: Add a list of each person's roles.
@ToDo: Is there a virtual field for pr_person.name that provides a
localized representation of the name?
"""

import gluon.contrib.simplejson as json

# A caution when using executesql on joined tables:
# Even with as_dict = True, Web2py does not prefix field names with their
# table names nor nest fields for each table in their own dict.
# So make sure all selected field names are unique -- use "as" if needed.
raw_rows = db.executesql("""
select
pr_person.first_name, pr_person.middle_name, pr_person.last_name,
hrm_bio.short_bio, pr_image.url
from
pr_person
left join hrm_bio on (pr_person.id = hrm_bio.person_id)
left join pr_image on (pr_person.pe_id = pr_image.pe_id and pr_image.profile = 'T');
""",
as_dict = True)

rows = []
for raw_row in raw_rows:
row = {}

fname = raw_row.get("first_name", "")
mname = raw_row.get("middle_name", "")
lname = raw_row.get("last_name", "")
row["name"] = s3_format_fullname(fname, mname, lname, False)

row["bio"] = raw_row.get("short_bio", "")

row["picture"] = raw_row.get("url", "")

rows.append(row)

response.headers["Content-Type"] = "application/json"
rows_json = json.dumps(rows)
return rows_json

# END =========================================================================
4 changes: 4 additions & 0 deletions models/zzz_1st_roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ def parseACL(_acl):
#dict(c="gis", f="cache_feed", uacl=acl.ALL, oacl=default_oacl),
# Allow unauthenticated users to view feature queries
#dict(c="gis", f="feature_query", uacl=acl.NONE, oacl=default_oacl),
# Allow unauthenticated "profile" query. This is used to fetch
# basic user info -- name, image, bio, roles -- for display on
# another server (e.g. the SSF Wordpress site).
dict(c="pr", f="profile", uacl=acl.READ, oacl=default_oacl),
uid=sysroles.ANONYMOUS,
protected=True)

Expand Down
2 changes: 2 additions & 0 deletions modules/s3/s3aaa.py
Original file line number Diff line number Diff line change
Expand Up @@ -1766,6 +1766,8 @@ def resolve_role_ids(self, roles):
@param roles: list of role IDs or UIDs (or mixed)
"""

db = current.db

if not isinstance(roles, (list, tuple)):
roles = [roles]

Expand Down
45 changes: 33 additions & 12 deletions modules/s3/s3utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"s3_split_multi_value",
"s3_get_db_field_value",
"s3_filter_staff",
"s3_format_fullname",
"s3_fullname",
"s3_represent_facilities",
"s3_represent_multiref",
Expand Down Expand Up @@ -444,6 +445,37 @@ def s3_filter_staff(r):
except:
pass

# =============================================================================
def s3_format_fullname(fname=None, mname=None, lname=None, truncate=True):
"""
Returns the full name of a person
@param fname: the person's pr_person.first_name value
@param mname: the person's pr_person.middle_name value
@param lname: the person's pr_person.last_name value
@param truncate: truncate the name to max 24 characters
"""

name = ""
if fname or mname or lname:
if not fname:
fname = ""
if not mname:
mname = ""
if not lname:
lname = ""
if truncate:
fname = "%s" % s3_truncate(fname, 24)
mname = "%s" % s3_truncate(mname, 24)
lname = "%s" % s3_truncate(lname, 24, nice = False)
if not mname or mname.isspace():
name = ("%s %s" % (fname, lname)).rstrip()
else:
name = ("%s %s %s" % (fname, mname, lname)).rstrip()
if truncate:
name = s3_truncate(name, 24, nice = False)
return name

# =============================================================================
def s3_fullname(person=None, pe_id=None, truncate=True):
"""
Expand Down Expand Up @@ -491,18 +523,7 @@ def s3_fullname(person=None, pe_id=None, truncate=True):
mname = record.pr_person.middle_name.strip()
if record.pr_person.last_name:
lname = record.pr_person.last_name.strip()

if fname:
fname = "%s " % s3_truncate(fname, 24)
if mname:
mname = "%s " % s3_truncate(mname, 24)
if lname:
lname = "%s " % s3_truncate(lname, 24, nice = False)

if mname.isspace():
return "%s%s" % (fname, lname)
else:
return "%s%s%s" % (fname, mname, lname)
return s3_format_fullname(fname, mname, lname, truncate)
else:
return DEFAULT

Expand Down

0 comments on commit f97cc2b

Please sign in to comment.