def create_request_debit_for_tree(self, user: MyUser, request: Request, action: int = 2): transactions = list() user_price_field = get_price_field_for_category(request.ts_category) for ancestors in user.get_ancestors( include_self=True).select_related('parent'): if action == 2: cost = getattr(ancestors.parent, user_price_field) if ancestors.parent_id else 0 subtotal = getattr(ancestors, user_price_field) else: cost = 0 subtotal = 0 transactions.append( Transaction(user_id=ancestors.pk, status=1, action=2, request=request, cost=cost, subtotal=subtotal, user_parent_id=ancestors.parent_id)) return self.bulk_create(transactions)
def get_available_for_user(self, user: MyUser, category: int = None): if user.stantion_order_type == 1: order = ('requests_created', 'id',) else: order = ('order', 'id',) user_stantion = self.filter(users__id=user.pk, active=True).order_by(*order) if user_stantion or user.use_only_self_stantions: return [stantion for stantion in user_stantion if stantion.is_allow_create(category)] else: ancestors = user.get_ancestors(ascending=True).values_list('id', 'use_only_self_stantions', 'stantion_order_type') use_only_self_stantions = False filter_ancestor = [] for ancestor in ancestors: if ancestor[1]: # reset filter_ancestor and set only this user filter_ancestor = [ancestor[0]] use_only_self_stantions = True if ancestor[2] == 1: order = ('requests_created', 'id',) else: order = ('order', 'id',) break else: filter_ancestor.append(ancestor[0]) if filter_ancestor: child_stantion = self.filter(users__id__in=filter_ancestor, active=True, is_available_for_child=True)\ .order_by(*order) if filter_ancestor and child_stantion.count(): return [stantion for stantion in child_stantion if stantion.is_allow_create(category)] elif use_only_self_stantions: return [] else: if category: filter_by_category = { CATEGORY_FIELD_AVAILABLE[category]: True } else: filter_by_category = {} return self.filter(active=True, requests_created__lt=F('daily_limit'), is_available_for_all_users=True, **filter_by_category)\ .filter(Q(freeze_date_end=None) | Q(freeze_date_end__lt=timezone.now()))\ .extra(select={'fieldsum': 'daily_limit - requests_created'}).order_by(*order)