Ejemplo n.º 1
0
def update_entity_deleted_on(sender, **kwargs):
    if 'cubane.cms' in settings.INSTALLED_APPS:
        from cubane.cms.views import get_cms
        cms = get_cms()
        cms.notify_content_changed(
            sender, (PageBase, Entity, SettingsBase, DateTimeBase),
            delete=True)
Ejemplo n.º 2
0
        def wrapper(request, *args, **kwargs):
            # call actual view handler
            context = func(request, *args, **kwargs)

            # if the result is a dict, inject cms-related context
            if isinstance(context, dict):
                # get cache generator if this is rendering content for the
                # cache system...
                if hasattr(request, 'cache_generator'):
                    cache_generator = request.cache_generator
                else:
                    cache_generator = None

                # get template context from cms
                cms = request.cms if hasattr(request, 'cms') else get_cms()
                page = context.get('page')
                page_context = context.get('pageinfo', {})
                page_context_class = context.get('page_context_class')
                cms_context = cms.get_template_context(
                    request,
                    page=page,
                    page_context=page_context,
                    page_context_class=page_context_class,
                    cache_generator=cache_generator,
                    additional_context=context
                )
                return cms_context
            else:
                return context
Ejemplo n.º 3
0
 def test_create_object_should_raises_error_for_unknown_model(self):
     cms = get_cms()
     with self.assertRaisesRegexp(ValueError, 'Unknown model name'):
         cms.create_object('FakeModel', {
             'title': 'Page 1',
             'slug': 'page-1'
         })
Ejemplo n.º 4
0
    def password_forgotten(self, request, email):
        """
        Generate a new password for the given user and send the new password
        by email.
        """
        try:
            user = User.objects.get(email=email.lower().strip())
            password_plaintext = get_pronounceable_password()
            user.set_password(password_plaintext)
            user.save()

            from cubane.cms.views import get_cms
            cms = get_cms()
            cubane_send_shop_mail(
                request, email,
                '%s | New Password Request' % cms.settings.name, {
                    'password_reset': True,
                    'customer': user,
                    'password_plaintext': password_plaintext,
                    'login_url': get_absolute_url('shop.account.login',
                                                  https=True)
                })

            return True
        except User.DoesNotExist:
            return False
Ejemplo n.º 5
0
    def handle(self, *args, **options):
        """
        Run command.
        """
        from cubane.cms.views import get_cms
        cms = get_cms()

        out('Generating redirect urls...Please Wait...')

        sitemap = cms.get_sitemaps()
        redirects = {}
        for k, sitemap in sitemap.items():
            print k
            for item in sitemap.items():
                if hasattr(item, 'legacy_url'):
                    legacy_url = item.legacy_url
                    if legacy_url:
                        redirects[legacy_url] = item.get_absolute_url()
                elif hasattr(item, 'get_legacy_urls'):
                    legacy_urls = item.get_legacy_urls()
                    for legacy_url in legacy_urls:
                        redirects[legacy_url] = item.get_absolute_url()

        if not settings.TEST:
            print redirects
Ejemplo n.º 6
0
    def test_save_should_duplicate_file_if_asset_was_duplicated(self):
        cms = get_cms()
        self.m = cms.create_media_from_file(
            self.get_test_image_path('test.jpg'), 'Test')

        # prepare for duplication
        org = Media.objects.get(pk=self.m.pk)
        self.m.on_duplicated()
        self.assertEqual(self.m.pk, self.m._prior_pk)
        self.assertTrue(self.m._being_duplicated)
        self.assertIsNone(self.m.uid)
        self.m.pk = None

        # save() should create duplicate
        self.m.caption = 'Hello World'
        self.m.save()
        self.assertNotEqual(self.m.pk, org.pk)
        self.assertNotEqual(self.m.uid, org.uid)
        self.assertNotEqual(self.m.original_path, org.original_path)
        self.assertTrue(os.path.isfile(self.m.original_path))
        self.assertTrue(os.path.isfile(org.original_path))

        # was also renamed due to caption change
        self.assertEqual('Hello World', self.m.caption)
        self.assertEqual('hello-world.jpg', self.m.filename)
        self.assertEqual('hello-world.jpg',
                         os.path.basename(self.m.original_path))

        org.delete()
