Created
June 10, 2021 06:03
-
-
Save chyld/42ac62f0c5bc265af01513b3b0a8c0ca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # for f in 'engagement_managers', 'clients', 'instructors', 'units': | |
| # self.fields[f].required = False | |
| # self.fields['products'] = forms.CharField(widget=ProductWidget) | |
| # self.fields['products'].queryset = 'llolololo' | |
| # print('#?' * 1000) | |
| # print('product widget:', self.fields['products'], 'dict:', self.fields['products'].__dict__, 'dir:', dir(self.fields['products'])) | |
| # # forms.ModelMultipleChoiceField(queryset=Author.objects.all()) | |
| # if self.instance.id: | |
| # self.fields['units'] = forms.CharField(widget=UnitWidget) | |
| # self.fields['products'].queryset = 'llolololo' # self.instance.ordered_products() | |
| # else: | |
| # self.fields['units'].widget = forms.HiddenInput() | |
| def clean_products(self): | |
| pids = self.cleaned_data['products'] | |
| pids = pids.strip() if isinstance(pids, str) else pids | |
| pids = [line.strip() for line in pids.splitlines()] | |
| products = list(Product.objects.filter(pid__in=pids)) | |
| if len(pids) != len(products): | |
| raise ValidationError("Invalid product(s)") | |
| products = [[*filter(lambda product: product.pid == pid, products)][0] for pid in pids] | |
| print('&' * 1000) | |
| print('pids:', pids, 'clean products:', products) | |
| return products | |
| def clean_units(self): | |
| lines = self.cleaned_data['units'] | |
| lines = lines.strip() if isinstance(lines, str) else lines | |
| lines = [line.strip() for line in lines.splitlines()] if lines else [] | |
| units = list(Unit.objects.filter(unit__in=lines)) | |
| if len(set(lines)) != len(units): | |
| raise ValidationError("Invalid unit(s)") | |
| units = [[*filter(lambda unit: unit.unit == line, units)][0] for line in lines] | |
| print('x' * 1000) | |
| print('clean units:', units) | |
| return units | |
| class ProductWidget(Textarea): | |
| def format_value(self, value): | |
| print('$#' * 1000) | |
| print('product value:', value) | |
| if not value: | |
| return None | |
| if isinstance(value, str): | |
| return value | |
| return '\n'.join([product.pid for product in value]) | |
| class UnitWidget(Textarea): | |
| def format_value(self, value): | |
| print('@*' * 1000) | |
| print('unit value:', value) | |
| if not value: | |
| return None | |
| if isinstance(value, str): | |
| return value | |
| return '\n'.join([unit.unit for unit in value]) | |
| class EngagementUpdatePostMixin(AllowedRoleMixin): | |
| def get_success_url(self): | |
| return reverse_lazy('engagement-detail', kwargs={'pk': self.object.id}) | |
| @transaction.atomic | |
| def form_valid(self, form): | |
| print('$~' * 1000) | |
| print('before valid') | |
| if form.is_valid(): | |
| print('after valid') | |
| form.instance.save() | |
| print('deleting all engagement products') | |
| self.object.engagementproduct_set.all().delete() | |
| for idx, product in enumerate(form.cleaned_data['products']): | |
| print('idx:', idx, 'product:', product) | |
| EngagementProduct(product=product, engagement=form.instance, position=idx).save() | |
| return super().form_valid(form) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment