Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: error handling for view functions #228

Open
wants to merge 3 commits into
base: ui-improvements
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 30 additions & 41 deletions Access/accessrequest_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,44 +117,33 @@ def __init__(self):
def get_request_access(request):
""" Get list of all accesses for requesting access to """
context = {}
try:
for each_tag, each_module in \
helpers.get_available_access_modules().items():
if each_tag in request.GET.getlist("accesses"):
if "accesses" not in context:
context["accesses"] = []
context["genericForm"] = True
try:
extra_fields = each_module.get_extra_fields()
except Exception:
extra_fields = []
try:
notice = each_module.get_notice()

except Exception:
notice = ""
context["accesses"].append({
"formDesc": each_module.access_desc(),
"accessTag": each_tag,
"accessTypes": each_module.access_types(),
"accessRequestData": each_module.access_request_data(
request, is_group=False
),
"extraFields": extra_fields,
"notice": notice,
"accessRequestPath":
each_module.fetch_access_request_form_path(),
})
except Exception as exception:
logger.exception(exception)
context = {}
context["status"] = {
"title": "Error Occured",
"msg": (
"There was an error in getting the requested access"
" resources. Please contact Admin"
),
}
for each_tag, each_module in \
helpers.get_available_access_modules().items():
if each_tag in request.GET.getlist("accesses"):
if "accesses" not in context:
context["accesses"] = []
context["genericForm"] = True
try:
extra_fields = each_module.get_extra_fields()
except Exception:
extra_fields = []
try:
notice = each_module.get_notice()

except Exception:
notice = ""
context["accesses"].append({
"formDesc": each_module.access_desc(),
"accessTag": each_tag,
"accessTypes": each_module.access_types(),
"accessRequestData": each_module.access_request_data(
request, is_group=False
),
"extraFields": extra_fields,
"notice": notice,
"accessRequestPath":
each_module.fetch_access_request_form_path(),
})
return context


Expand Down Expand Up @@ -441,6 +430,9 @@ def create_request(auth_user, access_request_form):
access_module = helpers.get_available_access_module_from_tag(access_tag)
module_access_labels = access_module.validate_request(access_request_form, auth_user.user)

if access_module.can_auto_approve():
raise ImplementationPendingException()

for _, access_label in enumerate(module_access_labels):
request_id = (
auth_user.username
Expand All @@ -458,9 +450,6 @@ def create_request(auth_user, access_request_form):
logger.info("Duplicate request found: %s", access_create_error)
continue

if access_module.can_auto_approve():
raise ImplementationPendingException()

json_response["status"] = {
"title": REQUEST_SUCCESS_MSG["title"].format(
access_tag=access_tag
Expand Down
12 changes: 9 additions & 3 deletions Access/decorators.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.core.exceptions import PermissionDenied
from Access.helpers import get_possible_approver_permissions
from django.core.paginator import Paginator
from django.template.response import TemplateResponse


def user_admin_or_ops(function):
Expand Down Expand Up @@ -54,9 +55,14 @@ def wrap(request, *args, **kwargs):

def paginated_search(view_function):
def wrap(request, *args, **kwargs):
template, context = view_function(request, *args, **kwargs)
if template == "No render":
return context
response = view_function(request, *args, **kwargs)
if type(response) is not tuple:
return response
template, context = response
if not context:
if type(template) == TemplateResponse:
return template.render()
return template
if context.get("error"):
template.context_data = context
return template.render()
Expand Down
25 changes: 9 additions & 16 deletions Access/group_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
"msg": "Error Occured while loading the page. Please contact admin",
}

INVALID_REQUEST_ERROR = {
"error_msg": "Invalid Request",
"msg": "Error in request parameters, please check and request."
}

USER_UNAUTHORIZED_MESSAGE = "User unauthorised to perform the action."
GROUP_ACCESS_MAPPING_NOT_FOUND = "Group Access Mapping not found in the database."

Expand Down Expand Up @@ -128,10 +133,8 @@ def create_group(request):
except Exception as e:
logger.exception(e)
logger.error("Error in Create New Group request.")
context = {}
context["error"] = {
"error_msg": INTERNAL_ERROR_MESSAGE["error_msg"],
"msg": INTERNAL_ERROR_MESSAGE["msg"],
context = {
"error": INVALID_REQUEST_ERROR
}
return context

Expand Down Expand Up @@ -215,15 +218,6 @@ def get_group_access_list(auth_user, group_name):
return context

auth_user = auth_user
if not auth_user.user.is_allowed_admin_actions_on_group(group):
logger.debug("Permission denied, requester is non owner")
context = {
"error": {
"error_msg": NON_OWNER_PERMISSION_DENIED_ERROR["error_msg"],
"msg": NON_OWNER_PERMISSION_DENIED_ERROR["msg"],
}
}
return context

group_members = group.get_all_members().filter(status="Approved")
group_members = [
Expand Down Expand Up @@ -638,8 +632,7 @@ def accept_member(auth_user, requestId, shouldRender=True):


def get_group_access(form_data, auth_user):
data = dict(form_data.lists())
group_name = data["groupName"][0]
group_name = form_data.get("groupName")
context = {}
context["accesses"] = []

Expand All @@ -651,7 +644,7 @@ def get_group_access(form_data, auth_user):
context["status"] = validation_error
return context

access_module_list = data["accessList"]
access_module_list = form_data.getlist("accessList")
for module_value in access_module_list:
module = helper.get_available_access_modules()[module_value]
try:
Expand Down
Loading