def workbook_share(request, workbook_id=0): emails = re.split('\s*,\s*', request.POST['share_users'].strip()) workbook = request.user.workbook_set.get(id=workbook_id, active=True) create_share(request, workbook, emails, 'Workbook') return JsonResponse({ 'status': 'success' })
def project_share(request, project_id=0): proj = request.user.project_set.get(id=project_id) emails = re.split('\s*,\s*', request.POST['share_users'].strip()) create_share(request, proj, emails, 'Project') return JsonResponse({ 'status': 'success' })
def workbook_share(request, workbook_id=0): status = None result = None try: emails = re.split('\s*,\s*', request.POST['share_users'].strip()) users_not_found = [] req_user = None try: req_user = User.objects.get(id=request.user.id) except ObjectDoesNotExist as e: raise Exception("{} is not a user ID in this database!".format( str(request.user.id))) workbook_to_share = request.user.workbook_set.get(id=workbook_id, active=True) if workbook_to_share.owner.id != req_user.id: raise Exception(" {} is not the owner of this workbook!".format( req_user.email)) for email in emails: try: User.objects.get(email=email) except ObjectDoesNotExist as e: users_not_found.append(email) if len(users_not_found) > 0: status = 'error' result = { 'msg': 'The following user emails could not be found; please ask them to log into the site first: ' + ", ".join(users_not_found) } else: create_share(request, workbook_to_share, emails, 'Workbook') status = 'success' except Exception as e: logger.error("[ERROR] While trying to share a workbook:") logger.exception(e) status = 'error' result = { 'msg': 'There was an error while attempting to share this workbook.' } finally: if not status: status = 'error' result = { 'msg': 'An unknown error has occurred while sharing this workbook.' } return JsonResponse({'status': status, 'result': result})
def program_share(request, program_id=0): message = None status = None status_text = None try: # Verify all emails are in our user database emails = re.split('\s*,\s*', request.POST['share_users'].strip()) users_not_found = [] users = [] req_user = None try: req_user = User.objects.get(id=request.user.id) except ObjectDoesNotExist as e: raise Exception("{} is not a user ID in this database!".format(str(request.user.id))) for email in emails: try: user = User.objects.get(email=email) users.append(user) except ObjectDoesNotExist as e: users_not_found.append(email) # If any aren't found, warn the user and don't share if len(users_not_found) > 0: status_text = 'error' # An actual error will close the modal, so this is a '200 error' i.e. 'ok request, but can't carry it out' status = 200 message = 'The following user emails could not be found; please ask them to log into the site first: ' + ", ".join(users_not_found) # Otherwise, share the program else: program = request.user.program_set.get(id=program_id) create_share(request, program, emails, 'program') status = 200 status_text = 'success' message = 'Program ID {} has been successfully shared with the following user(s): {}'.format(str(program_id),", ".join(emails)) except Exception as e: logger.error("[ERROR] While attempting to share program ID {}: ".format(str(program_id))) logger.exception(e) status_text = 'error' status = 500 message = 'There was an error while attempting to share program ID {}.'.format(str(program_id)) return JsonResponse({ 'status': status_text, 'result': {'msg': message} }, status=status)