Пример #1
0
    def db_get(self, request, model_name, pk):
        """
        As part of the REST API, return a specific instance of the given model
        with the given pk.
        """
        # get model
        try:
            model = get_class_from_string(model_name)
        except:
            return self._db_error('Unknown model.')

        # get instance
        try:
            qs = model.objects.filter(pk=pk)
            instance = Acl.of(model).filter(request, qs)[0]
        except:
            return self._db_error('Unknown pk.')

        # result
        return {
            'success': True,
            'message': 'OK',
            'result': model_to_dict(instance,
                                    exclude_many_to_many=True,
                                    json=True)
        }
Пример #2
0
def get_page_model():
    """
    Return the page model as configured by settings.CMS_PAGE_MODEL.
    """
    if hasattr(settings, 'CMS_PAGE_MODEL'):
        return get_class_from_string(settings.CMS_PAGE_MODEL)
    else:
        # default cms page model
        from cubane.cms.models import Page
        return Page
Пример #3
0
def get_featured_item_model():
    """
    Return the page model as configured by settings.CMS_PAGE_MODEL.
    """
    if hasattr(settings, 'SHOP_FEATURED_ITEM_MODEL'):
        return get_class_from_string(settings.SHOP_FEATURED_ITEM_MODEL)
    else:
        raise ValueError(
            "cubane.ishop requires the settings variable 'SHOP_FEATURED_ITEM_MODEL' "
            +
            "to be set to the full path of the model class that represents " +
            "the featured items for the shop, for example myproject.models.FeaturedItem"
        )
Пример #4
0
    def get_backends(self):
        """
        Return a list of all registered font backends that are used to acquire
        fonts.
        """
        if not hasattr(self, '_backends'):
            self._backends = []

            if isinstance(settings.CUBANE_FONT_BACKENDS, list):
                for class_name in settings.CUBANE_FONT_BACKENDS:
                    _class = get_class_from_string(class_name)
                    backend = _class()
                    self._backends.append(backend)

        return self._backends
Пример #5
0
def get_enquiry_model():
    """
    Return the enquiry model as configured by settings.ENQUIRY_MODEL.
    """
    try:
        if hasattr(settings, 'ENQUIRY_MODEL'):
            return get_class_from_string(settings.ENQUIRY_MODEL)
        else:
            raise ImportError()
    except ImportError:
        raise ValueError(
            "cubane.enquiry requires the settings variable 'ENQUIRY_MODEL' " +
            "to be set to the full path of the model class that represents " +
            "enquiry messages, for example myproject.models.Enquiry. The " +
            "settings variable is either not configured or the model or class "
            + "cannot be imported.")
Пример #6
0
    def db_get_all(self, request, model_name):
        """
        As part of the REST API, return a list of all model instances.
        """
        # get model
        try:
            model = get_class_from_string(model_name)
        except:
            return self._db_error('Unknown model.')

        # get list
        qs = model.objects.all()
        instances = Acl.of(model).filter(request, qs)
        result = [
            model_to_dict(instance, exclude_many_to_many=True, json=True)
            for instance in instances
        ]

        # result
        return {'success': True, 'message': 'OK', 'result': result}
Пример #7
0
def get_shop():
    """
    Return the custom CMS implementation that is used to render CMS content.
    A site may implement its own CMS by deriving from the CMS base class.
    The custom class needs to be setup via settings.CMS, for example
    CMS = 'myproject.views.MyCMS'.
    """
    global SHOP_CLASS

    if not SHOP_CLASS:
        if hasattr(settings, 'SHOP') and settings.SHOP:
            SHOP_CLASS = get_class_from_string(settings.SHOP)
        else:
            raise ValueError(
                "cubane.cms requires the settings variable 'SHOP' " +
                "to be set to the full path of the shop class that represents " +
                "the cms system (derived from cubane.ishop.views.Shop), " +
                "for example myproject.views.MyProjectShop"
            )

    # creates a new instance every time...
    return SHOP_CLASS()
Пример #8
0
 def test_should_raise_if_none(self):
     with self.assertRaises(ImportError):
         get_class_from_string(None)
Пример #9
0
 def test_should_raise_if_empty_string(self):
     with self.assertRaises(ImportError):
         get_class_from_string('')
Пример #10
0
 def test_should_raise_if_model_does_not_exist(self):
     with self.assertRaises(ImportError):
         get_class_from_string(
             'cubane.testapp.does-not-exists.DoesNotExist')
Пример #11
0
 def test_should_raise_if_not_exists_in_module(self):
     with self.assertRaises(AttributeError):
         get_class_from_string('cubane.testapp.models.DoesNotExist')
Пример #12
0
 def test_should_return_class_that_exists(self):
     self.assertEqual(
         get_class_from_string('cubane.testapp.models.Enquiry'), Enquiry)
Пример #13
0
    def old_data_importer(self, request):
        """
        Import Form.
        """
        if request.method == 'POST':
            form = ShopDataImportForm(request.POST, request.FILES)
        else:
            form = ShopDataImportForm()
            form.configure(request)

            export_form = ShopInventoryExportForm()
            export_form.configure(request)

        if request.method == 'POST' and form.is_valid():
            d = form.cleaned_data

            # load data importer
            import_classname = settings.CUBANE_SHOP_IMPORT.get(
                'importer',
                'cubane.ishop.apps.merchant.dataimport.importer.ShopDataImporter'
            )
            import_class = get_class_from_string(import_classname)

            # create importer and start importing...
            importer = import_class(import_images=d.get('import_images'))
            importer.import_from_stream(request,
                                        request.FILES['csvfile'],
                                        encoding=d.get('encoding'))

            # present information what happend during import
            if importer.has_errors:
                transaction.rollback()

                errors = importer.get_formatted_errors()
                messages.add_message(
                    request, messages.ERROR,
                    ('Import failed due to %s. No data ' +
                     'has been imported. Please correct all issues ' +
                     'and try again.') %
                    pluralize(len(errors), 'error', tag='em'))

                for message in errors:
                    messages.add_message(request, messages.ERROR, message)

                # redirect to itself if we have errors
                return HttpResponseRedirect(
                    get_absolute_url('cubane.ishop.dataimport.index'))
            else:
                # success message, render image processing page
                messages.add_message(
                    request, messages.SUCCESS,
                    pluralize(importer.num_records_processed,
                              'record',
                              'processed successfully',
                              tag='em'))

                # redirect to itself if we have errors
                return HttpResponseRedirect(
                    get_absolute_url('cubane.ishop.dataimport.index'))

        return {'form': form, 'export_form': export_form}