|
|
|
@ -8,11 +8,13 @@ from django.http import HttpResponse |
|
|
|
from django.shortcuts import render, get_object_or_404, redirect |
|
|
|
from django.shortcuts import render, get_object_or_404, redirect |
|
|
|
from django.views.generic import ListView, DetailView, View, UpdateView, CreateView |
|
|
|
from django.views.generic import ListView, DetailView, View, UpdateView, CreateView |
|
|
|
from pprint import pprint, pformat |
|
|
|
from pprint import pprint, pformat |
|
|
|
|
|
|
|
import itertools |
|
|
|
|
|
|
|
import natsort |
|
|
|
import pydash as _; _.map = _.map_; _.filter = _.filter_ |
|
|
|
import pydash as _; _.map = _.map_; _.filter = _.filter_ |
|
|
|
|
|
|
|
|
|
|
|
from .forms import UserEditForm, ContractorFilterForm, ContractorFinancicalInfoForm |
|
|
|
from .forms import UserEditForm, ContractorFilterForm, ContractorFinancicalInfoForm |
|
|
|
from .mixins import CheckForUserMixin |
|
|
|
from .mixins import CheckForUserMixin |
|
|
|
from .models import User, ContractorFinancialInfo |
|
|
|
from .models import User, Team, ContractorFinancialInfo |
|
|
|
from archilance.mixins import BaseMixin |
|
|
|
from archilance.mixins import BaseMixin |
|
|
|
from common.utils import get_or_none |
|
|
|
from common.utils import get_or_none |
|
|
|
from projects.forms import PortfolioForm |
|
|
|
from projects.forms import PortfolioForm |
|
|
|
@ -38,8 +40,7 @@ class ContractorFilterView(BaseMixin, View): |
|
|
|
def get(self, request, *args, **kwargs): |
|
|
|
def get(self, request, *args, **kwargs): |
|
|
|
form = self.form_class(request.GET, request=request) |
|
|
|
form = self.form_class(request.GET, request=request) |
|
|
|
context = self.get_context_data(**_.merge({}, request.GET, kwargs)) |
|
|
|
context = self.get_context_data(**_.merge({}, request.GET, kwargs)) |
|
|
|
|
|
|
|
contractors = teams = None |
|
|
|
contractors = User.contractor_objects |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if form.is_valid(): |
|
|
|
if form.is_valid(): |
|
|
|
cro = form.cleaned_data.get('cro') |
|
|
|
cro = form.cleaned_data.get('cro') |
|
|
|
@ -48,56 +49,53 @@ class ContractorFilterView(BaseMixin, View): |
|
|
|
work_type = form.cleaned_data.get('work_type') |
|
|
|
work_type = form.cleaned_data.get('work_type') |
|
|
|
build_classif = form.cleaned_data.get('building_classification') |
|
|
|
build_classif = form.cleaned_data.get('building_classification') |
|
|
|
constr_type = form.cleaned_data.get('construction_type') |
|
|
|
constr_type = form.cleaned_data.get('construction_type') |
|
|
|
|
|
|
|
party_types = form.cleaned_data.get('party_types') |
|
|
|
|
|
|
|
|
|
|
|
contractors = contractors.filter(cro=cro) |
|
|
|
get_contractors = get_teams = None |
|
|
|
|
|
|
|
|
|
|
|
if specialization: |
|
|
|
|
|
|
|
contractors = contractors.filter( |
|
|
|
|
|
|
|
# specialization__lft__gte=specialization.lft, |
|
|
|
|
|
|
|
# specialization__rght__lte=specialization.rght, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
contractor_specializations=specialization, # TODO: Honor the hierarchical data structure |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if location: |
|
|
|
|
|
|
|
contractors = contractors.filter( |
|
|
|
|
|
|
|
location__lft__gte=location.lft, |
|
|
|
|
|
|
|
location__rght__lte=location.rght, |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if work_type: |
|
|
|
if not party_types: |
|
|
|
contractors = contractors.filter(orders__project__work_type=work_type).distinct() # TODO: OK? |
|
|
|
get_contractors = get_teams = True |
|
|
|
|
|
|
|
elif party_types == 'contractors': |
|
|
|
|
|
|
|
get_contractors = True |
|
|
|
|
|
|
|
elif party_types == 'teams': |
|
|
|
|
|
|
|
get_teams = True |
|
|
|
|
|
|
|
|
|
|
|
if build_classif: |
|
|
|
if get_contractors: |
|
|
|
contractors = contractors.filter(orders__project__realty__building_classification=build_classif) |
|
|
|
contractors = User.contractor_objects.filter(cro=cro) |
|
|
|
|
|
|
|
|
|
|
|
if constr_type: |
|
|
|
if specialization: |
|
|
|
contractors = contractors.filter(orders__project__realty__construction_type=constr_type) |
|
|
|
contractors = contractors.filter( |
|
|
|
|
|
|
|
# specialization__lft__gte=specialization.lft, |
|
|
|
# import code; code.interact(local=dict(globals(), **locals())) |
|
|
|
# specialization__rght__lte=specialization.rght, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
contractor_specializations=specialization, # TODO: Honor the hierarchical data structure |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if location: |
|
|
|
|
|
|
|
contractors = contractors.filter( |
|
|
|
|
|
|
|
location__lft__gte=location.lft, |
|
|
|
|
|
|
|
location__rght__lte=location.rght, |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if work_type: |
|
|
|
|
|
|
|
contractors = contractors.filter(orders__project__work_type=work_type).distinct() # TODO: OK? |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if build_classif: |
|
|
|
|
|
|
|
contractors = contractors.filter(orders__project__realty__building_classification=build_classif) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if constr_type: |
|
|
|
|
|
|
|
contractors = contractors.filter(orders__project__realty__construction_type=constr_type) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# contr_count = contractors.count() |
|
|
|
|
|
|
|
# display_msg = 'Найдено %s исполнителей' % contr_count if contr_count > 0 else 'Ничего не найдено' |
|
|
|
|
|
|
|
|
|
|
|
# order_by = form.cleaned_data.get('order_by') |
|
|
|
if get_teams: |
|
|
|
# last_order_by = form.cleaned_data.get('last_order_by') |
|
|
|
teams = Team.objects.all() |
|
|
|
# reverse_order = form.cleaned_data.get('reverse_order') |
|
|
|
|
|
|
|
# |
|
|
|
# team_count = teams.count() |
|
|
|
# if order_by: |
|
|
|
# display_msg = 'Найдено %s команд' % team_count if team_count > 0 else 'Ничего не найдено' |
|
|
|
# if order_by == last_order_by: |
|
|
|
|
|
|
|
# reverse_order = not reverse_order |
|
|
|
|
|
|
|
# else: |
|
|
|
|
|
|
|
# reverse_order = False |
|
|
|
|
|
|
|
# |
|
|
|
|
|
|
|
# projects = projects.order_by('-%s' % order_by if reverse_order else order_by) |
|
|
|
|
|
|
|
# last_order_by = order_by |
|
|
|
|
|
|
|
# elif last_order_by: |
|
|
|
|
|
|
|
# projects = projects.order_by('-%s' % last_order_by if reverse_order else last_order_by) |
|
|
|
|
|
|
|
# |
|
|
|
|
|
|
|
# context.update({ |
|
|
|
|
|
|
|
# 'last_order_by': last_order_by, |
|
|
|
|
|
|
|
# 'reverse_order': reverse_order, |
|
|
|
|
|
|
|
# }) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
contr_count = contractors.count() |
|
|
|
display_msg = 'Результаты поиска' |
|
|
|
display_msg = 'Найдено %s исполнителей' % contr_count if contr_count > 0 else 'Ничего не найдено' |
|
|
|
|
|
|
|
else: |
|
|
|
else: |
|
|
|
display_msg = 'Пожалуйста, введите корректные данные' |
|
|
|
display_msg = 'Пожалуйста, введите корректные данные' |
|
|
|
|
|
|
|
|
|
|
|
@ -107,22 +105,24 @@ class ContractorFilterView(BaseMixin, View): |
|
|
|
'<pre>{form}</pre>' |
|
|
|
'<pre>{form}</pre>' |
|
|
|
).format(form=pformat(form.errors))) |
|
|
|
).format(form=pformat(form.errors))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
coll = tuple(itertools.chain(contractors, teams)) |
|
|
|
|
|
|
|
coll = natsort.natsorted(coll, key=lambda obj: getattr(obj, 'username', None) or getattr(obj, 'name')) |
|
|
|
|
|
|
|
|
|
|
|
paginator = Paginator(contractors.all(), settings.PAGE_SIZE) |
|
|
|
paginator = Paginator(coll, settings.PAGE_SIZE) |
|
|
|
page = request.GET.get('page') |
|
|
|
page = request.GET.get('page') |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
|
try: |
|
|
|
contractors = paginator.page(page) |
|
|
|
coll = paginator.page(page) |
|
|
|
except PageNotAnInteger: |
|
|
|
except PageNotAnInteger: |
|
|
|
contractors = paginator.page(1) |
|
|
|
coll = paginator.page(1) |
|
|
|
except EmptyPage: |
|
|
|
except EmptyPage: |
|
|
|
contractors = paginator.page(paginator.num_pages) |
|
|
|
coll = paginator.page(paginator.num_pages) |
|
|
|
|
|
|
|
|
|
|
|
context.update({ |
|
|
|
context.update({ |
|
|
|
'form': form, |
|
|
|
'form': form, |
|
|
|
'contractors': contractors, |
|
|
|
'coll': coll, |
|
|
|
'is_paginated': True, |
|
|
|
'is_paginated': True, |
|
|
|
'page_obj': contractors, |
|
|
|
'page_obj': coll, |
|
|
|
'display_msg': display_msg, |
|
|
|
'display_msg': display_msg, |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|