diff --git a/api/views.py b/api/views.py index e629711..9e7d7a4 100755 --- a/api/views.py +++ b/api/views.py @@ -20,37 +20,38 @@ from chat.models import Message from chat.serializers import MessageSerializer from chat.filters import MessageFilterSet + class ProjectViewSet(ModelViewSet): - queryset = Project.objects.all() - serializer_class = ProjectSerializer - filter_class = ProjectFilterSet + queryset = Project.objects.all() + serializer_class = ProjectSerializer + filter_class = ProjectFilterSet class MessageViewSet(ModelViewSet): - queryset = Message.objects.all() - serializer_class = MessageSerializer - filter_class = MessageFilterSet + queryset = Message.objects.all() + serializer_class = MessageSerializer + filter_class = MessageFilterSet class RealtyViewSet(ModelViewSet): - queryset = Realty.objects.all() - serializer_class = RealtySerializer - filter_class = RealtyFilterSet + queryset = Realty.objects.all() + serializer_class = RealtySerializer + filter_class = RealtyFilterSet class SpecializationViewSet(ModelViewSet): - queryset = Specialization.objects.all() - serializer_class = SpecializationSerializer - filter_class = SpecializationFilterSet - + queryset = Specialization.objects.root_nodes()[0].get_descendants() + serializer_class = SpecializationSerializer + filter_class = SpecializationFilterSet + class UserViewSet(ModelViewSet): - queryset = User.objects.all() - serializer_class = UserSerializer - filter_class = UserFilterSet + queryset = User.objects.all() + serializer_class = UserSerializer + filter_class = UserFilterSet class LocationViewSet(ModelViewSet): - queryset = Location.objects.all() - serializer_class = LocationSerializer - filter_class = LocationFilterSet + queryset = Location.objects.root_nodes()[0].get_descendants() + serializer_class = LocationSerializer + filter_class = LocationFilterSet diff --git a/assets/index.js b/assets/index.js index 4e1eb4d..ae1aa22 100644 --- a/assets/index.js +++ b/assets/index.js @@ -1,89 +1,126 @@ // Fancy widget initializations ------------------------ -$(function() { - var $specSelects = $('.-spec-select') +var $specSelects = $('.-spec-select') + +var select2Options = { + language: 'ru', + //minimumInputLength: 1, // Commented out to immediately load remote data + placeholder: '', // Required by `allowClear` + allowClear: true, - var select2Options = { - language: 'ru', - //minimumInputLength: 1, // Commented out to immediately load remote data - placeholder: '', // Required by `allowClear` - allowClear: true, + ajax: { + url: '/api/specializations/', + dataType: 'json', + quietMillis: 250, + cache: true, - ajax: { - url: '/api/specializations/', - dataType: 'json', - quietMillis: 250, - cache: true, - - data: function(term, page) { - return { - 'name__icontains': term, - page: page, - } - }, - - results: function(data, page) { - return { - results: _.map(function(item) { - return { - text: item.name, - id: item.id, - origItem: item, - } - }, data.results), - - more: (page * 10) < data.count, - } - }, + data: function(term, page) { + return { + 'name__icontains': term, + page: page, + } }, - initSelection: function(element, callback) { - var id = $(element).val() - - if (id !== '') { - $.ajax('/api/specializations/' + id, {dataType: 'json'}) - .then(function(data) {callback(data)}) + results: function(data, page) { + return { + results: _.map(function(item) { + return { + text: item.name, + id: item.id, + origItem: item, + } + }, data.results), + + more: (page * 10) < data.count, } + }, + }, + + initSelection: function(element, callback) { + var id = $(element).val() + + if (id !== '') { + $.ajax('/api/specializations/' + id + '/', {dataType: 'json'}) + .then(function(data) {callback(data)}) } } - - $specSelects.select2(select2Options) - - var $specSelect1 = $('.-spec-select-level-1') - var $specSelect2 = $('.-spec-select-level-2') - var $specSelect3 = $('.-spec-select-level-3') - var $specSelect4 = $('.-spec-select-level-4') - - $specSelects.on('change', function($evt) { - getSpecializationTree($evt.added.origItem).then(function(specs) { - $specSelect1.select2('data', specs.specLevel1 ? {id: specs.specLevel1.id, text: specs.specLevel1.name, origItem: specs.specLevel1} : null) - $specSelect2.select2('data', specs.specLevel2 ? {id: specs.specLevel2.id, text: specs.specLevel2.name, origItem: specs.specLevel2} : null) - $specSelect3.select2('data', specs.specLevel3 ? {id: specs.specLevel3.id, text: specs.specLevel3.name, origItem: specs.specLevel3} : null) - $specSelect4.select2('data', specs.specLevel4 ? {id: specs.specLevel4.id, text: specs.specLevel4.name, origItem: specs.specLevel4} : null) +} + +$specSelects.select2(select2Options) + +var $specSelect1 = $('.-spec-select-level-1') +var $specSelect2 = $('.-spec-select-level-2') +var $specSelect3 = $('.-spec-select-level-3') +var $specSelect4 = $('.-spec-select-level-4') + + +var chosenSpecId = $('#chosenSpecId').val() + +if (chosenSpecId) { + $.ajax({url: '/api/specializations/' + chosenSpecId + '/', method: 'GET'}) + .then(function(spec) { + return updateSpecializationWidgets(spec) }) - }) -}) +} +$specSelects.on('change', function($evt) { + updateSpecializationWidgets($evt.added.origItem) +}) +function updateSpecializationWidgets(spec) { + return getSpecializationTree(spec).then(function(specs) { + var specLevel1 = specs.specLevel1 + var specLevel2 = specs.specLevel2 + var specLevel3 = specs.specLevel3 + var specLevel4 = specs.specLevel4 + var chosenSpecId = specs.chosenSpecId + + $specSelect1.select2('data', specLevel1 ? {id: specLevel1.id, text: specLevel1.name, origItem: specLevel1} : null) + $specSelect2.select2('data', specLevel2 ? {id: specLevel2.id, text: specLevel2.name, origItem: specLevel2} : null) + $specSelect3.select2('data', specLevel3 ? {id: specLevel3.id, text: specLevel3.name, origItem: specLevel3} : null) + $specSelect4.select2('data', specLevel4 ? {id: specLevel4.id, text: specLevel4.name, origItem: specLevel4} : null) + + $('#chosenSpecId').val(chosenSpecId) + }) +} function getSpecializationTree(spec) { var specLevel1, specLevel2, specLevel3, specLevel4 specLevel1 = specLevel2 = specLevel3 = specLevel4 = null + var chosenSpecId = '' + if (spec.level === 1) { specLevel1 = spec - return $.when({specLevel1: specLevel1, specLevel2: specLevel2, specLevel3: specLevel3, specLevel4: specLevel4}) + chosenSpecId = spec.id + + return $.when({ + specLevel1: specLevel1, + specLevel2: specLevel2, + specLevel3: specLevel3, + specLevel4: specLevel4, + chosenSpecId: chosenSpecId, + }) } else if (spec.level === 2) { specLevel2 = spec + chosenSpecId = spec.id return $.ajax({url: '/api/specializations/' + specLevel2.parent.id + '/', method: 'GET'}) .then(function(spec) { specLevel1 = spec - return {specLevel1: specLevel1, specLevel2: specLevel2, specLevel3: specLevel3, specLevel4: specLevel4} + + return { + specLevel1: specLevel1, + specLevel2: specLevel2, + specLevel3: specLevel3, + specLevel4: specLevel4, + chosenSpecId: chosenSpecId, + } }) } else if (spec.level === 3) { specLevel3 = spec + chosenSpecId = spec.id return $.ajax({url: '/api/specializations/' + specLevel3.parent.id + '/', method: 'GET'}) .then(function(spec) { @@ -92,11 +129,19 @@ function getSpecializationTree(spec) { return $.ajax({url: '/api/specializations/' + specLevel2.parent.id + '/', method: 'GET'}) .then(function(spec) { specLevel1 = spec - return {specLevel1: specLevel1, specLevel2: specLevel2, specLevel3: specLevel3, specLevel4: specLevel4} + + return { + specLevel1: specLevel1, + specLevel2: specLevel2, + specLevel3: specLevel3, + specLevel4: specLevel4, + chosenSpecId: chosenSpecId, + } }) }) } else if (spec.level === 4) { specLevel4 = spec + chosenSpecId = spec.id return $.ajax({url: '/api/specializations/' + specLevel4.parent.id + '/', method: 'GET'}) .then(function(spec) { @@ -109,7 +154,14 @@ function getSpecializationTree(spec) { return $.ajax({url: '/api/specializations/' + specLevel2.parent.id + '/', method: 'GET'}) .then(function(spec) { specLevel1 = spec - return {specLevel1: specLevel1, specLevel2: specLevel2, specLevel3: specLevel3, specLevel4: specLevel4} + + return { + specLevel1: specLevel1, + specLevel2: specLevel2, + specLevel3: specLevel3, + specLevel4: specLevel4, + chosenSpecId: chosenSpecId, + } }) }) }) diff --git a/projects/forms.py b/projects/forms.py index 9e508ab..8e978dd 100644 --- a/projects/forms.py +++ b/projects/forms.py @@ -4,6 +4,7 @@ from django.forms.models import inlineformset_factory from mptt.forms import TreeNodeChoiceField from .models import Project, ProjectFile, Portfolio, Answer, Realty, PortfolioPhoto, Stage, Specialization +from common.models import Location from users.models import User @@ -52,7 +53,7 @@ class CustomerProjectEditForm(forms.ModelForm): self.fields['realty'].empty_label = 'Создать новый' - self.fields['specialization'].queryset = Specialization.objects.exclude(id=1) + self.fields['specialization'].queryset = Specialization.objects.root_nodes()[0].get_descendants() self.fields['specialization'].widget.attrs = {'class': '-spec-select', 'style': 'width: 100%'} if self.instance.pk: @@ -78,6 +79,8 @@ class RealtyForm(forms.ModelForm): def __init__(self, *args, **kwargs): self.request = kwargs.pop('request') super().__init__(*args, **kwargs) + + self.fields['location'].queryset = Location.objects.root_nodes()[0].get_descendants() class Realty1Form(forms.Form): @@ -131,20 +134,6 @@ class StageForm(forms.ModelForm): PortfolioPhotoFormSet = inlineformset_factory(Portfolio, PortfolioPhoto, fields=('img',)) -# class CustomerProjectEditForm(forms.ModelForm): -# pk = forms.ModelChoiceField(queryset=Project.objects.none()) -# -# class Meta: -# model = Project -# fields = '__all__' -# -# def __init__(self, *args, **kwargs): -# self.req = kwargs.pop('req') -# super().__init__(*args, **kwargs) -# -# self.fields['pk'].queryset = self.req.user.projects.filter(state='active') - - class CustomerProjectTrashForm(forms.Form): pk = forms.ModelChoiceField(queryset=Project.objects.none()) diff --git a/projects/templates/customer_project_create.html b/projects/templates/customer_project_create.html index a571955..6633b4a 100644 --- a/projects/templates/customer_project_create.html +++ b/projects/templates/customer_project_create.html @@ -82,23 +82,6 @@
-->
-{#
#} -{# {{ form.specialization }}#} -{#
#} - -{#
#} -{# #} -{#
#} -{#
#} -{# #} -{#
#} -{#
#} -{# #} -{#
#} -{#
#} -{# #} -{#
#} -
@@ -114,6 +97,8 @@
+ +
Бюджет {{ form.budget.errors.as_text }}
diff --git a/projects/templates/customer_project_edit.html b/projects/templates/customer_project_edit.html index 2523b5c..ef8f248 100644 --- a/projects/templates/customer_project_edit.html +++ b/projects/templates/customer_project_edit.html @@ -91,20 +91,22 @@
- {{ form.specialization }} +
+
- +
+
- +
+
- +
+ +
Бюджет {{ form.budget.errors.as_text }}