Skip to content

Commit

Permalink
Merge pull request #111 from CityOfNewYork/hotfix/OP-584
Browse files Browse the repository at this point in the history
hotfix/OP-584 - added logging to functions in db_helpers, notifications, prr, upload_…
  • Loading branch information
BrandonTang committed Apr 7, 2016
2 parents 7fed9e2 + 0fd1420 commit dd95004
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 52 deletions.
68 changes: 34 additions & 34 deletions public_records_portal/db_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def id_counter():
currentRequestId = id_generator.next()


# @export "get_subscriber"
def get_subscriber(request_id, user_id=None):
# Returns the subscriber for a given request by user ID
app.logger.info("def get_subscriber")
if request_id and user_id:
return Subscriber.query.filter_by(user_id=user_id).filter_by(
request_id=request_id).first()
Expand All @@ -42,23 +42,23 @@ def get_subscriber(request_id, user_id=None):
return None


# @export "get_count"
def get_count(obj_type):
app.logger.info("def get_count")
return db.session.query(func.count(eval(obj_type).id)).scalar()


# @export "get_obj"
def get_obj(obj_type, obj_id):
""" Query the database for an object via its class/type (defined in models.py) and ID and return the object. """
app.logger.info("def get_obj")
if not obj_id:
return None
return eval(obj_type).query.get(obj_id)


# @export "get_objs"
def get_objs(obj_type):
""" Query the database for all objects of a certain class/type (defined in models.py) and return queryset. """
# There has to be a better way of doing this
app.logger.info("def get_objs")
if obj_type == "User":
return User.query.all()
elif obj_type == "Request":
Expand All @@ -76,8 +76,8 @@ def get_objs(obj_type):
return None


# @export "get_avg_response_time"
def get_avg_response_time(department):
app.logger.info("def get_avg_response_time")
app.logger.info(
"\n\nCalculating average response time for department: %s" % department)
d = Department.query.filter_by(name=department).first()
Expand All @@ -100,45 +100,43 @@ def get_avg_response_time(department):
return None


# @export "get_request_by_owner"
def get_request_by_owner(owner_id):
""" Return the request that a particular owner belongs to """
app.logger.info("def get_request_by_owner")
if not owner_id:
return None
return Owner.query.get(owner_id).request


# @export "get_owners_by_user_id"
def get_owners_by_user_id(user_id):
""" Return the queryset of owners for a particular user. (A user can be associated with multiple owners)."""
app.logger.info("def get_owners_by_user_id")
if not user_id:
return None
return Owner.query.filter_by(user_id=user_id)


def get_contact_by_dept(dept):
""" Return the contact for a given department. """
app.logger.info("def get_contact_by_dept")
d = Department.query.filter(Department.name == dept).first()
if d and d.primary_contact:
return d.primary_contact.email
return None


### @export "get_backup_by_dept"
def get_backup_by_dept(dept):
""" Return the backup for a given department. """
app.logger.info("def get_backup_by_dept")
d = Department.query.filter(Department.name == dept).first()
if d and d.backup_contact:
return d.backup_contact.email
return None


### @export "put_obj"


# @export "put_obj"
def put_obj(obj):
""" Add and commit the object to the database. Return true if successful. """
app.logger.info("def put_obj")
if obj:
db.session.add(obj)
db.session.commit()
Expand All @@ -147,9 +145,9 @@ def put_obj(obj):
return False


# @export "get_attribute"
def get_attribute(attribute, obj_id=None, obj_type=None, obj=None):
""" Obtain the object by obj_id and obj_type if obj is not provided, and return the specified attribute for that object. """
app.logger.info("def get_attribute")
if obj_id and obj_type:
obj = get_obj(obj_type, obj_id)
if obj:
Expand All @@ -160,9 +158,9 @@ def get_attribute(attribute, obj_id=None, obj_type=None, obj=None):
return None


