You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
519 lines
20 KiB
519 lines
20 KiB
from django.conf import settings
|
|
from django.contrib import messages
|
|
from django.contrib.auth.models import Group
|
|
from django.core.mail import send_mail
|
|
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
|
|
from django.core.urlresolvers import reverse
|
|
from django.db.models import Q
|
|
from django.http import HttpResponse, HttpResponseForbidden
|
|
from django.shortcuts import render, get_object_or_404, redirect
|
|
from django.views.generic import ListView, DetailView, View, UpdateView, CreateView
|
|
from pprint import pprint, pformat
|
|
import itertools
|
|
import natsort
|
|
import pydash as _; _.map = _.map_; _.filter = _.filter_
|
|
|
|
from .mixins import CheckForUserMixin
|
|
from .models import User, Team, UserFinancialInfo
|
|
from .forms import TeamForm
|
|
from archilance import util
|
|
from archilance.mixins import BaseMixin
|
|
from projects.forms import PortfolioForm
|
|
from projects.models import Project, Portfolio
|
|
from reviews.models import Review
|
|
from specializations.models import Specialization
|
|
from work_sell.forms import WorkSellForm
|
|
from work_sell.models import WorkSell
|
|
|
|
from .forms import (
|
|
ContractorFilterForm,
|
|
CustomerProfileProjectRealtyForm,
|
|
UserProfileBasicInfoEditForm,
|
|
UserFinancialInfoEditForm,
|
|
UserProfileEditForm,
|
|
)
|
|
|
|
|
|
def send_mail_test(request):
|
|
send_mail('Subject here', 'Here is the message.Mukhtar hello ', '', ['muhtarzubanchi05@gmail.com'], fail_silently=False)
|
|
return HttpResponse("Mail send")
|
|
|
|
|
|
class UserListView(ListView):
|
|
model = User
|
|
template_name = 'users_list.html'
|
|
context_object_name = 'users'
|
|
|
|
|
|
class UserProfileEditView(BaseMixin, View):
|
|
form_class = UserProfileEditForm
|
|
template_name = 'user_profile_edit.html'
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
if request.user.is_authenticated() and request.user.pk == int(kwargs.get('pk')):
|
|
return super().dispatch(request, *args, **kwargs)
|
|
else:
|
|
return HttpResponseForbidden('403 Forbidden')
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
context = self.get_context_data(**_.merge({}, request.GET, kwargs))
|
|
|
|
form = self.form_class(request=request, instance=request.user)
|
|
context.update({'form': form})
|
|
|
|
return render(request, self.template_name, context)
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
context = self.get_context_data(**kwargs)
|
|
|
|
specs = request.POST.getlist('contractor_specializations')
|
|
request.POST.setlist('contractor_specializations', _.compact(specs)) # Ignore empty input values
|
|
|
|
form = self.form_class(request.POST, request.FILES, request=request, instance=request.user)
|
|
|
|
if form.is_valid():
|
|
form.save()
|
|
|
|
messages.info(request, 'Заказчик успешно отредактирован')
|
|
redirect_to = request.POST.get('next')
|
|
return redirect(redirect_to)
|
|
else:
|
|
if form.errors:
|
|
messages.info(request, (
|
|
'<p>Произошла ошибка (form)</p>'
|
|
'<pre>{form}</pre>'
|
|
).format(form=pformat(form.errors)))
|
|
|
|
context.update({'form': form})
|
|
return render(request, self.template_name, context)
|
|
|
|
|
|
class UserFinancialInfoEditView(BaseMixin, View):
|
|
form_class = UserProfileBasicInfoEditForm
|
|
fin_info_form_class = UserFinancialInfoEditForm
|
|
template_name = 'user_financial_info_edit.html'
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
if request.user.is_authenticated() and request.user.pk == int(kwargs.get('pk')):
|
|
return super().dispatch(request, *args, **kwargs)
|
|
else:
|
|
return HttpResponseForbidden('403 Forbidden')
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
context = self.get_context_data(**_.merge({}, request.GET, kwargs))
|
|
|
|
form = self.form_class(request=request, instance=request.user)
|
|
fin_info_form = self.fin_info_form_class(request=request, instance=request.user.financial_info, prefix='fin_info')
|
|
|
|
context.update({
|
|
'form': form,
|
|
'fin_info_form': fin_info_form,
|
|
})
|
|
|
|
return render(request, self.template_name, context)
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
context = self.get_context_data(**kwargs)
|
|
|
|
specs = request.POST.getlist('contractor_specializations')
|
|
request.POST.setlist('contractor_specializations', _.compact(specs)) # Ignore empty input values
|
|
|
|
form = self.form_class(request.POST, request.FILES, request=request, instance=request.user)
|
|
fin_info_form = self.fin_info_form_class(request.POST, request.FILES, request=request, instance=request.user.financial_info, prefix='fin_info')
|
|
|
|
if form.is_valid() and fin_info_form.is_valid():
|
|
user = form.save()
|
|
fin_info = fin_info_form.save()
|
|
|
|
user.financial_info = fin_info
|
|
user.save()
|
|
|
|
messages.info(request, 'Финансовая информация успешно отредактирована')
|
|
redirect_to = request.POST.get('next')
|
|
return redirect(redirect_to)
|
|
else:
|
|
if form.errors:
|
|
messages.info(request, (
|
|
'<p>Произошла ошибка (form)</p>'
|
|
'<pre>{form}</pre>'
|
|
).format(form=pformat(form.errors)))
|
|
|
|
if fin_info_form.errors:
|
|
messages.info(request, (
|
|
'<p>Произошла ошибка (fin_info_form)</p>'
|
|
'<pre>{form}</pre>'
|
|
).format(form=pformat(fin_info_form.errors)))
|
|
|
|
context.update({
|
|
'form': form,
|
|
'fin_info_form': fin_info_form,
|
|
})
|
|
|
|
return render(request, self.template_name, context)
|
|
|
|
|
|
class ContractorFilterView(BaseMixin, View):
|
|
template_name = 'contractor_filter.html'
|
|
form_class = ContractorFilterForm
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
form = self.form_class(request.GET, request=request)
|
|
context = self.get_context_data(**_.merge({}, request.GET, kwargs))
|
|
coll = []
|
|
|
|
if form.is_valid():
|
|
contractors = teams = None
|
|
contr_count = team_count = None
|
|
get_contractors = get_teams = None
|
|
ord = None
|
|
|
|
cro = form.cleaned_data.get('cro')
|
|
specialization = form.cleaned_data.get('specialization')
|
|
location = form.cleaned_data.get('location')
|
|
work_type = form.cleaned_data.get('work_type')
|
|
build_classif = form.cleaned_data.get('building_classification')
|
|
constr_type = form.cleaned_data.get('construction_type')
|
|
|
|
|
|
party_types = form.cleaned_data.get('party_types')
|
|
last_party_types = form.cleaned_data.get('last_party_types')
|
|
|
|
if party_types == 'all':
|
|
get_contractors = get_teams = True
|
|
elif party_types == 'contractors':
|
|
get_contractors = True
|
|
elif party_types == 'teams':
|
|
get_teams = True
|
|
elif not party_types:
|
|
if last_party_types == 'contractors':
|
|
get_contractors = True
|
|
elif last_party_types == 'teams':
|
|
get_teams = True
|
|
else:
|
|
get_contractors = get_teams = True
|
|
|
|
if party_types:
|
|
last_party_types = party_types
|
|
|
|
context.update({'last_party_types': last_party_types})
|
|
|
|
|
|
|
|
if get_contractors:
|
|
contractors = User.contractor_objects.filter(cro=cro)
|
|
|
|
if specialization:
|
|
contractors = contractors.filter(
|
|
contractor_specializations__lft__gte=specialization.lft,
|
|
contractor_specializations__rght__lte=specialization.rght,
|
|
)
|
|
|
|
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)
|
|
|
|
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)
|
|
|
|
|
|
|
|
if get_teams:
|
|
teams = Team.objects.filter(Q(contractors__cro=cro) | Q(owner__cro=cro))
|
|
|
|
if specialization:
|
|
teams = teams.filter(
|
|
(
|
|
Q(contractors__contractor_specializations__lft__gte=specialization.lft)
|
|
& Q(contractors__contractor_specializations__rght__lte=specialization.rght)
|
|
) | (
|
|
Q(owner__contractor_specializations__lft__gte=specialization.lft)
|
|
& Q(owner__contractor_specializations__rght__lte=specialization.rght)
|
|
),
|
|
)
|
|
|
|
if location:
|
|
teams = teams.filter(
|
|
(
|
|
Q(contractors__location__lft__gte=location.lft)
|
|
& Q(contractors__location__rght__lte=location.rght)
|
|
) | (
|
|
Q(owner__location__lft__gte=location.lft)
|
|
& Q(owner__location__rght__lte=location.rght)
|
|
),
|
|
)
|
|
|
|
if work_type:
|
|
teams = teams.filter(
|
|
Q(contractors__orders__project__work_type=work_type)
|
|
| Q(owner__orders__project__work_type=work_type),
|
|
)
|
|
|
|
if build_classif:
|
|
teams = teams.filter(
|
|
Q(contractors__orders__project__realty__building_classification=build_classif)
|
|
| Q(owner__orders__project__realty__building_classification=build_classif),
|
|
)
|
|
|
|
if constr_type:
|
|
teams = teams.filter(
|
|
Q(contractors__orders__project__realty__construction_type=constr_type)
|
|
| Q(owner__orders__project__realty__construction_type=constr_type),
|
|
)
|
|
|
|
if get_contractors and get_teams:
|
|
coll = tuple(itertools.chain(contractors.distinct(), teams.distinct()))
|
|
count = len(coll)
|
|
display_msg = 'Найдено %s элементов' % count if count > 0 else 'Ничего не найдено'
|
|
elif get_contractors:
|
|
coll = contractors.distinct()
|
|
count = coll.count()
|
|
display_msg = 'Найдено %s исполнителей' % count if count > 0 else 'Ничего не найдено'
|
|
elif get_teams:
|
|
coll = teams.distinct()
|
|
count = coll.count()
|
|
display_msg = 'Найдено %s групп' % count if count > 0 else 'Ничего не найдено'
|
|
|
|
|
|
order_by = form.cleaned_data.get('order_by')
|
|
last_order_by = form.cleaned_data.get('last_order_by')
|
|
reverse_order = form.cleaned_data.get('reverse_order')
|
|
|
|
if order_by:
|
|
reverse_order = not reverse_order if order_by == last_order_by else False
|
|
ord = order_by
|
|
last_order_by = ord
|
|
elif last_order_by:
|
|
ord = last_order_by
|
|
|
|
if ord:
|
|
if ord == 'name':
|
|
coll = natsort.natsorted(coll, key=lambda obj: getattr(obj, 'username', None) or obj.name, reverse=reverse_order)
|
|
elif ord =='created':
|
|
coll = natsort.natsorted(coll, key=lambda obj: obj.created, reverse=reverse_order)
|
|
|
|
context.update({
|
|
'last_order_by': last_order_by,
|
|
'reverse_order': reverse_order,
|
|
})
|
|
else:
|
|
display_msg = 'Пожалуйста, введите корректные данные'
|
|
|
|
if form.errors:
|
|
messages.info(request, (
|
|
'<p>Произошла ошибка (form)</p>'
|
|
'<pre>{form}</pre>'
|
|
).format(form=pformat(form.errors)))
|
|
|
|
paginator = Paginator(coll, settings.PAGE_SIZE)
|
|
page = request.GET.get('page')
|
|
|
|
try:
|
|
coll = paginator.page(page)
|
|
except PageNotAnInteger:
|
|
coll = paginator.page(1)
|
|
except EmptyPage:
|
|
coll = paginator.page(paginator.num_pages)
|
|
|
|
context.update({
|
|
'form': form,
|
|
'coll': coll,
|
|
'is_paginated': True,
|
|
'page_obj': coll,
|
|
'display_msg': display_msg,
|
|
})
|
|
|
|
return render(request, self.template_name, context)
|
|
|
|
|
|
class ContractorProfileDetailView(DetailView):
|
|
model = User
|
|
worksell_form_class = WorkSellForm
|
|
portfolio_form_class = PortfolioForm
|
|
template_name = 'contractor_profile.html'
|
|
context_object_name = 'contractor'
|
|
queryset = User.contractor_objects
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context['worksell_form'] = self.worksell_form_class
|
|
context['portfolio_form'] = self.portfolio_form_class
|
|
|
|
resume = self.object.contractor_resume
|
|
|
|
if resume:
|
|
context['resume_diploms'] = resume.resume_files.filter(type='diplom')
|
|
context['resume_cro'] = resume.resume_files.filter(type='cro')
|
|
|
|
return context
|
|
|
|
|
|
class ContractorOfficeDetailView(DetailView):
|
|
model = User
|
|
template_name = 'contractor_office.html'
|
|
context_object_name = 'contractor'
|
|
form_team = TeamForm
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
context['form_team'] = TeamForm
|
|
participants = []
|
|
if self.object.is_owner_team():
|
|
participants = self.object.team.contractors.all()
|
|
user_ids = [p.pk for p in participants]
|
|
context['participants'] = participants
|
|
context['participants_count'] = len(participants)
|
|
portfolios = Portfolio.objects.filter(user__in=user_ids)
|
|
work_sells = WorkSell.objects.filter(contractor__in=user_ids)
|
|
context['portfolios'] = portfolios
|
|
context['work_sells'] = work_sells
|
|
return context
|
|
|
|
|
|
|
|
class CustomerProfileOpenProjectsView(BaseMixin, View):
|
|
template_name = 'customer_profile_open_projects.html'
|
|
form_class = CustomerProfileProjectRealtyForm
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
context = self.get_context_data(**_.merge({}, request.GET, kwargs))
|
|
customer = get_object_or_404(User.customer_objects, pk=kwargs.get('pk'))
|
|
form = self.form_class(request.GET, request=request, customer=customer)
|
|
projects = customer.projects.filter(state='active')
|
|
trashed_projects = customer.projects.filter(state='trashed')
|
|
|
|
if form.is_valid():
|
|
realty = form.cleaned_data.get('realty')
|
|
|
|
if realty:
|
|
projects = projects.filter(realty=realty)
|
|
trashed_projects = trashed_projects.filter(realty=realty)
|
|
else:
|
|
if form.errors:
|
|
messages.info(request, (
|
|
'<p>Произошла ошибка (form)</p>'
|
|
'<pre>{form}</pre>'
|
|
).format(form=pformat(form.errors)))
|
|
|
|
paginator = Paginator(projects, settings.PAGE_SIZE)
|
|
page = request.GET.get('page')
|
|
|
|
try:
|
|
projects = paginator.page(page)
|
|
except PageNotAnInteger:
|
|
projects = paginator.page(1)
|
|
except EmptyPage:
|
|
projects = paginator.page(paginator.num_pages)
|
|
|
|
context.update({
|
|
'form': form,
|
|
|
|
'projects': projects,
|
|
'customer': customer,
|
|
|
|
'open_project_count': projects.paginator.count,
|
|
'trashed_project_count': trashed_projects.count(),
|
|
|
|
'is_paginated': True,
|
|
'page_obj': projects,
|
|
})
|
|
|
|
return render(request, self.template_name, context)
|
|
|
|
|
|
class CustomerProfileTrashedProjectsView(BaseMixin, View):
|
|
template_name = 'customer_profile_trashed_projects.html'
|
|
form_class = CustomerProfileProjectRealtyForm
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
context = self.get_context_data(**_.merge({}, request.GET, kwargs))
|
|
customer = get_object_or_404(User.customer_objects, pk=kwargs.get('pk'))
|
|
form = self.form_class(request.GET, request=request, customer=customer)
|
|
projects = customer.projects.filter(state='trashed')
|
|
open_projects = customer.projects.filter(state='active')
|
|
|
|
if form.is_valid():
|
|
realty = form.cleaned_data.get('realty')
|
|
|
|
if realty:
|
|
projects = projects.filter(realty=realty)
|
|
open_projects = open_projects.filter(realty=realty)
|
|
else:
|
|
if form.errors:
|
|
messages.info(request, (
|
|
'<p>Произошла ошибка (form)</p>'
|
|
'<pre>{form}</pre>'
|
|
).format(form=pformat(form.errors)))
|
|
|
|
paginator = Paginator(projects, settings.PAGE_SIZE)
|
|
page = request.GET.get('page')
|
|
|
|
try:
|
|
projects = paginator.page(page)
|
|
except PageNotAnInteger:
|
|
projects = paginator.page(1)
|
|
except EmptyPage:
|
|
projects = paginator.page(paginator.num_pages)
|
|
|
|
context.update({
|
|
'form': form,
|
|
|
|
'projects': projects,
|
|
'customer': customer,
|
|
|
|
'open_project_count': open_projects.count(),
|
|
'trashed_project_count': projects.paginator.count,
|
|
|
|
'is_paginated': True,
|
|
'page_obj': projects,
|
|
})
|
|
|
|
return render(request, self.template_name, context)
|
|
|
|
|
|
class CustomerProfileCurrentProjectsView(BaseMixin, DetailView):
|
|
model = User
|
|
template_name = 'customer_profile_current_projects.html'
|
|
context_object_name = 'customer'
|
|
|
|
|
|
|
|
class TeamCreateView(View):
|
|
form_class = TeamForm
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
form = self.form_class(request.POST)
|
|
# import code; code.interact(local=dict(globals(), **locals()))
|
|
if form.is_valid():
|
|
instance = form.save(commit=False)
|
|
instance.owner = request.user
|
|
instance.save()
|
|
return redirect('users:contractor-office', pk=request.user.pk)
|
|
|
|
|
|
class CustomerProfileReviewsView(BaseMixin, View):
|
|
template_name = 'customer_profile_reviews.html'
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
context = self.get_context_data(**_.merge({}, self.request.GET, kwargs))
|
|
|
|
customer = get_object_or_404(User.customer_objects, pk=self.kwargs['pk'])
|
|
reviews = Review.objects.filter(target_customer=customer)
|
|
|
|
context.update({
|
|
'reviews': reviews,
|
|
'customer': customer,
|
|
})
|
|
|
|
return render(request, self.template_name, context)
|
|
|
|
|
|
# import code; code.interact(local=dict(globals(), **locals()))
|
|
|