Ejemplo n.º 7
0
    def handle(self, *args, **options):
        """
        Run command.
        """
        print 'Rewriting CMS links ...Please Wait...'

        # simulation mode?
        simulate = options.get('simulate', False)

        # collect content models
        cms = get_cms()
        cms_models = tuple(cms.get_supported_models())
        links = self.get_page_links()

        # process all models in the system
        models = get_models()
        i = 0
        n = 0
        for m in models:
            if issubclass(m, cms_models):
                _i, _n = self.process_model(m, links, simulate,
                                            options.get('pk'))
                i += _i
                n += _n

        print '%d item(s) updated out of %d content page(s) in total.%s' % (
            i, n, ' (Simulated!)' if simulate else '')
Ejemplo n.º 8
0
    def handle(self, *args, **options):
        """
        Run command.
        """
        cms = get_cms()

        # remove any existing test page
        page = cms.get_page_by_slug('testpage')
        if page:
            page.delete()

        # find some image with high-enough res. or create a new test image
        try:
            img = Media.objects.filter(is_image=True, width__gte=200)[0]
        except IndexError:
            # create new test image
            img_path = os.path.join(settings.BASE_PATH, 'backend', 'static',
                                    'cubane', 'backend', 'img', 'logo.png')
            img = cms.create_media_from_file(path, 'Test Image for TestPage')

        # create new test page
        page = cms.create_page({
            'title': 'Test Page',
            'slug': 'testpage',
        })
        page.set_slot_content(
            cms.get_default_slotname(), self.CONTENT % {
                'img_url': img.original_url,
                'img_height': 200 / img.aspect_ratio,
                'img_id': img.pk
            })
        page.save()

        print 'Test page generated.'
        print make_absolute_url(page.get_absolute_url(), force_debug=True)
Ejemplo n.º 9
0
def cubane_send_cms_mail(request,
                         to,
                         subject,
                         page,
                         context=None,
                         cc=None,
                         bcc=None,
                         attachments=None):
    """
    Send an email to the given recepient with given subject line.
    The email is send from the sender that is configured in the
    cms settings. The email content is derived from rendering the
    given cms page.
    """
    if 'cubane.cms' not in settings.INSTALLED_APPS:
        raise ValueError('cubane.cms required for sending cms page emails.')

    # construct email message (derived from cms content)
    from cubane.cms.views import get_cms
    cms = get_cms()
    html = cms.render_page(page, request=None,
                           additional_context=context).content

    # send email
    cubane_send_mail(to,
                     subject,
                     html,
                     cc=cc,
                     bcc=bcc,
                     attachments=attachments)
Ejemplo n.º 10
0
def content(request, pk, slug, model=None, attr_name=None, backend_section=None):
    """
    Request handler for directory content assets.
    """
    # get page by id only, ignoring slug
    q = {
        'pk': pk,
        'disabled': False
    }

    # backend section?
    if attr_name and backend_section:
        q[attr_name] = backend_section

    # get that page
    try:
        page = model.filter_visibility(model.objects.filter(**q))[0]
    except IndexError:
        page = None

    # raise 404 if we cannot find a page
    if page == None:
        raise Http404('Unknown primary key or page is disabled.')

    # if we have a page, check if the slug is matching, if not redirect
    # to the correct url (temporary redirect)
    if page.slug != slug:
        return HttpResponsePermanentRedirect(page.get_fullslug())

    # render page or 404
    return get_cms().page(request, page)
Ejemplo n.º 11
0
def cubane_send_mail_no_html(to, subject, text, attachments=None):
    """
    Send an email to the given recepient with given subject line and text
    content.
    """
    if 'cubane.cms' not in settings.INSTALLED_APPS:
        raise ValueError('cubane.cms required for sending cms page emails.')
    from cubane.cms.views import get_cms
    cms = get_cms()

    # construct email
    msg = EmailMultiAlternatives(subject,
                                 text,
                                 cms.settings.enquiry_reply, [to],
                                 headers={
                                     'Reply-To': cms.settings.enquiry_reply,
                                     'From': cms.settings.enquiry_from
                                 })

    # attachement(s)
    if attachments:
        if not isinstance(attachments, list):
            attachments = [attachments]
        for attachment in attachments:
            msg.attach_file(attachment)

    # send it off
    msg.send()
