def tasks_by_monitoring_and_organization(request, monitoring_id, organization_id): """ We have 3 generic group: experts, customers, organizations. Superusers: all tasks Experts: only own tasks Customers: all approved tasks organizations: all approved tasks of organizations that belongs to user Also for every ogranization we can have group. """ monitoring = get_object_or_404(Monitoring, pk = monitoring_id) organization = get_object_or_404(Organization, pk = organization_id) user = request.user profile = None if user.is_active: profile = user.profile if not user.has_perm('exmo2010.view_monitoring', monitoring): return HttpResponseForbidden(_('Forbidden')) title = _('Task list for %s') % organization.name queryset = Task.objects.filter(organization = organization) # Or, filtered by user if user.has_perm('exmo2010.admin_monitoring', monitoring): headers = ( (_('organization'), 'organization__name', 'organization__name', None, None), (_('expert'), 'user__username', 'user__username', None, None), (_('status'), 'status', 'status', int, Task.TASK_STATUS), (_('complete, %'), None, None, None, None), (_('openness, %'), None, None, None, None), ) elif profile and profile.is_expert: # Or, without Expert headers = ( (_('organization'), 'organization__name', 'organization__name', None, None), (_('status'), 'status', 'status', int, Task.TASK_STATUS), (_('complete, %'), None, None, None, None), (_('openness, %'), None, None, None, None) ) else: queryset = Task.approved_tasks.all() queryset = queryset.filter(organization = organization) headers = ( (_('organization'), 'organization__name', 'organization__name', None, None), (_('openness, %'), None, None, None, None) ) task_list = [] for task in queryset: if user.has_perm('exmo2010.view_task', task): task_list.append(task.pk) queryset = Task.objects.filter(pk__in = task_list) crumbs = ['Home', 'Monitoring', 'Organization'] breadcrumbs(request, crumbs, monitoring) current_title = _('Organization') return table(request, headers, queryset=queryset, paginate_by=15, extra_context={ 'monitoring': monitoring, 'organization': organization, 'current_title': current_title, 'title': title, }, template_name="task_list.html",)
def monitoring_by_experts(request, monitoring_pk): """ Статистика мониторинга по экспертам. """ monitoring = get_object_or_404(Monitoring, pk=monitoring_pk) if not request.user.has_perm('exmo2010.admin_monitoring', monitoring): raise PermissionDenied experts = Task.objects.filter(organization__monitoring=monitoring).values('user').annotate(cuser=Count('user')) epk = [e['user'] for e in experts] org_list = "( %s )" % " ,".join([str(o.pk) for o in Organization.objects.filter(monitoring=monitoring)]) queryset = User.objects.filter(pk__in=epk).extra(select={ 'open_tasks': 'select count(*) from %(task_table)s where %(task_table)s.user_id = %(user_table)s.id and status = %(status)s and %(task_table)s.organization_id in %(org_list)s' % { 'task_table': Task._meta.db_table, 'user_table': User._meta.db_table, 'org_list': org_list, 'status': Task.TASK_OPEN }, 'ready_tasks': 'select count(*) from %(task_table)s where %(task_table)s.user_id = %(user_table)s.id and status = %(status)s and %(task_table)s.organization_id in %(org_list)s' % { 'task_table': Task._meta.db_table, 'user_table': User._meta.db_table, 'org_list': org_list, 'status': Task.TASK_READY }, 'approved_tasks': 'select count(*) from %(task_table)s where %(task_table)s.user_id = %(user_table)s.id and status = %(status)s and %(task_table)s.organization_id in %(org_list)s' % { 'task_table': Task._meta.db_table, 'user_table': User._meta.db_table, 'org_list': org_list, 'status': Task.TASK_APPROVED }, 'all_tasks': 'select count(*) from %(task_table)s where %(task_table)s.user_id = %(user_table)s.id and %(task_table)s.organization_id in %(org_list)s' % { 'task_table': Task._meta.db_table, 'user_table': User._meta.db_table, 'org_list': org_list, }, }) headers = ( (_('Expert'), 'username', 'username', None, None), (_('Open tasks'), 'open_tasks', None, None, None), (_('Ready tasks'), 'ready_tasks', None, None, None), (_('Approved tasks'), 'approved_tasks', None, None, None), (_('All tasks'), 'all_tasks', None, None, None), ) return table( request, headers, queryset=queryset, paginate_by=15, extra_context={'monitoring': annotate_exmo_perms(monitoring, request.user)}, template_name="manage_monitoring/expert_list.html", )
def tasks_by_monitoring(request, monitoring_pk): user = request.user monitoring = get_object_or_404(Monitoring, pk=monitoring_pk) if not user.is_expert or not user.has_perm('exmo2010.view_monitoring', monitoring): raise PermissionDenied title = _('Task list for %(monitoring)s') % {'monitoring': monitoring} headers = [ (_('organization'), 'organization__name', 'organization__name', None, None), (_('status'), 'status', 'status', int, Task.TASK_STATUS), (_('complete, %'), None, None, None, None), ] if user.is_expertA: users = User.objects.filter( task__organization__monitoring=monitoring).distinct() user_choice = [(u.username, u.profile.legal_name) for u in users] headers.insert(1, (_('expert'), 'user__username', 'user__username', None, user_choice)) else: filter1 = request.GET.get('filter1') if filter1: try: int(filter1) except ValueError: request.GET = QueryDict('') tasks = Task.objects.filter( organization__monitoring=monitoring).select_related( 'user__userprofile', 'organization') # TODO: use queryform instead of table(). return table( request, headers, queryset=perm_filter(request.user, 'view_task', tasks).prefetch_related('qanswer_set'), paginate_by=50, extra_context={ 'monitoring': annotate_exmo_perms(monitoring, request.user), 'title': title, 'invcodeform': SettingsInvCodeForm(), }, template_name="manage_monitoring/tasks.html", )
def tasks_by_monitoring(request, monitoring_pk): user = request.user monitoring = get_object_or_404(Monitoring, pk=monitoring_pk) if not user.is_expert or not user.has_perm('exmo2010.view_monitoring', monitoring): raise PermissionDenied title = _('Task list for %(monitoring)s') % {'monitoring': monitoring} headers = [ (_('organization'), 'organization__name', 'organization__name', None, None), (_('status'), 'status', 'status', int, Task.TASK_STATUS), (_('complete, %'), None, None, None, None), ] if user.is_expertA: users = User.objects.filter(task__organization__monitoring=monitoring).distinct() user_choice = [(u.username, u.profile.legal_name) for u in users] headers.insert(1, (_('expert'), 'user__username', 'user__username', None, user_choice)) else: filter1 = request.GET.get('filter1') if filter1: try: int(filter1) except ValueError: request.GET = QueryDict('') tasks = Task.objects.filter(organization__monitoring=monitoring).select_related('user__userprofile', 'organization') # TODO: use queryform instead of table(). return table( request, headers, queryset=perm_filter(request.user, 'view_task', tasks).prefetch_related('qanswer_set'), paginate_by=50, extra_context={ 'monitoring': annotate_exmo_perms(monitoring, request.user), 'title': title, 'invcodeform': SettingsInvCodeForm(), }, template_name="manage_monitoring/tasks.html", )
def organization_list(request, monitoring_id): name_filter = invite_filter = None alert = request.GET.get('alert', False) if request.method == "GET": name_filter = request.GET.get('name_filter', False) invite_filter = request.GET.get('invite_filter', False) monitoring = get_object_or_404(Monitoring, pk=monitoring_id) if not request.user.has_perm('exmo2010.view_monitoring', monitoring): return HttpResponseForbidden(_('Forbidden')) title = _('Organizations for monitoring %s') % monitoring orgs = Organization.objects.filter(monitoring=monitoring) sent = orgs.exclude(inv_status='NTS') initial = {'monitoring': monitoring} org_type = 'all' if request.method == "POST" and "submit_invite" in request.POST: inv_form = InviteOrgsForm(request.POST) comment = inv_form.data['comment'] inv_status = inv_form.data['inv_status'] if inv_form.is_valid(): inv_form.save() if inv_status != 'ALL': orgs = orgs.filter(inv_status=inv_status) for org in orgs: subject = _('Email Subject') message = comment.replace('%code%', org.inv_code) context = { 'subject': subject, 'message': message } if org.email: emails = filter(None, org.email.split(', ')) else: continue task_id = send_email.delay(emails, subject, 'organizations/invitation_email', context=context, mdn=True) task = EmailTasks() task.task_id = task_id task.organization = org task.save() redirect = reverse('exmo2010:organization_list', args=[monitoring_id])+"?alert=success" return HttpResponseRedirect(redirect) else: initial.update({'comment': comment, 'inv_status': inv_status}) alert = 'fail' inv_form = InviteOrgsForm(initial=initial) if request.user.has_perm('exmo2010.admin_monitoring', monitoring): queryset = Organization.objects.filter(monitoring=monitoring).extra( select={ 'task__count': 'SELECT count(*) FROM %s WHERE organization_id = %s.id' % ( Task._meta.db_table, Organization._meta.db_table, ), } ) headers = ( (_('organization'), 'name', None, None, None), (_('email'), 'email', None, None, None), (_('phone'), 'phone', None, None, None), (_('invitation code'), 'inv_code', None, None, None), (_('tasks'), 'task__count', None, None, None), (_('invitation'), 'inv_status', None, None, None), ) else: org_list = [] for task in Task.objects.filter(organization__monitoring=monitoring).select_related(): if request.user.has_perm('exmo2010.view_task', task): org_list.append(task.organization.pk) org_list = list(set(org_list)) if not org_list: return HttpResponseForbidden(_('Forbidden')) queryset = Organization.objects.filter(pk__in=org_list) headers = ( (_('organization'), 'name', None, None, None), (_('email'), 'email', None, None, None), (_('phone'), 'phone', None, None, None), (_('invitation code'), 'inv_code', None, None, None), (_('invitation'), 'inv_status', None, None, None), ) if not sent: headers_list = list(headers) headers_list.pop() headers = tuple(headers_list) if name_filter: queryset = queryset.filter(name__icontains=name_filter) if invite_filter and invite_filter != 'ALL': queryset = queryset.filter(inv_status=invite_filter) crumbs = ['Home', 'Monitoring'] breadcrumbs(request, crumbs) if request.expert: current_title = _('Monitoring cycle') else: current_title = _('Rating') if monitoring.status == 5 else _('Tasks') initial = {'monitoring': monitoring} form = OrganizationForm(initial=initial) if request.method == "POST" and "submit_add" in request.POST: form = OrganizationForm(request.POST) if form.is_valid(): form.save() else: org_type = 'add' inv_history = InviteOrgs.objects.filter(monitoring=monitoring) date_filter_history = None invite_filter_history = None if request.method == "GET": date_filter_history = request.GET.get('date_filter_history', False) invite_filter_history = request.GET.get('invite_filter_history', False) if date_filter_history: start_datetime = datetime.strptime("%s 00:00:00" % date_filter_history, '%d.%m.%Y %H:%M:%S') finish_datetime = datetime.strptime("%s 23:59:59" % date_filter_history, '%d.%m.%Y %H:%M:%S') inv_history = inv_history.filter(timestamp__gt=start_datetime, timestamp__lt=finish_datetime) if invite_filter_history and invite_filter_history != 'ALL': inv_history = inv_history.filter(inv_status=invite_filter_history) return table( request, headers, queryset=queryset, paginate_by=100, extra_context={ 'current_title': current_title, 'title': title, 'sent': sent, 'inv_form': inv_form, 'alert': alert, 'org_type': org_type, 'inv_status': INV_STATUS, 'monitoring': monitoring, 'invcodeform': SettingsInvCodeForm(), 'form': form, 'inv_history': inv_history, 'date_filter_history': date_filter_history, 'invite_filter_history': invite_filter_history, }, )
def tasks_by_monitoring(request, monitoring_pk): monitoring = get_object_or_404(Monitoring, pk=monitoring_pk) profile = None if request.user.is_active: profile = request.user.profile if not request.user.has_perm('exmo2010.view_monitoring', monitoring): return HttpResponseForbidden(_('Forbidden')) title = _('Task list for %(monitoring)s') % {'monitoring': monitoring} task_list = [] queryset = Task.objects.filter(organization__monitoring=monitoring).\ select_related() for task in queryset: if request.user.has_perm('exmo2010.view_task', task): task_list.append(task.pk) if not task_list and not \ request.user.has_perm('exmo2010.admin_monitoring', monitoring): return HttpResponseForbidden(_('Forbidden')) queryset = Task.objects.filter(pk__in=task_list).extra( select={'complete_sql': Task.complete_sql_extra()}).select_related() if request.user.has_perm('exmo2010.admin_monitoring', monitoring): users = User.objects.filter(task__organization__monitoring = monitoring).distinct() UserChoice = [(u.username, u.profile.legal_name) for u in users] headers = ( (_('organization'), 'organization__name', 'organization__name', None, None), (_('expert'), 'user__username', 'user__username', None, UserChoice), (_('status'), 'status', 'status', int, Task.TASK_STATUS), (_('complete, %'), None, None, None, None), ) elif profile and profile.is_expertB and not profile.is_expertA: filter1 = request.GET.get('filter1') if filter1: try: int(filter1) except ValueError: q = QueryDict('') request.GET = q headers = ( (_('organization'), 'organization__name', 'organization__name', None, None), (_('status'), 'status', 'status', int, Task.TASK_STATUS), (_('complete, %'), None, None, None, None), ) elif profile and profile.is_expert: headers = ( (_('organization'), 'organization__name', 'organization__name', None, None), (_('status'), 'status', 'status', int, Task.TASK_STATUS), (_('complete, %'), None, None, None, None), ) else: headers = ( (_('organization'), 'organization__name', 'organization__name', None, None), ) return table( request, headers, queryset = queryset, paginate_by = 50, extra_context = { 'monitoring': monitoring, 'title': title, 'invcodeform': SettingsInvCodeForm(), }, template_name="task_list.html", )
def tasks_by_monitoring(request, monitorgin_id): monitoring = get_object_or_404(Monitoring, pk=monitorgin_id) profile = None if request.user.is_active: profile = request.user.profile if not request.user.has_perm('exmo2010.view_monitoring', monitoring): return HttpResponseForbidden(_('Forbidden')) title = _('Task list for %(monitoring)s') % {'monitoring': monitoring} task_list = [] queryset = Task.objects.filter(organization__monitoring=monitoring).\ select_related() for task in queryset: if request.user.has_perm('exmo2010.view_task', task): task_list.append(task.pk) if not task_list and not \ request.user.has_perm('exmo2010.admin_monitoring', monitoring): return HttpResponseForbidden(_('Forbidden')) queryset = Task.objects.filter(pk__in=task_list) if request.user.has_perm('exmo2010.admin_monitoring', monitoring): users = User.objects.filter(task__organization__monitoring = monitoring).distinct() UserChoice = [(u.username, u.profile.legal_name) for u in users] headers = ( (_('organization'), 'organization__name', 'organization__name', None, None), (_('expert'), 'user__username', 'user__username', None, UserChoice), (_('status'), 'status', 'status', int, Task.TASK_STATUS), (_('complete, %'), None, None, None, None), ) elif profile and profile.is_expert: headers = ( (_('organization'), 'organization__name', 'organization__name', None, None), (_('status'), 'status', 'status', int, Task.TASK_STATUS), (_('complete, %'), None, None, None, None), ) else: headers = ( (_('organization'), 'organization__name', 'organization__name', None, None), ) crumbs = ['Home', 'Monitoring'] breadcrumbs(request, crumbs) if request.expert: current_title = _('Monitoring cycle') else: current_title = _('Rating') if monitoring.status == 5 else _('Tasks') return table( request, headers, queryset = queryset, paginate_by = 50, extra_context = { 'monitoring': monitoring, 'current_title': current_title, 'title': title, 'invcodeform': SettingsInvCodeForm(), }, template_name="task_list.html", )
def monitoring_by_experts(request, monitoring_pk): """ Статистика мониторинга по экспертам. """ monitoring = get_object_or_404(Monitoring, pk=monitoring_pk) if not request.user.has_perm('exmo2010.admin_monitoring', monitoring): raise PermissionDenied experts = Task.objects.filter( organization__monitoring=monitoring).values('user').annotate( cuser=Count('user')) epk = [e['user'] for e in experts] org_list = "( %s )" % " ,".join([ str(o.pk) for o in Organization.objects.filter(monitoring=monitoring) ]) queryset = User.objects.filter(pk__in=epk).extra( select={ 'open_tasks': 'select count(*) from %(task_table)s where %(task_table)s.user_id = %(user_table)s.id and status = %(status)s and %(task_table)s.organization_id in %(org_list)s' % { 'task_table': Task._meta.db_table, 'user_table': User._meta.db_table, 'org_list': org_list, 'status': Task.TASK_OPEN }, 'ready_tasks': 'select count(*) from %(task_table)s where %(task_table)s.user_id = %(user_table)s.id and status = %(status)s and %(task_table)s.organization_id in %(org_list)s' % { 'task_table': Task._meta.db_table, 'user_table': User._meta.db_table, 'org_list': org_list, 'status': Task.TASK_READY }, 'approved_tasks': 'select count(*) from %(task_table)s where %(task_table)s.user_id = %(user_table)s.id and status = %(status)s and %(task_table)s.organization_id in %(org_list)s' % { 'task_table': Task._meta.db_table, 'user_table': User._meta.db_table, 'org_list': org_list, 'status': Task.TASK_APPROVED }, 'all_tasks': 'select count(*) from %(task_table)s where %(task_table)s.user_id = %(user_table)s.id and %(task_table)s.organization_id in %(org_list)s' % { 'task_table': Task._meta.db_table, 'user_table': User._meta.db_table, 'org_list': org_list, }, }) headers = ( (_('Expert'), 'username', 'username', None, None), (_('Open tasks'), 'open_tasks', None, None, None), (_('Ready tasks'), 'ready_tasks', None, None, None), (_('Approved tasks'), 'approved_tasks', None, None, None), (_('All tasks'), 'all_tasks', None, None, None), ) return table( request, headers, queryset=queryset, paginate_by=15, extra_context={ 'monitoring': annotate_exmo_perms(monitoring, request.user) }, template_name="manage_monitoring/expert_list.html", )