def process_request(self, request): if request.META.get('HTTP_AUTHORIZATION'): token_key = request.META.get('HTTP_AUTHORIZATION').split(' ')[-1] try: request.user = Token.objects.get(key=token_key).user except: pass if not request.user.is_anonymous(): role = None if request.session.get('role'): try: role = Role.objects.select_related('group', 'organization').get(pk=request.session.get('role'), user=request.user) except Role.DoesNotExist: pass if not role: roles = Role.get_active_roles(request.user) # roles = Role.objects.filter(user=request.user).select_related('group', 'organization') if roles: role = roles[0] request.session['role'] = role.id if role: request.__class__.role = role request.__class__.organization = role.organization request.__class__.project = role.project request.__class__.site = role.site if "Super Admin" in request.user.user_roles.all().distinct('group__name').values_list('group__name', flat = True): request.__class__.group = Group.objects.get(pk=5) else: request.__class__.group = role.group # request.__class__.roles = Role.objects.filter(user=request.user, organization=role.organization) request.__class__.roles = Role.get_active_roles(request.user) request.__class__.is_super_admin = 'Super Admin' in request.user.user_roles.all().distinct('group__name').values_list('group__name', flat = True) # for role in request.roles: # groups.append(role.group) # request.__class__.groups = groups else: # request = clear_roles(request) logout(request) return render(request, 'fieldsight/permission_denied.html') else: request = clear_roles(request)
def current_usertwo(request): user = request.user if user.is_anonymous(): return Response({'code': 401, 'message': 'Unauthorized User'}) elif not user.user_profile.organization: return Response({ 'code': 403, 'message': 'Sorry, you are not assigned to any organization yet. ' 'Please contact your project manager.' }) else: site_supervisor = UserRole.get_active_site_roles_exists(user) users_payload = { 'username': user.username, 'full_name': user.first_name, 'email': user.email, 'server_time': datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S'), 'is_supervisor': site_supervisor, 'last_login': user.last_login, 'organization': user.user_profile.organization.name, 'organization_url': user.user_profile.organization.logo.url, 'address': user.user_profile.address, 'skype': user.user_profile.skype, 'phone': user.user_profile.phone, 'profile_pic': user.user_profile.profile_picture.url, 'profile_data': UserSerializerProfile(user.user_profile).data # 'languages': settings.LANGUAGES, # profile data here, role supervisor } response_data = {'code': 200, 'data': users_payload} return Response(response_data)
def add_supervisor(request, pk): obj = get_object_or_404( Site, pk=int(pk)) group = Group.objects.get(name__exact="Site Supervisor") role_obj = UserRole(site=obj, group=group) if request.method == 'POST': form = SetSupervisorForm(data=request.POST, instance=role_obj, request=request) if form.is_valid(): role_obj = form.save(commit=False) user_id = request.POST.get('user') role_obj.user_id = int(user_id) role_obj.save() messages.add_message(request, messages.INFO, 'Site Supervisor Added') return HttpResponseRedirect(reverse("fieldsight:site-dashboard", kwargs={'pk': obj.pk})) else: form = SetSupervisorForm(instance=role_obj, request=request) return render(request, "fieldsight/add_supervisor.html", {'obj':obj,'form':form})
def process_request(self, request): if request.META.get('HTTP_AUTHORIZATION'): token_key = request.META.get('HTTP_AUTHORIZATION').split(' ')[-1] try: request.user = Token.objects.get(key=token_key).user except: pass if not request.user.is_anonymous(): role = None if request.session.get('role'): try: role = Role.objects.select_related( 'group', 'organization').get(pk=request.session.get('role'), user=request.user) except Role.DoesNotExist: pass if not role: roles = Role.get_active_roles(request.user) # roles = Role.objects.filter(user=request.user).select_related('group', 'organization') if roles: role = roles[0] request.session['role'] = role.id if role: request.__class__.role = role request.__class__.organization = role.organization request.__class__.project = role.project request.__class__.site = role.site request.__class__.group = role.group # request.__class__.roles = Role.objects.filter(user=request.user, organization=role.organization) request.__class__.roles = roles = Role.get_active_roles( request.user) request.__class__.is_super_admin = request.group.name in ( 'Super Admin') # for role in request.roles: # groups.append(role.group) # request.__class__.groups = groups else: request = clear_roles(request) else: request = clear_roles(request)
def add_project_role(request, pk): obj = get_object_or_404( Project, pk=pk) role_obj = UserRole(project=obj) scenario = 'Assign People' form = SetProjectRoleForm(instance=role_obj, request=request) if request.method == 'POST': form = SetProjectRoleForm(data=request.POST, instance=role_obj, request=request) if form.is_valid(): role_obj = form.save(commit=False) user_id = request.POST.get('user') role_obj.user_id = int(user_id) role_obj.save() messages.add_message(request, messages.INFO, '{} Added'.format(role_obj.group.name)) return HttpResponseRedirect(reverse("fieldsight:project-dashboard", kwargs={'pk': obj.pk})) existing_staffs = obj.get_staffs return render(request, "fieldsight/add_central_engineer.html", {'obj':obj,'form':form, 'scenario':scenario, "existing_staffs":existing_staffs})
def add_central_engineer(request, pk): obj = get_object_or_404( Project, pk=pk) group = Group.objects.get(name__exact="Reivewer") role_obj = UserRole(project=obj, group=group) scenario = 'Assign' if request.method == 'POST': form = SetProjectRoleForm(data=request.POST, instance=role_obj, request=request) if form.is_valid(): role_obj = form.save(commit=False) user_id = request.POST.get('user') role_obj.user_id = int(user_id) role_obj.save() messages.add_message(request, messages.INFO, 'Reviewer Added') return HttpResponseRedirect(reverse("fieldsight:project-dashboard", kwargs={'pk': obj.pk})) else: form = SetProjectRoleForm(instance=role_obj, request=request,) return render(request, "fieldsight/add_central_engineer.html", {'obj':obj,'form':form, 'scenario':scenario})
def add_org_admin(request, pk=None): organization = get_object_or_404(Organization, id=pk) group = Group.objects.get(name__exact="Organization Admin") role_obj = UserRole(organization=organization,group=group) scenario = 'Assign' if request.POST: form = AssignOrgAdmin(data=request.POST, instance=role_obj, request=request) if form.is_valid(): role_obj = form.save(commit=False) user_id = request.POST.get('user') role_obj.user_id = int(user_id) role_obj.save() messages.add_message(request, messages.INFO, 'Organization Admin Added') return HttpResponseRedirect(reverse("fieldsight:organization-dashboard", kwargs={'pk': pk})) else: form = AssignOrgAdmin(instance=role_obj, request=request) return render(request, 'fieldsight/add_admin_form.html', {'form': form, 'scenario': scenario, 'obj': organization})
def current_user(request): user = request.user if user.is_anonymous(): return Response({'code': 401, 'message': 'Unauthorized User'}) elif not user.user_profile.organization: return Response({'code': 403, 'message': 'Sorry, you are not assigned to any organization yet. ' 'Please contact your project manager.'}) else: site_supervisor = False field_sight_info = [] roles = UserRole.get_active_site_roles(user) if roles.exists(): site_supervisor = True for role in roles: site = role.site data = site.blueprints.all() bp = [m.image.url for m in data] project = role.project site_info = {'site': {'id': site.id, 'phone': site.phone, 'name': site.name, 'description': site.public_desc, 'address':site.address, 'lat': repr(site.latitude), 'lon': repr(site.longitude), 'identifier':site.identifier, 'progress': site.progress(), 'type_id':site.type.id, 'type_label':site.type.name, 'add_desc': site.additional_desc, 'blueprints':bp, 'site_meta_attributes_ans':site.site_meta_attributes_ans}, 'project': {'name': project.name, 'id': project.id, 'description': project.public_desc, 'address':project.address, 'type_id':project.type.id, 'type_label':project.type.name,'phone':project.phone, 'organization_name':project.organization.name, 'organization_url':project.organization.logo.url, 'lat': repr(project.latitude), 'lon': repr(project.longitude), 'cluster_sites':project.cluster_sites, 'site_meta_attributes':project.site_meta_attributes}, } field_sight_info.append(site_info) users_payload = {'username': user.username, 'full_name': user.first_name, 'email': user.email, 'my_sites': field_sight_info, 'server_time': datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S'), 'is_supervisor': site_supervisor, 'last_login': user.last_login, 'organization': user.user_profile.organization.name, 'organization_url': user.user_profile.organization.logo.url, 'address': user.user_profile.address, 'skype': user.user_profile.skype, 'phone': user.user_profile.phone, 'profile_pic': user.user_profile.profile_picture.url, # 'languages': settings.LANGUAGES, # profile data here, role supervisor } response_data = {'code':200, 'data': users_payload} return Response(response_data)
def process_request(self, request): if request.META.get('HTTP_AUTHORIZATION'): token_key = request.META.get('HTTP_AUTHORIZATION').split(' ')[-1] try: request.user = Token.objects.get(key=token_key).user except: pass if not request.user.is_anonymous(): roles = cache.get('roles_{}'.format(request.user.id)) is_admin = cache.get('admin_{}'.format(request.user.id), False) if roles: request.roles = roles request.is_super_admin = is_admin if not roles: roles = Role.get_active_roles(request.user) if roles: cache.set('roles_{}'.format(request.user.id), roles, 20 * 60) if roles.filter(group__name="Super Admin").exists(): request.is_super_admin = True cache.set('admin_{}'.format(request.user.id), True, 20 * 60) else: request.is_super_admin = False cache.set('admin_{}'.format(request.user.id), False, 20 * 60) request.roles = roles if not roles: print(" user have no roles") logout(request) # return render(request, 'fieldsight/permission_denied.html') else: clear_roles(request)