# @export "update_obj"
def update_obj(attribute, val, obj_type=None, obj_id=None, obj=None):
""" Obtain the object by obj_id and obj_type if obj is not provided, and update the specified attribute for that object. Return true if successful. """
app.logger.info("def update_obj")
app.logger.info(
"\n\nUpdating attribute: %s with value: %s for obj_type: %s, obj_id: %s, obj: %s" % (
attribute, val, obj_type, obj_id, obj))
Expand All @@ -179,20 +177,20 @@ def update_obj(attribute, val, obj_type=None, obj_id=None, obj=None):
return False


# @export "create_QA"
def create_QA(request_id, question, user_id):
""" Create a QA object and return the ID. """
app.logger.info("def create_QA")
qa = QA(request_id=request_id, question=question, user_id=user_id)
db.session.add(qa)
db.session.commit()
return qa.id


# @export "create_request"
def create_request(id=id, agency=None, summary=None, text=None, user_id=None,
offline_submission_type=None,
date_received=None):
""" Create a Request object and return the ID. """
app.logger.info("def create_request")
agency_id = Department.query.filter_by(name=agency).first().id
req = Request(id=id, agency=agency_id, summary=summary, text=text, creator_id=user_id,
offline_submission_type=offline_submission_type, date_received=date_received)
Expand All @@ -202,9 +200,9 @@ def create_request(id=id, agency=None, summary=None, text=None, user_id=None,
return req.id


# @export "create_subscriber"
def create_subscriber(request_id, user_id):
""" Create a Subscriber object and return the ID. """
app.logger.info("def create_subscriber")
subscriber = Subscriber.query.filter_by(
request_id=request_id, user_id=user_id).first()
if not subscriber:
Expand All @@ -215,9 +213,9 @@ def create_subscriber(request_id, user_id):
return subscriber.id, False


# @export "create_note"
def create_note(request_id, text, user_id, privacy):
""" Create a Note object and return the ID. """
app.logger.info("def create_note")
try:
note = Note(request_id=request_id, text=text, user_id=user_id,
privacy=privacy)
Expand All @@ -230,9 +228,9 @@ def create_note(request_id, text, user_id, privacy):
return None


# @export "create_record"
def create_record(request_id, user_id, description, doc_id=None, filename=None,
access=None, url=None, privacy=True):
app.logger.info("def create_record")
try:
record = Record(doc_id=doc_id, request_id=request_id, user_id=user_id,
description=description, filename=filename, url=url,
Expand All @@ -246,13 +244,14 @@ def create_record(request_id, user_id, description, doc_id=None, filename=None,


def remove_obj(obj_type, obj_id):
app.logger.info("def remove_obj")
obj = get_obj(obj_type, obj_id)
db.session.delete(obj)
db.session.commit()


# @export "create_answer"
def create_answer(qa_id, subscriber_id, answer):
app.logger.info("def create_answer")
qa = get_obj("QA", qa_id)
if not qa:
app.logger.info("\n\nQA with id: %s does not exist" % (qa_id))
Expand All @@ -266,18 +265,17 @@ def create_answer(qa_id, subscriber_id, answer):

# Following three functions are for integration with Mozilla Persona

# @export "get_user"
def get_user(kwargs):
app.logger.info("def get_user")
return User.query.filter(User.email == kwargs.get('email')).filter(
User.is_staff == True).first()


# @export "get_user_by_id"
def get_user_by_id(id):
app.logger.info("def get_user_by_id")
return User.query.get(id)


### @export "authenticate_login"
def authenticate_login(email, password):
"""
Expand All @@ -288,9 +286,10 @@ def authenticate_login(email, password):
:return: The user object, if the user is authenticated, otherwise None
:rtype: User object
"""
app.logger.info("def authenticate_login")
if app.config['USE_LDAP'] == 'True':
app.logger.info("Use LDAP: %s" % app.config['USE_LDAP'])
# # Setup the LDAP Options
# Setup the LDAP Options
if app.config['LDAP_USE_TLS']:
# Sets up TLS for LDAP connection
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT,
Expand Down Expand Up @@ -344,10 +343,11 @@ def authenticate_login(email, password):
return user
return None


def create_or_return_user(email=None, alias=None, first_name=None, last_name=None, phone=None, fax=None, address1=None, address2=None, city=None, state=None,
zipcode=None, department=None, contact_for=None, backup_for=None, not_id=False, is_staff=None,
password=None, role=None):
app.logger.info("\n\nCreating or returning user...")
app.logger.info("def create_or_return_user")
try:
if phone is not None:
phone = str(phone.country_code) + str(phone.national_number)
Expand Down Expand Up @@ -407,9 +407,9 @@ def create_or_return_user(email=None, alias=None, first_name=None, last_name=Non
return user.id


# @export "create_user"
def create_user(email=None, alias=None, first_name=None, last_name=None, phone=None, fax=None, address1=None, address2=None, city=None, state=None, zipcode=None,
department=None, contact_for=None, backup_for=None, password=None, is_staff=None, role=None):
app.logger.info("def create_user")
user = User(email=email, alias=alias, first_name=first_name, last_name=last_name, phone=phone, fax=fax, address1=address1,
address2=address2, city=city, state=state,
zipcode=zipcode, department=department, contact_for=contact_for,
Expand All @@ -422,9 +422,9 @@ def create_user(email=None, alias=None, first_name=None, last_name=None, phone=N
return user


# @export "update_user"
def update_user(user, alias=None, first_name=None, last_name=None, phone=None, fax=None, address1=None, address2=None, city=None, state=None, zipcode=None,
department=None, contact_for=None, backup_for=None, is_staff=None, role=None):
app.logger.info("def update_user")
if alias:
user.alias = alias
if first_name:
Expand Down Expand Up @@ -472,10 +472,10 @@ def update_user(user, alias=None, first_name=None, last_name=None, phone=None, f
return user


# @export "create_owner"
def create_owner(request_id, reason, email=None, user_id=None):
""" Adds a staff member to the request without assigning them as current owner. (i.e. "participant")
Useful for multidepartmental requests."""
app.logger.info("def create_owner")
if not user_id:
user_id = create_or_return_user(email=email)
participant = Owner(request_id=request_id, user_id=user_id, reason=reason)
Expand All @@ -485,8 +485,8 @@ def create_owner(request_id, reason, email=None, user_id=None):
return participant.id


# @export "change_request_status"
def change_request_status(request_id, status):
app.logger.info("def change_request_status")
req = get_obj("Request", request_id)
req.prev_status = req.status
req.status = status
Expand All @@ -505,18 +505,18 @@ def change_request_status(request_id, status):
db.session.commit()


# @export "find_request"
def find_request(text):
app.logger.info("def find_request")
req = Request.query.filter_by(summary=text).first()
if req:
return req
return None


# @export "add_staff_participant"
def add_staff_participant(request_id, is_point_person=False, email=None,
user_id=None, reason=None):
""" Creates an owner for the request if it doesn't exist, and returns the owner ID and True if a new one was created. Returns the owner ID and False if existing."""
app.logger.info("def add_staff_participant")
is_new = True
if not user_id:
user_id = create_or_return_user(email=email)
Expand Down Expand Up @@ -550,8 +550,8 @@ def add_staff_participant(request_id, is_point_person=False, email=None,
return participant.id, is_new


# @export "remove_staff_participant"
def remove_staff_participant(owner_id, reason=None):
app.logger.info("def remove_staff_participant")
participant = Owner.query.get(owner_id)
participant.active = False
participant.date_updated = datetime.now().isoformat()
Expand All @@ -564,9 +564,9 @@ def remove_staff_participant(owner_id, reason=None):
return owner_id


# @export "update_subscriber"
def update_subscriber(request_id, alias, phone):
""" Update a subscriber for a given request with the name and phone number provided. """
app.logger.info("def update_subscriber")
user_id = create_or_return_user(alias=alias, phone=phone)
r = Request.query.get(request_id)
sub = r.subscribers[0]
Expand All @@ -578,8 +578,8 @@ def update_subscriber(request_id, alias, phone):
request_id, alias, phone))


### @export "set_random_password"
def set_random_password(email):
app.logger.info("def set_random_password")
user = User.query.filter(User.email == func.lower(email)).first()
# Must be staff or admin to reset password
if user and (user.is_staff == True or user.is_admin() == True):
Expand Down
Loading

0 comments on commit dd95004

Please sign in to comment.