|
21 | 21 | from django.db import connections |
22 | 22 | from django.contrib.admin.models import LogEntry |
23 | 23 | from django.contrib.admin.widgets import AutocompleteSelect |
| 24 | +from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME |
24 | 25 | from django.shortcuts import render |
25 | 26 | from django.views.generic.edit import FormView |
26 | 27 | from django.core.cache import caches |
@@ -678,11 +679,21 @@ class ProductAdmin(CustomModelAdmin): |
678 | 679 | list_select_related = ('tax', 'model', 'group') |
679 | 680 | list_filter = (ProductGroupFilter, ProductManufacturerFilter, ProductModelFilter, TaxFilter) |
680 | 681 | autocomplete_fields = ('tax', 'model', 'group', 'barcodes', 'qrcodes') |
681 | | - actions = ('from_xls', 'to_xls', 'price_to_xls', 'barcode_to_svg', 'fix_barcodes') |
| 682 | + actions = ('from_xls', 'to_xls', 'price_to_xls', 'barcode_to_svg', 'fix_barcodes', 'reset_cached') |
682 | 683 |
|
683 | 684 | #class Media: |
684 | 685 | #js = ['admin/js/autocomplete.js', 'admin/js/vendor/select2/select2.full.js'] |
685 | 686 |
|
| 687 | + def changelist_view(self, request, extra_context=None): |
| 688 | + if 'action' in request.POST and request.POST['action'] == 'reset_cached': |
| 689 | + if not request.POST.getlist(ACTION_CHECKBOX_NAME): |
| 690 | + post = request.POST.copy() |
| 691 | + p = Product.objects.first() |
| 692 | + if p: |
| 693 | + post.update({ACTION_CHECKBOX_NAME: str(p.id)}) |
| 694 | + request._set_post(post) |
| 695 | + return super().changelist_view(request, extra_context) |
| 696 | + |
686 | 697 | def get_last_reg(self, obj): |
687 | 698 | if obj.id in self.__objs__ and 'lreg' in self.__objs__[obj.id]: |
688 | 699 | return self.__objs__[obj.id]['lreg'] |
@@ -742,21 +753,25 @@ def get_qrcodes(self, obj): |
742 | 753 | return format_html('<details><summary>{}</summary>{}</details>', idxs[0][1], content) |
743 | 754 | get_qrcodes.short_description = _('Qr Codes') |
744 | 755 |
|
| 756 | + def refresh_count_from_reg(self, obj): |
| 757 | + if obj.id not in self.__objs__: |
| 758 | + self.__objs__[obj.id] = {} |
| 759 | + SumIncome=Sum('rec__count', filter=Q(rec__doc__type__income=True), default=0) |
| 760 | + SumExpense=Sum('rec__count', filter=Q(rec__doc__type__income=False), default=0) |
| 761 | + try: |
| 762 | + self.__objs__[obj.id]['count'] = get_model('core.Register').objects.filter(rec__product_id=obj.id).aggregate(count=SumIncome-SumExpense)['count'] |
| 763 | + except Exception as e: |
| 764 | + self.loge(e) |
| 765 | + else: |
| 766 | + self.logi('FROM BASE', self.__objs__[obj.id]['count']) |
| 767 | + return self.__objs__[obj.id]['count'] |
| 768 | + return 0 |
| 769 | + |
745 | 770 | def get_count_from_reg(self, obj): |
746 | 771 | if obj.id in self.__objs__ and 'count' in self.__objs__[obj.id]: |
| 772 | + self.logi('FROM BUFFER', self.__objs__[obj.id]['count']) |
747 | 773 | return self.__objs__[obj.id]['count'] |
748 | | - else: |
749 | | - if obj.id not in self.__objs__: |
750 | | - self.__objs__[obj.id] = {} |
751 | | - SumIncome=Sum('rec__count', filter=Q(rec__doc__type__income=True), default=0) |
752 | | - SumExpense=Sum('rec__count', filter=Q(rec__doc__type__income=False), default=0) |
753 | | - try: |
754 | | - self.__objs__[obj.id]['count'] = get_model('core.Register').objects.filter(rec__product_id=obj.id).aggregate(count=SumIncome-SumExpense)['count'] |
755 | | - except Exception as e: |
756 | | - self.loge(e) |
757 | | - else: |
758 | | - return self.__objs__[obj.id]['count'] |
759 | | - return 0 |
| 774 | + return self.refresh_count_from_reg(obj) |
760 | 775 |
|
761 | 776 | def count(self, obj): |
762 | 777 | result = self.get_count_from_reg(obj) |
@@ -800,6 +815,10 @@ def get_group(self, obj): |
800 | 815 | get_group.short_description = _('Product Group') |
801 | 816 | get_group.admin_order_field = 'group' |
802 | 817 |
|
| 818 | + def reset_cached(self, request, queryset, **kwargs): |
| 819 | + self.__objs__ = {} |
| 820 | + reset_cached.short_description = f'↻💦{_("reset cached values")}' |
| 821 | + |
803 | 822 | def from_xls(self, request, queryset, **kwargs): |
804 | 823 | from barcode import EAN13 |
805 | 824 | from transliterate import slugify |
@@ -945,7 +964,7 @@ def barcode_to_svg(self, request, queryset): |
945 | 964 | from barcode import EAN13 |
946 | 965 | from barcode.writer import SVGWriter |
947 | 966 | def eval_dim(val, n, m='-'): |
948 | | - v = re.findall('\d+\.\d+', val)[0] |
| 967 | + v = re.findall(r'\d+\.\d+', val)[0] |
949 | 968 | suffix = val.replace(v, '') |
950 | 969 | return f'{eval(f'{v}{m}{n}')}{suffix}' |
951 | 970 | svgs = '' |
|
0 commit comments