Ejemplo n.º 12
0
    def send_email(self,
                   request,
                   template,
                   url,
                   subject,
                   message,
                   reason=None):
        """
        Send an email to the customer with the given subject line and message. The email will also contain
        a unique link to prompt the user to perform the desired action, for example to change the password.
        """
        if 'cubane.cms' in settings.INSTALLED_APPS:
            from cubane.cms.views import get_cms
            cms = get_cms()

            if cms.settings.name:
                subject = '%s | %s' % (subject, cms.settings.name)
        else:
            cms = None

        cubane_send_mail_template(
            request, self.user.email, subject, template, {
                'message': message,
                'url': make_absolute_url(url),
                'reason': reason,
                'settings': cms.settings if cms else None
            })
Ejemplo n.º 13
0
 def test_create_object_should_raise_error_for_missing_get_form_method(
         self):
     cms = get_cms()
     with self.assertRaisesRegexp(
             ValueError,
             'Unable to call \'get_form\' class method on model'):
         cms.create_object('TestGroupedModelA', {})
Ejemplo n.º 14
0
 def test_add_media_to_gallery_should_create_media_gallery(self):
     cms = get_cms()
     p = self.create_page('Page')
     m = Media()
     m.save()
     self.assertEqual(len(MediaGallery.objects.filter(target_id=p.pk)), 0)
     cms.add_media_to_gallery(p, m)
     self.assertEqual(len(MediaGallery.objects.filter(target_id=p.pk)), 1)
Ejemplo n.º 15
0
 def test_create_media_from_file_should_read_image_from_disk(self):
     cms = get_cms()
     filename = self.get_test_image_path('test.jpg')
     media = cms.create_media_from_file(filename, 'Test', 'testimage.jpg')
     self.assertIsNotNone(media.pk)
     self.assertEqual('Test', media.caption)
     self.assertEqual('test.jpg', media.filename)
     self.assertEqual([512, 512], [media.width, media.height])
Ejemplo n.º 16
0
 def test_resize_if_too_wide_should_ignore_non_image_file(self):
     cms = get_cms()
     self.m = cms.create_media_from_file(
         self.get_test_image_path('test.txt'), 'Test')
     self.m.resize_if_too_wide()
     self.assertEqual(0, self.m.width)
     self.assertEqual(0, self.m.height)
     self.assertFalse(self.m.is_image)
Ejemplo n.º 17
0
def send_client_mail(request, email, subject, context, attachments=None):
    from cubane.cms.views import get_cms
    cms = get_cms()
    cubane_send_shop_mail(request,
                          email,
                          subject,
                          context,
                          attachments=attachments)
Ejemplo n.º 18
0
    def test_create_object_should_ignore_missing_seq_field(self):
        cms = get_cms()

        def _get_seq_model_field(cms):
            raise FieldDoesNotExist()

        cms.get_seq_model_field = _get_seq_model_field
        cms.create_object('Page', {'title': 'Page', 'slug': 'page'})
Ejemplo n.º 19
0
 def test_create_media_from_url_should_download_file_from_url(self):
     cms = get_cms()
     filename = self.get_test_image_path('test.jpg')
     media = cms.create_media_from_url(
         'http://sipi.usc.edu/database/preview/textures/1.1.01.png', 'Test',
         'test.jpg')
     self.assertEqual('Test', media.caption)
     self.assertEqual('test.jpg', media.filename)
     self.assertEqual([200, 200], [media.width, media.height])
Ejemplo n.º 20
0
    def test_create_object_should_raise_error_when_unable_to_create_form_instance(
            self, get_form):
        get_form.return_value = 'not a form'

        cms = get_cms()
        with self.assertRaisesRegexp(
                ValueError,
                'Unable to create a new instance of the model form'):
            cms.create_object('Page', {'title': 'Page', 'slug': 'page'})
