示例#1
0
    def init_from_registered_applications(self, request):
        created_items = []
        for backend in CostTrackingRegister.get_registered_backends():
            try:
                items = backend.get_default_price_list_items()
            except NotImplementedError:
                continue
            for item in items:
                if not models.DefaultPriceListItem.objects.filter(resource_content_type=item.resource_content_type,
                                                                  item_type=item.item_type, key=item.key).exists():
                    if not item.name:
                        item.name = '{}: {}'.format(item.item_type, item.key)
                    item.save()
                    created_items.append(item)
        if created_items:
            message = ungettext(
                'Price item was created: {}'.format(created_items[0].name),
                'Price items were created: {}'.format(', '.join(item.name for item in created_items)),
                len(created_items)
            )
            self.message_user(request, message)
        else:
            self.message_user(request, "Price items exist for all registered applications")

        return redirect(reverse('admin:cost_tracking_defaultpricelistitem_changelist'))
示例#2
0
    def init_from_registered_applications(self, request):
        created_items = []
        for backend in CostTrackingRegister.get_registered_backends():
            try:
                items = backend.get_default_price_list_items()
            except NotImplementedError:
                continue
            with transaction.atomic():
                for item in items:
                    item, created = models.DefaultPriceListItem.objects.update_or_create(
                        resource_content_type=item.resource_content_type,
                        item_type=item.item_type,
                        key=item.key,
                        defaults={
                            'value': item.value,
                            'name': '{}: {}'.format(item.item_type, item.key),
                            'metadata': item.metadata,
                            'units': item.units
                        })
                    if created:
                        created_items.append(item)
        if created_items:
            message = ungettext(
                'Price item was created: {}'.format(created_items[0].name),
                'Price items were created: {}'.format(', '.join(
                    item.name for item in created_items)), len(created_items))
            self.message_user(request, message)
        else:
            self.message_user(
                request,
                "Price items for all registered applications have been updated"
            )

        return redirect(
            reverse('admin:cost_tracking_defaultpricelistitem_changelist'))