Ejemplo n.º 21
0
 def test_should_not_cache_disabled_directory_content(self):
     content = TestDirectoryContent(title='Hotel A', disabled=True)
     content.save()
     try:
         cms = get_cms()
         cache_items, _, _ = cms.publish()
         self.assertEqual(0, cache_items)
     finally:
         content.delete()
Ejemplo n.º 22
0
 def test_should_not_cache_disabled_aggregator_page(self):
     page = CustomDirectoryPage(title='Cromer', disabled=True)
     page.save()
     try:
         cms = get_cms()
         cache_items, _, _ = cms.publish()
         self.assertEqual(0, cache_items)
     finally:
         page.delete()
Ejemplo n.º 23
0
    def test_upload_png_image_without_transparency_should_store_image_as_jpg(
            self):
        cms = get_cms()
        self.m = cms.create_media_from_file(
            self.get_test_image_path('test.png'), 'Test')

        # verify it's a JPEG file
        img = open_image(self.m.original_path)
        self.assertEqual('test.jpg', self.m.filename)
        self.assertTrue(is_jpeg_image_object(img))
Ejemplo n.º 24
0
    def test_upload_png_with_transparency_should_not_convert_image_to_jpg(
            self):
        cms = get_cms()
        self.m = cms.create_media_from_file(
            self.get_test_image_path('test_with_transparency.png'), 'Test')

        # verify it's still a PNG file with transparency layer
        img = open_image(self.m.original_path)
        self.assertEqual('test.png', self.m.filename)
        self.assertTrue(is_png_image_object(img))
Ejemplo n.º 25
0
def collides_with_child_page_of_homepage(slug):
    """
    Return True, if the given slug matches any child page of the homepage.
    """
    homepage = get_cms().get_homepage()
    if homepage:
        child_page_model = homepage.get_entity_model()
        if child_page_model:
            return child_page_model.objects.filter(slug=slug,
                                                   page=homepage).count() > 0
    return False
Ejemplo n.º 26
0
 def test_should_inject_aggregated_content_for_nav_item(self):
     cms = get_cms()
     context = cms.get_page_context(self.factory.get('/cromer/'),
                                    page=self.page,
                                    view=cms)
     nav_item = context.get_template_context().get('nav').get('header')[0]
     items = [
         item.get('page_title')
         for item in nav_item.get('aggregated_pages')()
     ]
     self.assertEqual(['Hotel A', 'Hotel B'], items)
Ejemplo n.º 27
0
    def publish(self, request):
        """
        Publish cms changes.
        """
        cms = get_cms()

        try:
            items, size, time_sec = cms.publish()
        except Http404, e:
            # http404 errors are not send via email notification in production
            # mode, therefore rethrow the exception here
            raise ValueError(unicode(e))
Ejemplo n.º 28
0
    def handle(self, *args, **options):
        """
        Run command.
        """
        from cubane.cms.views import get_cms
        cms = get_cms()

        out('Invalidating cache...Please Wait...')
        out('CACHE: %s' % settings.CACHE_ROOT)

        items = cms.invalidate(verbose=True)
        out('%d files removed.' % items)
Ejemplo n.º 29
0
    def test_create_object_should_raise_error_if_configure_method_has_not_been_implemented_on_form(
            self, get_form):
        class Form(object):
            def __init__(self, *args, **kwargs):
                pass

        get_form.return_value = Form

        cms = get_cms()
        with self.assertRaisesRegexp(NotImplementedError,
                                     'must implement configure'):
            cms.create_object('Page', {'title': 'Page', 'slug': 'page'})
Ejemplo n.º 30
0
 def test_should_not_cache_directory_content_with_custom_visibility_checks(
         self):
     content = TestDirectoryContent(title='Hotel A')
     content.save()
     _filter_visibility = self.patch_filter_visibility(TestDirectoryContent)
     try:
         cms = get_cms()
         cache_items, _, _ = cms.publish(True)
         self.assertEqual(0, cache_items)
     finally:
         content.delete()
         self.restore_filter_visibility(TestDirectoryContent,
                                        _filter_visibility)