Example #1
0
    def render(self, name, value, attrs=None, choices=()):
        if value is None: value = []
        has_id = attrs and 'id' in attrs
        final_attrs = self.build_attrs(attrs, name=name)
        output = ['<table  style="width:99%;"><tr>']
        # Normalize to strings
        str_values = set([force_unicode(v) for v in value])
        for i, (option_value, option_label) in enumerate(
                chain(self.choices, choices)):
            # If an ID attribute was given, add a numeric index as a suffix,
            # so that the checkboxes don't all have the same ID attribute.
            if has_id:
                final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i))
                label_for = ' for="%s"' % final_attrs['id']
            else:
                label_for = ''

            cb = forms.CheckboxInput(
                final_attrs, check_test=lambda value: value in str_values)
            option_value = force_unicode(option_value)
            rendered_cb = cb.render(name, option_value)
            option_label = conditional_escape(force_unicode(option_label))
            if i != 0 and i % self.items_per_row == 0:
                output.append('</tr><tr>')
            output.append('<td nowrap><label%s>%s %s</label></td>' %
                          (label_for, rendered_cb, option_label))
        output.append('</tr></table>')
        return mark_safe('\n'.join(output))
Example #2
0
    def test_relatedfieldlistfilter_manytomany(self):
        modeladmin = BookAdmin(Book, site)

        request = self.request_factory.get('/', {'contributors__isnull': 'True'})
        changelist = self.get_changelist(request, Book, modeladmin)

        # Make sure the correct queryset is returned
        queryset = changelist.get_query_set(request)
        self.assertEqual(list(queryset), [self.django_book, self.bio_book, self.djangonaut_book])

        # Make sure the last choice is None and is selected
        filterspec = changelist.get_filters(request)[0][2]
        self.assertEquals(force_unicode(filterspec.title), u'Verbose Contributors')
        choices = list(filterspec.choices(changelist))
        self.assertEqual(choices[-1]['selected'], True)
        self.assertEqual(choices[-1]['query_string'], '?contributors__isnull=True')

        request = self.request_factory.get('/', {'contributors__id__exact': self.bob.pk})
        changelist = self.get_changelist(request, Book, modeladmin)

        # Make sure the correct choice is selected
        filterspec = changelist.get_filters(request)[0][2]
        self.assertEquals(force_unicode(filterspec.title), u'Verbose Contributors')
        choice = select_by(filterspec.choices(changelist), "display", "bob")
        self.assertEqual(choice['selected'], True)
        self.assertEqual(choice['query_string'], '?contributors__id__exact=%d' % self.bob.pk)
Example #3
0
    def test_allvaluesfieldlistfilter(self):
        modeladmin = BookAdmin(Book, site)

        request = self.request_factory.get('/', {'year__isnull': 'True'})
        changelist = self.get_changelist(request, Book, modeladmin)

        # Make sure the correct queryset is returned
        queryset = changelist.get_query_set(request)
        self.assertEqual(list(queryset), [self.django_book])

        # Make sure the last choice is None and is selected
        filterspec = changelist.get_filters(request)[0][0]
        self.assertEqual(force_unicode(filterspec.title), u'year')
        choices = list(filterspec.choices(changelist))
        self.assertEqual(choices[-1]['selected'], True)
        self.assertEqual(choices[-1]['query_string'], '?year__isnull=True')

        request = self.request_factory.get('/', {'year': '2002'})
        changelist = self.get_changelist(request, Book, modeladmin)

        # Make sure the correct choice is selected
        filterspec = changelist.get_filters(request)[0][0]
        self.assertEqual(force_unicode(filterspec.title), u'year')
        choices = list(filterspec.choices(changelist))
        self.assertEqual(choices[2]['selected'], True)
        self.assertEqual(choices[2]['query_string'], '?year=2002')
Example #4
0
    def get_context(self):
        context = super(RevisionListView, self).get_context()

        opts = self.opts
        action_list = [
            {
                "revision": version.revision,
                "url": self.model_admin_url("revision", quote(version.object_id), version.id),
                "version": version,
            }
            for version in self._order_version_queryset(
                self.revision_manager.get_for_object_reference(self.model, self.obj.pk).select_related("revision__user")
            )
        ]
        context.update(
            {
                "title": _("Change history: %s") % force_unicode(self.obj),
                "action_list": action_list,
                "module_name": capfirst(force_unicode(opts.verbose_name_plural)),
                "object": self.obj,
                "app_label": opts.app_label,
                "changelist_url": self.model_admin_url("changelist"),
                "update_url": self.model_admin_url("change", self.obj.pk),
                "opts": opts,
            }
        )
        return context
Example #5
0
    def test_relatedfieldlistfilter_foreignkey(self):
        modeladmin = BookAdmin(Book, site)

        request = self.request_factory.get('/', {'author__isnull': 'True'})
        changelist = self.get_changelist(request, Book, modeladmin)

        # Make sure the correct queryset is returned
        queryset = changelist.get_query_set(request)
        self.assertEqual(list(queryset), [self.gipsy_book])

        # Make sure the last choice is None and is selected
        filterspec = changelist.get_filters(request)[0][1]
        self.assertEquals(force_unicode(filterspec.title), u'Verbose Author')
        choices = list(filterspec.choices(changelist))
        self.assertEqual(choices[-1]['selected'], True)
        self.assertEqual(choices[-1]['query_string'], '?author__isnull=True')

        request = self.request_factory.get('/', {'author__id__exact': self.alfred.pk})
        changelist = self.get_changelist(request, Book, modeladmin)

        # Make sure the correct choice is selected
        filterspec = changelist.get_filters(request)[0][1]
        self.assertEquals(force_unicode(filterspec.title), u'Verbose Author')
        # order of choices depends on User model, which has no order
        choice = select_by(filterspec.choices(changelist), "display", "alfred")
        self.assertEqual(choice['selected'], True)
        self.assertEqual(choice['query_string'], '?author__id__exact=%d' % self.alfred.pk)
Example #6
0
def reply_comment(request, parent):
	parent_comment = get_object_or_404(Comment.all_comments, pk = parent)
	content_object = parent_comment.content_object

	if parent_comment.parent_id:
		new_subject = parent_comment.subject
		if not new_subject.startswith(force_unicode('RE: ')):
			new_subject = force_unicode('RE: ') + new_subject
	else:
		new_subject = force_unicode('RE: ') + force_unicode(content_object)

	model_meta = content_object.__class__._meta
	template_list = [
		"comments/{0}_{1}_preview.html".format(*tuple(str(model_meta).split('.'))),
		"comments/{0}_preview.html".format(model_meta.app_label),
		"comments/preview.html",
	]
	next = request.GET.get('next', content_object.get_absolute_url())
	context = {
		'next': next,
		'parent': parent_comment if parent_comment.parent_id else False,
		'content_object': content_object,
		'module_name': get_module_name(content_object),
		'module_url': get_module_url(content_object),
	}

	if parent_comment.is_locked:
		return TemplateResponse(request, "comments/error.html", context)

	form = get_form()(content_object, logged = request.user.is_authenticated(), parent_comment = parent_comment, initial = {'subject': new_subject}, request = request)

	context["form"] = form
	context["attachments"] = form.get_attachments()

	return TemplateResponse(request, template_list, context)
Example #7
0
 def render(self, request=None):
   # This code is a slightly modified version of a standard Django tag.
   # The only change is to look for the logentry records in the right database.
   # See the file django\contrib\admin\templatetags\log.py
   from freppledb.common.middleware import _thread_locals
   try:
     db = _thread_locals.request.database or DEFAULT_DB_ALIAS
   except:
     db = DEFAULT_DB_ALIAS
   if isinstance(_thread_locals.request.user, AnonymousUser):
     q = LogEntry.objects.using(db).select_related('content_type', 'user')[:self.limit]
   else:
     q = LogEntry.objects.using(db).filter(user__id__exact=_thread_locals.request.user.pk).select_related('content_type', 'user')[:self.limit]
   result = []
   for entry in q:
     if entry.is_change():
       result.append('<span style="display: inline-block;" class="ui-icon ui-icon-pencil"></span><a href="%s%s">%s</a>' % (_thread_locals.request.prefix, entry.get_admin_url(), entry.object_repr))
     elif entry.is_addition():
       result.append('<span style="display: inline-block;" class="ui-icon ui-icon-plusthick"></span><a href="%s%s">%s</a>' % (_thread_locals.request.prefix, entry.get_admin_url(), entry.object_repr))
     elif entry.is_deletion():
       result.append('<span style="display: inline-block;" class="ui-icon ui-icon-minusthick"></span>%s' % entry.object_repr)
     else:
       raise "Unexpected log entry type"
     if entry.content_type:
       result.append('<span class="mini">%s</span><br/>' % capfirst(force_unicode(_(entry.content_type.name))) )
     else:
       result.append('<span class="mini">%s</span><br/>' % force_unicode(_('Unknown content')))
   return result and '\n'.join(result) or force_unicode(_('None available'))
Example #8
0
def safe_join(base, *paths):
    """
    A version of django.utils._os.safe_join for S3 paths.

    Joins one or more path components to the base path component
    intelligently. Returns a normalized version of the final path.

    The final path must be located inside of the base path component
    (otherwise a ValueError is raised).

    Paths outside the base path indicate a possible security
    sensitive operation.
    """
    from urlparse import urljoin
    base_path = force_unicode(base)
    base_path = base_path.rstrip('/')
    paths = [force_unicode(p) for p in paths]

    final_path = base_path
    for path in paths:
        final_path = urljoin(final_path.rstrip('/') + "/", path.rstrip("/"))

    # Ensure final_path starts with base_path and that the next character after
    # the final path is '/' (or nothing, in which case final_path must be
    # equal to base_path).
    base_path_len = len(base_path)
    if (not final_path.startswith(base_path) or
            final_path[base_path_len:base_path_len + 1] not in ('', '/')):
        raise ValueError('the joined path is located outside of the base path'
                         ' component')

    return final_path.lstrip('/')
Example #9
0
def send_html_mail(
    subject,
    message,
    message_html,
    from_email,
    recipient_list,
    priority="medium",
    fail_silently=False,
    auth_user=None,
    auth_password=None,
):
    """
    Function to queue HTML e-mails
    """
    from django.utils.encoding import force_unicode
    from django.core.mail import EmailMultiAlternatives
    from mailer.models import make_message

    priority = PRIORITY_MAPPING[priority]

    # need to do this in case subject used lazy version of ugettext
    subject = force_unicode(subject)
    message = force_unicode(message)

    msg = make_message(subject=subject, body=message, from_email=from_email, to=recipient_list, priority=priority)
    email = msg.email
    email = EmailMultiAlternatives(email.subject, email.body, email.from_email, email.to)
    email.attach_alternative(message_html, "text/html")
    msg.email = email
    msg.save()
    return 1
Example #10
0
    def test_new_behavior(self):
        """
        Test sanity of new verbose_names() model classmethod.
        """
        a = NewstyleSingularIntl.objects.create(name=u'Name')
        self.assertEqual('Modelo moderno traducible #1', NewstyleSingularIntl._meta.get_verbose_name())
        self.assertEqual('Modelo moderno traducible #1', a._meta.get_verbose_name())
        # Fallback get_verbose_name() implementation, its return value
        # can be bogus (note the arbitrary 's' tucked at the end)
        self.assertEqual('Modelo moderno traducible #1s', force_unicode(NewstyleSingularIntl._meta.get_verbose_name(0)))
        self.assertEqual('Modelo moderno traducible #1s', force_unicode(a._meta.get_verbose_name(0)))

        b = NewstyleBothIntl.objects.create(name=u'Name')
        self.assertEqual('Modelo moderno traducible #2', NewstyleBothIntl._meta.get_verbose_name())
        self.assertEqual('Modelo moderno traducible #2', b._meta.get_verbose_name())

        self.assertEqual('Modelos modernos traducibles #2', NewstyleBothIntl._meta.get_verbose_name(0))
        self.assertEqual('Modelos modernos traducibles #2', b._meta.get_verbose_name(0))

        c = NewstylePluralIntl.objects.create(name=u'Name')
        # Fallback get_verbose_name() implementation: Returns a value
        # automatically generated from the class name -- untranslatable
        self.assertEqual('newstyle plural intl', NewstylePluralIntl._meta.get_verbose_name())
        self.assertEqual('newstyle plural intl', c._meta.get_verbose_name())

        self.assertEqual('Modelos modernos traducibles #3', NewstylePluralIntl._meta.get_verbose_name(0))
        self.assertEqual('Modelos modernos traducibles #3', c._meta.get_verbose_name(0))
Example #11
0
def community_advprofile_edit_save_form(sender, instance, signal, request, **kwargs):
    data = instance.cleaned_data
    user = instance.user
    try:
        profile = CommunityUserProfile.objects.get( user = user, )
    except CommunityUserProfile.DoesNotExist:
        profile = CommunityUserProfile( user = user, )

    if profile.avatar and data['community_advprofile_avatar_remove']:
        # TODO: delete avatar file from disk.
        profile.avatar = None

    if data['community_advprofile_avatar']:
        f = getattr(data['community_advprofile_avatar'], 'tmpfile', None)
        if f is None:
            f = data['community_advprofile_avatar']

        #try to avoid limit of 100 characters to filename
        fname = data['community_advprofile_avatar'].name
        avt_name = force_unicode(fname)
        directory_name_len = len(force_unicode(profile.avatar.field.get_directory_name()))

        if len(avt_name) + directory_name_len > 100:
            name, ext = avt_name.split('.', 1)
            name = name[:96 - len(ext) - directory_name_len]  # 96 because django will append _x if filename already exists, and one for dot
            fname = '%s.%s' % (name, ext)

        profile.avatar.save( fname, f )
    #profile.avat = data['public_emailaddress']
    profile.save()
 def render_option(option_value, option_label):
     option_value = force_unicode(option_value)
     selected_html = (option_value in selected_choices) \
         and u' selected="selected"' or ''
     return u'<option value="%s"%s>%s</option>' % (
         escape(option_value), selected_html,
         conditional_escape(force_unicode(option_label)))
Example #13
0
    def test_backward_compatibility(self):
        """
        Test backward compatibility with legacy Meta.verbose_name{,_plural}
        attributes.
        """
        a = NewstyleSingularIntl.objects.create(name=u'Name')
        # The verbose_name derived from the verbose_names() method we specified
        self.assertEqual('Modelo moderno traducible #1', NewstyleSingularIntl._meta.verbose_name)
        self.assertEqual('Modelo moderno traducible #1', a._meta.verbose_name)
        # Automatically generated plural form, can be bogus (note the arbitrary
        # 's' tucked at the end)
        self.assertEqual('Modelo moderno traducible #1s', force_unicode(NewstyleSingularIntl._meta.verbose_name_plural))
        self.assertEqual('Modelo moderno traducible #1s', force_unicode(a._meta.verbose_name_plural))

        b = NewstyleBothIntl.objects.create(name=u'Name')
        # The verbose_name derived from the verbose_names() we specified
        self.assertEqual('Modelo moderno traducible #2', force_unicode(NewstyleBothIntl._meta.verbose_name))
        self.assertEqual('Modelo moderno traducible #2', b._meta.verbose_name)
        # The verbose_name_plural derived from the verbose_names() method we
        # specified
        self.assertEqual('Modelos modernos traducibles #2', NewstyleBothIntl._meta.verbose_name_plural)
        self.assertEqual('Modelos modernos traducibles #2', b._meta.verbose_name_plural)

        c = NewstylePluralIntl.objects.create(name=u'Name')
        # Verbose name automatically generated from the class name -- untranslatable
        self.assertEqual('newstyle plural intl', NewstylePluralIntl._meta.verbose_name)
        self.assertEqual('newstyle plural intl', c._meta.verbose_name)
        # The verbose_name_plural derived from the verbose_names() method we
        # specified
        self.assertEqual('Modelos modernos traducibles #3', NewstylePluralIntl._meta.verbose_name_plural)
        self.assertEqual('Modelos modernos traducibles #3', c._meta.verbose_name_plural)
Example #14
0
    def render(self, name, value, attrs=None, choices=()):
        if value is None:
            value = []
        has_id = attrs and 'id' in attrs
        final_attrs = self.build_attrs(attrs, name=name)
        output = []
        # Normalize to strings
        str_values = set([force_unicode(v) for v in value])
        for i, (option_value, option_label) in enumerate(chain(self.choices, choices)):
            # If an ID attribute was given, add a numeric index as a suffix,
            # so that the checkboxes don't all have the same ID attribute.
            if has_id:
                final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i))
                label_for = u' for="%s"' % final_attrs['id']
            else:
                label_for = ''

            cb = forms.CheckboxInput(
                final_attrs, check_test=lambda value: value in str_values)
            option_value = force_unicode(option_value)
            rendered_cb = cb.render(name, option_value)
            option_label = conditional_escape(force_unicode(option_label))

            if final_attrs.get('inline', False):
                output.append(u'<label%s class="checkbox-inline">%s %s</label>' % (label_for, rendered_cb, option_label))
            else:
                output.append(u'<div class="checkbox"><label%s>%s %s</label></div>' % (label_for, rendered_cb, option_label))
        return mark_safe(u'\n'.join(output))
 def render_options(self, choices, selected_choices):
     from itertools import chain
     from django.utils.encoding import force_unicode
     from django.utils.html import escape, conditional_escape
     def render_option(option_value, option_label):
         option_value = force_unicode(option_value)
         selected_html = (option_value in selected_choices) \
             and u' selected="selected"' or ''
         return u'<option value="%s"%s>%s</option>' % (
             escape(option_value), selected_html,
             conditional_escape(force_unicode(option_label)))
     # Normalize to strings.
     selected_choices = [
         v.get_value()
         for v in selected_choices if isinstance(v, DjangoState)]
     selected_choices = set([force_unicode(v) for v in selected_choices])
     output = []
     for option_value, option_label in chain(self.choices, choices):
         if isinstance(option_label, (list, tuple)):
             output.append(u'<optgroup label="%s">' %
                           escape(force_unicode(option_value)))
             for option in option_label:
                 output.append(render_option(*option))
             output.append(u'</optgroup>')
         else:
             output.append(render_option(option_value, option_label))
     return u'\n'.join(output)
Example #16
0
def quota(val, units=None):
    if val == float("inf"):
        return _("No Limit")
    elif units is not None:
        return "%s %s %s" % (val, force_text(units), force_unicode(_("Available")))
    else:
        return "%s %s" % (val, force_unicode(_("Available")))
def process_django_model_docstring(app, what, name, obj, options, lines):
    """
    Does special processing for django model docstrings, making docs for
    fields in the model.
    """
    # This causes import errors if left outside the function
    from django.db import models
    
    # Only look at objects that inherit from Django's base model class
    if inspect.isclass(obj) and issubclass(obj, models.Model):
        # Grab the field list from the meta class
        fields = obj._meta.fields
    
        for field in fields:
            # Decode and strip any html out of the field's help text
            help_text = strip_tags(force_unicode(field.help_text))
            
            # Decode and capitalize the verbose name, for use if there isn't
            # any help text
            verbose_name = force_unicode(field.verbose_name).capitalize()
            
            if help_text:
                # Add the model field to the end of the docstring as a param
                # using the help text as the description
                lines.append(u':param %s: %s' % (field.attname, help_text))
            else:
                # Add the model field to the end of the docstring as a param
                # using the verbose name as the description
                lines.append(u':param %s: %s' % (field.attname, verbose_name))
                
            # Add the field's type to the docstring
            lines.append(u':type %s: %s' % (field.attname, type(field).__name__))
    
    # Return the extended docstring
    return lines  
Example #18
0
    def clean(self, value):
        if self.required and not value:
            raise ValidationError(self.error_messages['required'])
        elif not self.required and not value:
            return []
        if not isinstance(value, (list, tuple)):
            raise ValidationError(self.error_messages['list'])
        new_values = []
        key = self.to_field_name or 'pk'
        for pk in list(value):
            try:
                self.queryset.filter(**{key: pk})
            except ValueError:
                value.remove(pk)
                new_values.append(pk)

        for val in new_values:
            value.append(self.create_new_value(force_unicode(val)))

        # Usually new_values will have list of new tags, but if the tag is
        # suppose of type int then that could be interpreted as valid pk
        # value and ValueError above won't be triggered.
        # Below we find such tags and create them, by check if the pk
        # actually exists.
        qs = self.queryset.filter(**{'%s__in' % key: value})
        pks = set([force_unicode(getattr(o, key)) for o in qs])
        for i in range(0, len(value)):
            val = force_unicode(value[i])
            if val not in pks:
                value[i] = self.create_new_value(val)
        # Since this overrides the inherited ModelChoiceField.clean
        # we run custom validators here
        self.run_validators(value)
        return qs
Example #19
0
	def lxml_soup(string):
		'Safe processing of any tag soup (which is a norm on the internets).'
		try: doc = lxml_fromstring(force_unicode(string))
		except (lxml_SyntaxError, lxml_ParserError): # last resort for "tag soup"
			from lxml.html.soupparser import fromstring as soup
			doc = soup(force_unicode(string))
		return doc
Example #20
0
 def render_options(self, choices, selected_choices):
     def render_option(option_value, option_label):
         option_value = force_unicode(option_value)
         try:
             indentation = self.model._default_manager.get(
                 pk=option_value,
                 ).get_level()
         except:
             indentation = 0
             
         selected_html = (option_value in selected_choices) and u' selected="selected"' or ''
         return u'<option value="%s"%s>%s</option>' % (
             escape(option_value), selected_html,
             ("-" * indentation) + " " + conditional_escape(force_unicode(option_label)),)
     # Normalize to strings.
     selected_choices = set([force_unicode(v) for v in selected_choices])
     output = []
     for option_value, option_label in chain(self.choices, choices):
         if isinstance(option_label, (list, tuple)):
             output.append(u'<optgroup label="%s">' % escape(force_unicode(option_value)))
             for option in option_label:
                 output.append(render_option(*option))
             output.append(u'</optgroup>')
         else:
             output.append(render_option(option_value, option_label))
     return u'\n'.join(output)
Example #21
0
 def __init__(self, req):
     self._req = req
     # FIXME: This isn't ideal. The request URI may be encoded (it's
     # non-normalized) slightly differently to the "real" SCRIPT_NAME
     # and PATH_INFO values. This causes problems when we compute path_info,
     # below. For now, don't use script names that will be subject to
     # encoding/decoding.
     self.path = force_unicode(req.uri)
     root = req.get_options().get('django.root', '')
     self.django_root = root
     # req.path_info isn't necessarily computed correctly in all
     # circumstances (it's out of mod_python's control a bit), so we use
     # req.uri and some string manipulations to get the right value.
     if root and req.uri.startswith(root):
         self.path_info = force_unicode(req.uri[len(root):])
     else:
         self.path_info = self.path
     if not self.path_info:
         # Django prefers empty paths to be '/', rather than '', to give us
         # a common start character for URL patterns. So this is a little
         # naughty, but also pretty harmless.
         self.path_info = u'/'
     self._post_parse_error = False
     self._stream = self._req
     self._read_started = False
Example #22
0
    def response_change(self, request, obj):
        """
        If the 'Save' button is clicked, redirect to fields list
        with the selected app.
        """
        if "_save" in request.POST:
            opts = obj._meta
            verbose_name = opts.verbose_name
            module_name = opts.module_name
            if obj._deferred:
                opts_ = opts.proxy_for_model._meta
                verbose_name = opts_.verbose_name
                module_name = opts_.module_name

            msg = _('The %(name)s "%(obj)s" was changed successfully.') % {
                "name": force_unicode(verbose_name),
                "obj": force_unicode(obj),
            }
            self.message_user(request, msg)
            post_url = "%s?membership_app_id=%d" % (
                reverse("admin:%s_%s_changelist" % (opts.app_label, module_name), current_app=self.admin_site.name),
                obj.membership_app_id,
            )
            return HttpResponseRedirect(post_url)
        else:
            return super(MembershipAppField2Admin, self).response_change(request, obj)
Example #23
0
    def count(self):
        """
        Returns the number of records as an integer.

        The result is not cached nor comes from cache, cache must be handled
        by the server.
        """
        clone = self._clone()

        # Instantiation of clone.model is necessary because we can't set
        # a staticmethod for get_resource_url_count and avoid to set it
        # for all model without relying on get_resource_url_list
        instance = clone.model()
        resource = Resource(instance.get_resource_url_count(),
                            filters=ROA_FILTERS, **ROA_SSL_ARGS)
        try:
            parameters = clone.query.parameters
            logger.debug(u"""Counting  : "%s" through %s with parameters "%s" """ % (
                clone.model.__name__,
                resource.uri,
                force_unicode(parameters)))
            response = resource.get(headers=self._get_http_headers(), **parameters)
        except Exception as e:
            raise ROAException(e)

        response = force_unicode(response.body_string()).encode(DEFAULT_CHARSET)
        data = self.model.get_parser().parse(StringIO(response))
        return self.model.count_response(data)
Example #24
0
 def history_view(self, request, object_id, extra_context=None):
     "The 'history' admin view for this model."
     from django.contrib.admin.models import LogEntry
     model = self.model
     opts = model._meta
     app_label = opts.app_label
     action_list = LogEntry.objects.filter(
         object_id = object_id,
         content_type__id__exact = ContentType.objects.get_for_model(model).id
     ).select_related().order_by('action_time')
     # If no history was found, see whether this object even exists.
     obj = get_object_or_404(model, pk=object_id)
     context = {
         'title': _('Change history: %s') % force_unicode(obj),
         'action_list': action_list,
         'module_name': capfirst(force_unicode(opts.verbose_name_plural)),
         'object': obj,
         'root_path': self.admin_site.root_path,
         'app_label': app_label,
     }
     context.update(extra_context or {})
     return render_to_response(self.object_history_template or [
         "admin/%s/%s/object_history.html" % (app_label, opts.object_name.lower()),
         "admin/%s/object_history.html" % app_label,
         "admin/object_history.html"
     ], context, context_instance=template.RequestContext(request))
Example #25
0
    def iterator(self):
        """
        An iterator over the results from applying this QuerySet to the
        remote web service.
        """
        resource = Resource(self.model.get_resource_url_list(**self.query.filters),
                            filters=ROA_FILTERS, **ROA_SSL_ARGS)
        try:
            parameters = self.query.parameters
            logger.debug(u"""Requesting: "%s" through %s with parameters "%s" """ % (
                          self.model.__name__,
                          resource.uri,
                          force_unicode(parameters)))
            response = resource.get(headers=self._get_http_headers(), **parameters)
        except ResourceNotFound:
            return
        except Exception as e:
            raise ROAException(e)

        response = force_unicode(response.body_string()).encode(DEFAULT_CHARSET)

        # Deserializing objects:
        data = self.model.get_parser().parse(StringIO(response))

        # [] is the case of empty no-paginated result
        if data != []:
            serializer = self.model.get_serializer(data=data)
            if not serializer.is_valid():
                raise ROAException(u'Invalid deserialization for {} model: {}'.format(self.model, serializer.errors))

            for obj in serializer.object:
                yield obj
Example #26
0
    def render(self, name, value, attrs=None, choices=()):
        if value is None: value = []
        has_id = attrs and 'id' in attrs
        final_attrs = self.build_attrs(attrs, name=name)
        output = [u'<ul>']
        # Normalize to strings
        str_values = set([force_unicode(v) for v in value])
        selected_cbs = {}
        unselected_cbs = []
        for i, (option_value, option_label) in enumerate(chain(self.choices, choices)):
            # If an ID attribute was given, add a numeric index as a suffix,
            # so that the checkboxes don't all have the same ID attribute.
            if has_id:
                final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i))
                label_for = u' for="%s"' % final_attrs['id']
            else:
                label_for = ''

            cb = forms.CheckboxInput(final_attrs, check_test=lambda value: value in str_values)
            option_value = force_unicode(option_value)
            rendered_cb = cb.render(name, option_value)
            option_label = conditional_escape(force_unicode(option_label))
            html = u'<li><label%s>%s %s</label></li>' % (label_for, rendered_cb, option_label)
            if option_value in str_values:
                selected_cbs[option_value] = html
            else:
                unselected_cbs.append(html)
        output.extend([selected_cbs[unicode(pk)] for pk in value])
        output.extend(unselected_cbs)
        output.append(u'</ul>')
        return mark_safe(u'\n'.join(output))
Example #27
0
    def construct_change_message(self, request, form, formsets):
        """
        Construct a change message from a changed object.
        """
        change_message = []
        if form.changed_data:
            change_message.append(_('Changed %s.') % get_text_list(form.changed_data, _('and')))

        if formsets:
            for formset in formsets:
                for added_object in formset.new_objects:
                    change_message.append(_('Added %(name)s "%(object)s".')
                                          % {'name': added_object._meta.verbose_name,
                                             'object': force_unicode(added_object)})
                for changed_object, changed_fields in formset.changed_objects:
                    change_message.append(_('Changed %(list)s for %(name)s "%(object)s".')
                                          % {'list': get_text_list(changed_fields, _('and')),
                                             'name': changed_object._meta.verbose_name,
                                             'object': force_unicode(changed_object)})
                for deleted_object in formset.deleted_objects:
                    change_message.append(_('Deleted %(name)s "%(object)s".')
                                          % {'name': deleted_object._meta.verbose_name,
                                             'object': force_unicode(deleted_object)})
        change_message = ' '.join(change_message)
        return change_message or _('No fields changed.')
Example #28
0
 def submission_problem_instance(self, instance):
     if instance.submission.kind != 'NORMAL':
         return '%s (%s)' % (force_unicode(
             instance.submission.problem_instance),
             force_unicode(instance.submission.get_kind_display()))
     else:
         return instance.submission.problem_instance
Example #29
0
 def response(self, request):
   data = dict(title=force_unicode(self.title), message=force_unicode(self.message), detail=force_unicode(self.detail), traceback=self.traceback)
   if not request.ajax:
     data['request'] = request
   response = desktop.lib.django_util.render("popup_error.mako", request, data)
   response.status_code = self.error_code
   return response
Example #30
0
 def render(self, name, value, attrs=None):
     if value is None: value = ''
     value = force_unicode(value)
     final_attrs = self.build_attrs(attrs, name=name)
     final_attrs.update({'class':'extTextArea'})
     return mark_safe(u'<textarea%s>%s</textarea>' % (flatatt(final_attrs),
             conditional_escape(force_unicode(value))))
Example #31
0
 def item_description(self, item):
     return force_unicode(item)
Example #32
0
    def more_like_this(self,
                       model_instance,
                       additional_query_string=None,
                       start_offset=0,
                       end_offset=None,
                       models=None,
                       limit_to_registered_models=None,
                       result_class=None,
                       **kwargs):
        if not self.setup_complete:
            self.setup()

        # Deferred models will have a different class ("RealClass_Deferred_fieldname")
        # which won't be in our registry:
        model_klass = model_instance._meta.concrete_model

        field_name = self.content_field_name
        narrow_queries = set()
        narrowed_results = None
        self.index = self.index.refresh()

        if limit_to_registered_models is None:
            limit_to_registered_models = getattr(
                settings, 'HAYSTACK_LIMIT_TO_REGISTERED_MODELS', True)

        if models and len(models):
            model_choices = sorted([
                '%s.%s' % (model._meta.app_label, model._meta.module_name)
                for model in models
            ])
        elif limit_to_registered_models:
            # Using narrow queries, limit the results to only models handled
            # with the current routers.
            model_choices = self.build_models_list()
        else:
            model_choices = []

        if len(model_choices) > 0:
            if narrow_queries is None:
                narrow_queries = set()

            narrow_queries.add(' OR '.join(
                ['%s:%s' % (DJANGO_CT, rm) for rm in model_choices]))

        if additional_query_string and additional_query_string != '*':
            narrow_queries.add(additional_query_string)

        narrow_searcher = None

        if narrow_queries is not None:
            # Potentially expensive? I don't see another way to do it in Whoosh...
            narrow_searcher = self.index.searcher()

            for nq in narrow_queries:
                recent_narrowed_results = narrow_searcher.search(
                    self.parser.parse(force_unicode(nq)))

                if len(recent_narrowed_results) <= 0:
                    return {
                        'results': [],
                        'hits': 0,
                    }

                if narrowed_results:
                    narrowed_results.filter(recent_narrowed_results)
                else:
                    narrowed_results = recent_narrowed_results

        # Prevent against Whoosh throwing an error. Requires an end_offset
        # greater than 0.
        if not end_offset is None and end_offset <= 0:
            end_offset = 1

        # Determine the page.
        page_num = 0

        if end_offset is None:
            end_offset = 1000000

        if start_offset is None:
            start_offset = 0

        page_length = end_offset - start_offset

        if page_length and page_length > 0:
            page_num = start_offset / page_length

        # Increment because Whoosh uses 1-based page numbers.
        page_num += 1

        self.index = self.index.refresh()
        raw_results = EmptyResults()

        if self.index.doc_count():
            query = "%s:%s" % (ID, get_identifier(model_instance))
            searcher = self.index.searcher()
            parsed_query = self.parser.parse(query)
            results = searcher.search(parsed_query)

            if len(results):
                raw_results = results[0].more_like_this(field_name,
                                                        top=end_offset)

            # Handle the case where the results have been narrowed.
            if narrowed_results is not None and hasattr(raw_results, 'filter'):
                raw_results.filter(narrowed_results)

        try:
            raw_page = ResultsPage(raw_results, page_num, page_length)
        except ValueError:
            if not self.silently_fail:
                raise

            return {
                'results': [],
                'hits': 0,
                'spelling_suggestion': None,
            }

        results = self._process_results(raw_page, result_class=result_class)
        searcher.close()

        if hasattr(narrow_searcher, 'close'):
            narrow_searcher.close()

        return results
Example #33
0
    def search(self,
               query_string,
               sort_by=None,
               start_offset=0,
               end_offset=None,
               fields='',
               highlight=False,
               facets=None,
               date_facets=None,
               query_facets=None,
               narrow_queries=None,
               spelling_query=None,
               within=None,
               dwithin=None,
               distance_point=None,
               models=None,
               limit_to_registered_models=None,
               result_class=None,
               **kwargs):
        if not self.setup_complete:
            self.setup()

        # A zero length query should return no results.
        if len(query_string) == 0:
            return {
                'results': [],
                'hits': 0,
            }

        query_string = force_unicode(query_string)

        # A one-character query (non-wildcard) gets nabbed by a stopwords
        # filter and should yield zero results.
        if len(query_string) <= 1 and query_string != u'*':
            return {
                'results': [],
                'hits': 0,
            }

        reverse = False

        if sort_by is not None:
            # Determine if we need to reverse the results and if Whoosh can
            # handle what it's being asked to sort by. Reversing is an
            # all-or-nothing action, unfortunately.
            sort_by_list = []
            reverse_counter = 0

            for order_by in sort_by:
                if order_by.startswith('-'):
                    reverse_counter += 1

            if len(sort_by) > 1 and reverse_counter > 1:
                raise SearchBackendError(
                    "Whoosh does not handle more than one field and any field being ordered in reverse."
                )

            for order_by in sort_by:
                if order_by.startswith('-'):
                    sort_by_list.append(order_by[1:])

                    if len(sort_by_list) == 1:
                        reverse = True
                else:
                    sort_by_list.append(order_by)

                    if len(sort_by_list) == 1:
                        reverse = False

            sort_by = sort_by_list[0]

        if facets is not None:
            warnings.warn("Whoosh does not handle faceting.",
                          Warning,
                          stacklevel=2)

        if date_facets is not None:
            warnings.warn("Whoosh does not handle date faceting.",
                          Warning,
                          stacklevel=2)

        if query_facets is not None:
            warnings.warn("Whoosh does not handle query faceting.",
                          Warning,
                          stacklevel=2)

        narrowed_results = None
        self.index = self.index.refresh()

        if limit_to_registered_models is None:
            limit_to_registered_models = getattr(
                settings, 'HAYSTACK_LIMIT_TO_REGISTERED_MODELS', True)

        if models and len(models):
            model_choices = sorted([
                '%s.%s' % (model._meta.app_label, model._meta.module_name)
                for model in models
            ])
        elif limit_to_registered_models:
            # Using narrow queries, limit the results to only models handled
            # with the current routers.
            model_choices = self.build_models_list()
        else:
            model_choices = []

        if len(model_choices) > 0:
            if narrow_queries is None:
                narrow_queries = set()

            narrow_queries.add(' OR '.join(
                ['%s:%s' % (DJANGO_CT, rm) for rm in model_choices]))

        narrow_searcher = None

        if narrow_queries is not None:
            # Potentially expensive? I don't see another way to do it in Whoosh...
            narrow_searcher = self.index.searcher()

            for nq in narrow_queries:
                recent_narrowed_results = narrow_searcher.search(
                    self.parser.parse(force_unicode(nq)))

                if len(recent_narrowed_results) <= 0:
                    return {
                        'results': [],
                        'hits': 0,
                    }

                if narrowed_results:
                    narrowed_results.filter(recent_narrowed_results)
                else:
                    narrowed_results = recent_narrowed_results

        self.index = self.index.refresh()

        if self.index.doc_count():
            searcher = self.index.searcher()
            parsed_query = self.parser.parse(query_string)

            # In the event of an invalid/stopworded query, recover gracefully.
            if parsed_query is None:
                return {
                    'results': [],
                    'hits': 0,
                }

            # Prevent against Whoosh throwing an error. Requires an end_offset
            # greater than 0.
            if not end_offset is None and end_offset <= 0:
                end_offset = 1

            raw_results = searcher.search(parsed_query,
                                          limit=end_offset,
                                          sortedby=sort_by,
                                          reverse=reverse)

            # Handle the case where the results have been narrowed.
            if narrowed_results is not None:
                raw_results.filter(narrowed_results)

            # Determine the page.
            page_num = 0

            if end_offset is None:
                end_offset = 1000000

            if start_offset is None:
                start_offset = 0

            page_length = end_offset - start_offset

            if page_length and page_length > 0:
                page_num = start_offset / page_length

            # Increment because Whoosh uses 1-based page numbers.
            page_num += 1

            try:
                raw_page = ResultsPage(raw_results, page_num, page_length)
            except ValueError:
                if not self.silently_fail:
                    raise

                return {
                    'results': [],
                    'hits': 0,
                    'spelling_suggestion': None,
                }

            results = self._process_results(raw_page,
                                            highlight=highlight,
                                            query_string=query_string,
                                            spelling_query=spelling_query,
                                            result_class=result_class)
            searcher.close()

            if hasattr(narrow_searcher, 'close'):
                narrow_searcher.close()

            return results
        else:
            if self.include_spelling:
                if spelling_query:
                    spelling_suggestion = self.create_spelling_suggestion(
                        spelling_query)
                else:
                    spelling_suggestion = self.create_spelling_suggestion(
                        query_string)
            else:
                spelling_suggestion = None

            return {
                'results': [],
                'hits': 0,
                'spelling_suggestion': spelling_suggestion,
            }
Example #34
0
 def render(self):
     """Outputs a <ul> for this set of radio fields."""
     return mark_safe(
         u'<ul>\n%s\n</ul>' %
         u'\n'.join([u'<li>%s</li>' % force_unicode(w) for w in self]))
Example #35
0
 def __init__(self, name, value, attrs, choice, index):
     self.name, self.value = name, value
     self.attrs = attrs
     self.choice_value = force_unicode(choice[0])
     self.choice_label = force_unicode(choice[1])
     self.index = index
Example #36
0
 def render(self, name, value, attrs=None):
     if value is None: value = ''
     final_attrs = self.build_attrs(attrs, name=name)
     return mark_safe(
         u'<textarea%s>%s</textarea>' %
         (flatatt(final_attrs), conditional_escape(force_unicode(value))))
Example #37
0
 def _get_FIELD_display(self, field):
     if isinstance(field, StateField):
         value = getattr(self, field.attname)
         return force_unicode(value.title)
     else:
         return super(WorkflowEnabled, self)._get_FIELD_display(field)
Example #38
0
    def render(self, name, value, attrs=None):
        if value is None:
            value = ''
        final_attrs = self.build_attrs(attrs, name=name)
        html = """
            <div id="%(id)sepiceditor"></div>
            <textarea%(attrs)s>%(body)s</textarea>
            <script type="text/javascript">
                var ee = {
                    'jQuery': (typeof window.django != 'undefined') ? django.jQuery : jQuery.noConflict(true)
                };

                (function($) {
                  $(document).ready(function() {
                    var opts = {
                        container: '%(id)sepiceditor',
                        basePath: '%(basePath)s',
                        theme: {
                          base:'%(theme_base)s',
                          preview:'%(theme_preview)s',
                          editor:'%(theme_editor)s'
                        },
                        file:{
                          defaultContent: "%(defaultContent)s",
                        },
                        clientSideStorage: false,
                        useNativeFullsreen: true,
                        parser: marked,
                        focusOnLoad: false,
                        shortcut: {
                            modifier: 18,
                            fullscreen: 70,
                            preview: 80
                        }
                    }
                    var editor = new EpicEditor(opts);

                    // get textarea and hide it
                    %(textarea)s = $('#%(id)s');
                    %(textarea)s.hide();
                    // then be sure to populate the textarea
                    editor.on('update', function () {
                      %(textarea)s.val(editor.exportFile());
                    });

                    // Everything is all setup, so load!
                    editor.load();
                  });
                })(ee.jQuery);
            </script>
            """ % {
            'basePath':
            (settings.STATIC_URL or settings.MEDIA_URL) + 'epiceditor',
            'defaultContent': escapejs(force_unicode(value)),
            'theme_base': self.themes['base'],
            'theme_preview': self.themes['preview'],
            'theme_editor': self.themes['editor'],
            'attrs': flatatt(final_attrs),
            'body': conditional_escape(force_unicode(value)),
            'id': attrs['id'],
            'textarea': "$textarea_" + attrs['id'].replace('-', '_'),
        }
        return mark_safe(html)
Example #39
0
 def process_clob(self, value):
     if value is None:
         return u''
     return force_unicode(value.read())
Example #40
0
 def item_title(self, item):
     # Titles should be double escaped by default (see #6533)
     return escape(force_unicode(item))
Example #41
0
            response = augment_solr_response(response, collection, query)
        except RestException, e:
            try:
                message = json.loads(e.message)
                response['error'] = message['error'].get(
                    'msg', message['error']['trace'])
            except Exception, e2:
                LOG.exception('failed to extract json message: %s' %
                              force_unicode(e2))
                LOG.exception('failed to parse json response: %s' %
                              force_unicode(e))
                response['error'] = force_unicode(e)
        except Exception, e:
            raise PopupException(e, title=_('Error while accessing Solr'))

            response['error'] = force_unicode(e)
    else:
        response['error'] = _('There is no collection to search.')

    if 'error' in response:
        augment_solr_exception(response, collection)

    return JsonResponse(response)


@allow_owner_only
def save(request):
    response = {'status': -1}

    collection = json.loads(request.POST.get('collection', '{}'))
    layout = json.loads(request.POST.get('layout', '{}'))
Example #42
0
 def _generate_preview_html(self, data_string):
     """Returns html of the MarkDown file as produced by markdown."""
     # Use safe filtering against injection attacks
     return markdown.markdown(force_unicode(data_string),
                              safe_mode='escape',
                              enable_attributes=False)
Example #43
0
    def execute(self, sql, params=()):
        __traceback_hide__ = True
        start = datetime.now()
        try:
            return self.cursor.execute(sql, params)
        finally:
            stop = datetime.now()
            duration = ms_from_timedelta(stop - start)
            stacktrace = tidy_stacktrace(reversed(inspect.stack()))
            _params = ''
            try:
                _params = simplejson.dumps([force_unicode(x, strings_only=True) for x in params])
            except TypeError:
                pass # object not JSON serializable

            template_info = None
            cur_frame = sys._getframe().f_back
            try:
                while cur_frame is not None:
                    if cur_frame.f_code.co_name == 'render':
                        node = cur_frame.f_locals['self']
                        if isinstance(node, Node):
                            template_info = get_template_info(node.source)
                            break
                    cur_frame = cur_frame.f_back
            except:
                pass
            del cur_frame

            alias = getattr(self.db, 'alias', 'default')
            conn = connections[alias].connection
            # HACK: avoid imports
            if conn:
                engine = conn.__class__.__module__.split('.', 1)[0]
            else:
                engine = 'unknown'

            params = {
                'engine': engine,
                'alias': alias,
                'sql': self.db.ops.last_executed_query(self.cursor, sql, params),
                'duration': duration,
                'raw_sql': sql,
                'params': _params,
                'hash': sha_constructor(settings.SECRET_KEY + sql + _params).hexdigest(),
                'stacktrace': stacktrace,
                'start_time': start,
                'stop_time': stop,
                'is_slow': (duration > SQL_WARNING_THRESHOLD),
                'is_select': sql.lower().strip().startswith('select'),
                'template_info': template_info,
            }

            if engine == 'psycopg2':
                params.update({
                    'trans_id': self.logger.get_transaction_id(alias),
                    'trans_status': conn.get_transaction_status(),
                    'iso_level': conn.isolation_level,
                    'encoding': conn.encoding,
                })


            # We keep `sql` to maintain backwards compatibility
            self.logger.record(**params)
Example #44
0
        def changelist_view(self, request, extra_context=None):
            "The 'change list' admin view for this model."
            from django.contrib.admin.views.main import ERROR_FLAG
            opts = self.model._meta
            app_label = opts.app_label
            if not self.has_change_permission(request, None):
                raise PermissionDenied

            # Check actions to see if any are available on this changelist
            actions = self.get_actions(request)

            # Remove action checkboxes if there aren't any actions available.
            list_display = list(self.list_display)
            if not actions:
                try:
                    list_display.remove('action_checkbox')
                except ValueError:
                    pass

            CL = self.get_changelist(request)

            try:
                cl = CL(request, self.model, list_display,
                        self.list_display_links, self.list_filter,
                        self.date_hierarchy, self.search_fields,
                        self.list_select_related, self.list_per_page,
                        self.list_editable, self)
            except IncorrectLookupParameters:
                # Wacky lookup parameters were given, so redirect to the main
                # changelist page, without parameters, and pass an 'invalid=1'
                # parameter via the query string. If wacky parameters were given and
                # the 'invalid=1' parameter was already in the query string, something
                # is screwed up with the database, so display an error page.
                if ERROR_FLAG in request.GET.keys():
                    return render_to_response('admin/invalid_setup.html',
                                              {'title': _('Database error')})
                return HttpResponseRedirect(request.path + '?' + ERROR_FLAG +
                                            '=1')

            # If the request was POSTed, this might be a bulk action or a bulk edit.
            # Try to look up an action or confirmation first, but if this isn't an
            # action the POST will fall through to the bulk edit check, below.
            if actions and request.method == 'POST' and (
                    helpers.ACTION_CHECKBOX_NAME in request.POST
                    or 'index' in request.POST):
                response = self.response_action(
                    request, queryset=cl.get_query_set(request))
                if response:
                    return response

            # If we're allowing changelist editing, we need to construct a formset
            # for the changelist given all the fields to be edited. Then we'll
            # use the formset to validate/process POSTed data.
            formset = cl.formset = None

            # Handle POSTed bulk-edit data.
            if request.method == "POST" and self.list_editable:
                FormSet = self.get_changelist_formset(request)
                formset = cl.formset = FormSet(request.POST,
                                               request.FILES,
                                               queryset=cl.result_list)
                if formset.is_valid():
                    changecount = 0
                    for form in formset.forms:
                        if form.has_changed():
                            obj = self.save_form(request, form, change=True)
                            self.save_model(request, obj, form, change=True)
                            form.save_m2m()
                            change_msg = self.construct_change_message(
                                request, form, None)
                            self.log_change(request, obj, change_msg)
                            changecount += 1

                    if changecount:
                        if changecount == 1:
                            name = force_unicode(opts.verbose_name)
                        else:
                            name = force_unicode(opts.verbose_name_plural)
                        msg = ungettext(
                            "%(count)s %(name)s was changed successfully.",
                            "%(count)s %(name)s were changed successfully.",
                            changecount) % {
                                'count': changecount,
                                'name': name,
                                'obj': force_unicode(obj)
                            }
                        self.message_user(request, msg)

                    return HttpResponseRedirect(request.get_full_path())

            # Handle GET -- construct a formset for display.
            elif self.list_editable:
                FormSet = self.get_changelist_formset(request)
                formset = cl.formset = FormSet(queryset=cl.result_list)

            # Build the list of media to be used by the formset.
            if formset:
                media = self.media + formset.media
            else:
                media = self.media

            # Build the action form and populate it with available actions.
            if actions:
                action_form = self.action_form(auto_id=None)
                action_form.fields['action'].choices = self.get_action_choices(
                    request)
            else:
                action_form = None

            context = {
                'title': cl.title,
                'is_popup': cl.is_popup,
                'cl': cl,
                'media': media,
                'has_add_permission': self.has_add_permission(request),
                'root_path': self.admin_site.root_path,
                'app_label': app_label,
                'action_form': action_form,
                'actions_on_top': self.actions_on_top,
                'actions_on_bottom': self.actions_on_bottom,
            }
            context.update(extra_context or {})
            context_instance = template.RequestContext(
                request, current_app=self.admin_site.name)
            return render_to_response(self.change_list_template or [
                'admin/%s/%s/change_list.html' %
                (app_label, opts.object_name.lower()),
                'admin/%s/change_list.html' % app_label,
                'admin/change_list.html'
            ],
                                      context,
                                      context_instance=context_instance)
Example #45
0
def truncate_html_words(s, num):
    """
    Truncates html to a certain number of words (not counting tags and
    comments). Closes opened tags if they were correctly closed in the given
    html.
    """
    s = force_unicode(s)
    length = int(num)
    if length <= 0:
        return u''
    html4_singlets = ('br', 'col', 'link', 'base', 'img', 'param', 'area',
                      'hr', 'input')
    # Set up regular expressions
    re_words = re.compile(r'&.*?;|<.*?>|(\w[\w-]*)', re.U)
    re_tag = re.compile(r'<(/)?([^ ]+?)(?: (/)| .*?)?>')
    # Count non-HTML words and keep note of open tags
    pos = 0
    ellipsis_pos = 0
    words = 0
    open_tags = []
    while words <= length:
        m = re_words.search(s, pos)
        if not m:
            # Checked through whole string
            break
        pos = m.end(0)
        if m.group(1):
            # It's an actual non-HTML word
            words += 1
            if words == length:
                ellipsis_pos = pos
            continue
        # Check for tag
        tag = re_tag.match(m.group(0))
        if not tag or ellipsis_pos:
            # Don't worry about non tags or tags after our truncate point
            continue
        closing_tag, tagname, self_closing = tag.groups()
        tagname = tagname.lower()  # Element names are always case-insensitive
        if self_closing or tagname in html4_singlets:
            pass
        elif closing_tag:
            # Check for match in open tags list
            try:
                i = open_tags.index(tagname)
            except ValueError:
                pass
            else:
                # SGML: An end tag closes, back to the matching start tag, all unclosed intervening start tags with omitted end tags
                open_tags = open_tags[i + 1:]
        else:
            # Add it to the start of the open tags list
            open_tags.insert(0, tagname)
    if words <= length:
        # Don't try to close tags if we don't need to truncate
        return s
    out = s[:ellipsis_pos] + ' ...'
    # Close any tags still open
    for tag in open_tags:
        out += '</%s>' % tag
    # Return string
    return out
Example #46
0
def items_for_tree_result(cl, result, form):
    """
    Generates the actual list of data.
    """
    first = True
    pk = cl.lookup_opts.pk.attname
    for field_name in cl.list_display:
        row_class = ''
        try:
            f, attr, value = lookup_field(field_name, result, cl.model_admin)
        except (AttributeError, ObjectDoesNotExist):
            result_repr = EMPTY_CHANGELIST_VALUE
        else:
            if f is None:
                allow_tags = getattr(attr, 'allow_tags', False)
                boolean = getattr(attr, 'boolean', False)
                if boolean:
                    allow_tags = True
                    result_repr = _boolean_icon(value)
                else:
                    result_repr = smart_unicode(value)
                # Strip HTML tags in the resulting text, except if the
                # function has an "allow_tags" attribute set to True.
                if not allow_tags:
                    result_repr = escape(result_repr)
                else:
                    result_repr = mark_safe(result_repr)
            else:
                if value is None:
                    result_repr = EMPTY_CHANGELIST_VALUE
                if isinstance(f.rel, models.ManyToOneRel):
                    result_repr = escape(getattr(result, f.name))
                else:
                    result_repr = display_for_field(value, f)
                if isinstance(f, models.DateField) or isinstance(
                        f, models.TimeField):
                    row_class = ' class="nowrap"'
            if first:
                try:
                    f, attr, value = lookup_field('action_checkbox', result,
                                                  cl.model_admin)
                    result_repr = mark_safe("%s%s" % (value, result_repr))
                    if row_class:
                        row_class = "%s%s" % (row_class[:-1], ' disclosure"')
                    else:
                        row_class = ' class="disclosure"'
                except (AttributeError, ObjectDoesNotExist):
                    pass

        if force_unicode(result_repr) == '':
            result_repr = mark_safe('&nbsp;')
        # If list_display_links not defined, add the link tag to the first field
        if (first and not cl.list_display_links
            ) or field_name in cl.list_display_links:
            table_tag = 'td'  #{True:'th', False:'td'}[first]
            url = cl.url_for_result(result)
            # Convert the pk to something that can be used in Javascript.
            # Problem cases are long ints (23L) and non-ASCII strings.
            if cl.to_field:
                attr = str(cl.to_field)
            else:
                attr = pk
            value = result.serializable_value(attr)
            result_id = repr(force_unicode(value))[1:]
            first = False
            yield mark_safe(u'<%s%s><a href="%s"%s>%s</a></%s>' % \
                (table_tag, row_class, url, (cl.is_popup and ' onclick="opener.dismissRelatedLookupPopup(window, %s); return false;"' % result_id or ''), conditional_escape(result_repr), table_tag))
        else:
            # By default the fields come from ModelAdmin.list_editable, but if we pull
            # the fields out of the form instead of list_editable custom admins
            # can provide fields on a per request basis
            if form and field_name in form.fields:
                bf = form[field_name]
                result_repr = mark_safe(
                    force_unicode(bf.errors) + force_unicode(bf))
            else:
                result_repr = conditional_escape(result_repr)
            yield mark_safe(u'<td%s>%s</td>' % (row_class, result_repr))
    if form and not form[cl.model._meta.pk.name].is_hidden:
        yield mark_safe(u'<td>%s</td>' %
                        force_unicode(form[cl.model._meta.pk.name]))
Example #47
0
def normalize_newlines(text):
    return force_unicode(re.sub(r'\r\n|\r|\n', '\n', text))
Example #48
0
 def pre_save(self, model_instance, add):
     value = force_unicode(self.create_slug(model_instance, add))
     setattr(model_instance, self.attname, value)
     return value
Example #49
0
    def _reverse_with_prefix(self, lookup_view, _prefix, *args, **kwargs):
        if args and kwargs:
            raise ValueError("Don't mix *args and **kwargs in call to reverse()!")
        try:
            lookup_view = get_callable(lookup_view, True)
        except (ImportError, AttributeError), e:
            raise NoReverseMatch("Error importing '%s': %s." % (lookup_view, e))
        possibilities = self.reverse_dict.getlist(lookup_view)
        prefix_norm, prefix_args = normalize(_prefix)[0]
        for possibility, pattern, defaults in possibilities:
            for result, params in possibility:
                if args:
                    if len(args) != len(params) + len(prefix_args):
                        continue
                    unicode_args = [force_unicode(val) for val in args]
                    candidate =  (prefix_norm + result) % dict(zip(prefix_args + params, unicode_args))
                else:
                    if set(kwargs.keys() + defaults.keys()) != set(params + defaults.keys() + prefix_args):
                        continue
                    matches = True
                    for k, v in defaults.items():
                        if kwargs.get(k, v) != v:
                            matches = False
                            break
                    if not matches:
                        continue
                    unicode_kwargs = dict([(k, force_unicode(v)) for (k, v) in kwargs.items()])
                    candidate = (prefix_norm + result) % unicode_kwargs
                if re.search(u'^%s%s' % (_prefix, pattern), candidate, re.UNICODE):
                    return candidate
Example #50
0
def recapitalize(text):
    "Recapitalizes text, placing caps after end-of-sentence punctuation."
    text = force_unicode(text).lower()
    capsRE = re.compile(r'(?:^|(?<=[\.\?\!] ))([a-z])')
    text = capsRE.sub(lambda x: x.group(1).upper(), text)
    return text
Example #51
0
 def get_help_text(self):
     """Returns the help text for this step."""
     text = linebreaks(force_unicode(self.help_text))
     text += self.action.get_help_text()
     return safe(text)
Example #52
0
import re
from django.conf import settings
from django.utils.encoding import force_unicode
from django.utils.functional import allow_lazy
from django.utils.translation import ugettext_lazy

# Capitalizes the first letter of a string.
capfirst = lambda x: x and force_unicode(x)[0].upper() + force_unicode(x)[1:]
capfirst = allow_lazy(capfirst, unicode)


def wrap(text, width):
    """
    A word-wrap function that preserves existing line breaks and most spaces in
    the text. Expects that existing line breaks are posix newlines.
    """
    text = force_unicode(text)

    def _generator():
        it = iter(text.split(' '))
        word = it.next()
        yield word
        pos = len(word) - word.rfind('\n') - 1
        for word in it:
            if "\n" in word:
                lines = word.split('\n')
            else:
                lines = (word, )
            pos += len(lines[0]) + 1
            if pos > width:
                yield '\n'
Example #53
0
def result_headers(cl):
    lookup_opts = cl.lookup_opts

    for i, field_name in enumerate(cl.list_display):
        attr = None
        try:
            f = lookup_opts.get_field(field_name)
            admin_order_field = None
        except models.FieldDoesNotExist:
            # For non-field list_display values, check for the function
            # attribute "short_description". If that doesn't exist, fall back
            # to the method name. And __str__ and __unicode__ are special-cases.
            if field_name == '__unicode__':
                header = force_unicode(lookup_opts.verbose_name)
            elif field_name == '__str__':
                header = smart_str(lookup_opts.verbose_name)
            else:
                if callable(field_name):
                    attr = field_name  # field_name can be a callable
                else:
                    try:
                        attr = getattr(cl.model_admin, field_name)
                    except AttributeError:
                        try:
                            attr = getattr(cl.model, field_name)
                        except AttributeError:
                            raise AttributeError, \
                                "'%s' model or '%s' objects have no attribute '%s'" % \
                                    (lookup_opts.object_name, cl.model_admin.__class__, field_name)

                try:
                    header = attr.short_description
                except AttributeError:
                    if callable(field_name):
                        header = field_name.__name__
                    else:
                        header = field_name
                    header = header.replace('_', ' ')

            # It is a non-field, but perhaps one that is sortable
            admin_order_field = getattr(attr, "admin_order_field", None)
            if not admin_order_field:
                yield {"text": header}
                continue

            # So this _is_ a sortable non-field.  Go to the yield
            # after the else clause.
        else:
            header = f.verbose_name

        th_classes = []
        new_order_type = 'asc'
        if field_name == cl.order_field or admin_order_field == cl.order_field:
            th_classes.append('sorted %sending' % cl.order_type.lower())
            new_order_type = {
                'asc': 'desc',
                'desc': 'asc'
            }[cl.order_type.lower()]

        yield {
            "text":
            header,
            "sortable":
            True,
            "url":
            cl.get_query_string({
                ORDER_VAR: i,
                ORDER_TYPE_VAR: new_order_type
            }),
            "class_attrib":
            mark_safe(th_classes and ' class="%s"' % ' '.join(th_classes)
                      or '')
        }
Example #54
0
 def __init__(self, parent_model, admin_site):
     super(SchemaModelInline, self).__init__(parent_model, admin_site)
     if self.collapse:
         self.verbose_name_plural = '_{}'.format(force_unicode(self.verbose_name_plural))
     # Show m2m fields as horizontal filter widget unless they have a custom widget:
     self.filter_horizontal = self.list_m2m_fields_without_custom_widget(self.model)
Example #55
0
def _string_concat(*strings):
    """
    Lazy variant of string concatenation, needed for translations that are
    constructed from multiple parts.
    """
    return u''.join([force_unicode(s) for s in strings])
Example #56
0
 def __unicode__(self):
     return force_unicode(self.name)
Example #57
0
    def clone_view(self, request, object_id, extra_context=None):
        "The 'clone' admin view for this model."

        exclude = [TrialInBucket, Illustration]
        model = self.model
        opts = model._meta
        app_label = opts.app_label

        try:
            obj = self.queryset(request).get(pk=unquote(object_id))
        except model.DoesNotExist:
            # Don't raise Http404 just yet, because we haven't checked
            # permissions yet. We don't want an unauthenticated user to be able
            # to determine whether a given object exists.
            obj = None

        if not self.has_add_permission(request):
            raise PermissionDenied

        if obj is None:
            raise Http404(_('%(name)s object with primary key %(key)r does not exist.') % {'name': force_unicode(opts.verbose_name), 'key': escape(object_id)})

        if not obj.cloneable():
            message = "Not cloneable!"
            request.user.message_set.create(message=message)
            c = RequestContext(request, {})
            return render_to_response('error.html', c)

        cloned_obj = duplicate(obj,exclude)
        cloned_obj.save()

        msg = _('The %(name)s "%(obj)s" was cloned successfully. You may edit it now below.') % {'name': force_unicode(opts.verbose_name), 'obj': obj}
        self.message_user(request, msg)
        post_url="/admin/%s/%s/%d/edit" % (app_label, opts.object_name.lower(),cloned_obj.pk)
        if request.GET.has_key('r'):
            return_to = request.GET['r']
            if return_to !=None and return_to !="":
                post_url=return_to
        return HttpResponseRedirect(post_url)
Example #58
0
def items_for_result(cl, result):
    first = True
    pk = cl.lookup_opts.pk.attname
    for field_name in cl.list_display:
        row_class = ''
        try:
            f = cl.lookup_opts.get_field(field_name)
        except models.FieldDoesNotExist:
            # For non-field list_display values, the value is either a method,
            # property or returned via a callable.
            try:
                if callable(field_name):
                    attr = field_name
                    value = attr(result)
                elif hasattr(cl.model_admin, field_name) and \
                   not field_name == '__str__' and not field_name == '__unicode__':
                    attr = getattr(cl.model_admin, field_name)
                    value = attr(result)
                else:
                    attr = getattr(result, field_name)
                    if callable(attr):
                        value = attr()
                    else:
                        value = attr
                allow_tags = getattr(attr, 'allow_tags', False)
                boolean = getattr(attr, 'boolean', False)
                if boolean:
                    allow_tags = True
                    result_repr = _boolean_icon(value)
                else:
                    result_repr = smart_unicode(value)
            except (AttributeError, ObjectDoesNotExist):
                result_repr = EMPTY_CHANGELIST_VALUE
            else:
                # Strip HTML tags in the resulting text, except if the
                # function has an "allow_tags" attribute set to True.
                if not allow_tags:
                    result_repr = escape(result_repr)
                else:
                    result_repr = mark_safe(result_repr)
        else:
            field_val = getattr(result, f.attname)

            if isinstance(f.rel, models.ManyToOneRel):
                if field_val is not None:
                    result_repr = escape(getattr(result, f.name))
                else:
                    result_repr = EMPTY_CHANGELIST_VALUE
            # Dates and times are special: They're formatted in a certain way.
            elif isinstance(f, models.DateField) or isinstance(
                    f, models.TimeField):
                if field_val:
                    (date_format, datetime_format,
                     time_format) = get_date_formats()
                    if isinstance(f, models.DateTimeField):
                        result_repr = capfirst(
                            dateformat.format(field_val, datetime_format))
                    elif isinstance(f, models.TimeField):
                        result_repr = capfirst(
                            dateformat.time_format(field_val, time_format))
                    else:
                        result_repr = capfirst(
                            dateformat.format(field_val, date_format))
                else:
                    result_repr = EMPTY_CHANGELIST_VALUE
                row_class = ' class="nowrap"'
            # Booleans are special: We use images.
            elif isinstance(f, models.BooleanField) or isinstance(
                    f, models.NullBooleanField):
                result_repr = _boolean_icon(field_val)
            # DecimalFields are special: Zero-pad the decimals.
            elif isinstance(f, models.DecimalField):
                if field_val is not None:
                    result_repr = ('%%.%sf' % f.decimal_places) % field_val
                else:
                    result_repr = EMPTY_CHANGELIST_VALUE
            # Fields with choices are special: Use the representation
            # of the choice.
            elif f.flatchoices:
                result_repr = dict(f.flatchoices).get(field_val,
                                                      EMPTY_CHANGELIST_VALUE)
            else:
                result_repr = escape(field_val)
        if force_unicode(result_repr) == '':
            result_repr = mark_safe('&nbsp;')
        # If list_display_links not defined, add the link tag to the first field
        if (first and not cl.list_display_links
            ) or field_name in cl.list_display_links:
            table_tag = {True: 'th', False: 'td'}[first]
            first = False
            url = cl.url_for_result(result)
            # Convert the pk to something that can be used in Javascript.
            # Problem cases are long ints (23L) and non-ASCII strings.
            if cl.to_field:
                attr = str(cl.to_field)
            else:
                attr = pk
            value = result.serializable_value(attr)
            result_id = repr(force_unicode(value))[1:]
            yield mark_safe(u'<%s%s><a href="%s"%s>%s</a></%s>' % \
                (table_tag, row_class, url, (cl.is_popup and ' onclick="opener.dismissRelatedLookupPopup(window, %s); return false;"' % result_id or ''), conditional_escape(result_repr), table_tag))
        else:
            yield mark_safe(u'<td%s>%s</td>' %
                            (row_class, conditional_escape(result_repr)))
Example #59
0
 def save(self, force_insert=False, force_update=False, using=None):
     self.content_show = mark_safe(
         markdown.markdown(force_unicode(self.content), ['codehilite'],
                           safe_mode='escape'))
     super(Wiki, self).save()
Example #60
0
    def view_view(self, request, object_id, extra_context=None):
        "The 'View' admin view for this model."

        obj = self.get_object(request, unquote(object_id))
        if self.has_change_permission(request, obj):
            request.feed_upload_status.update_with_object(obj, fail_silently=True)

        model = self.model
        opts = model._meta

        try:
            obj = self.queryset(request).get(pk=unquote(object_id))
        except model.DoesNotExist:
            # Don't raise Http404 just yet, because we haven't checked
            # permissions yet. We don't want an unauthenticated user to be able
            # to determine whether a given object exists.
            obj = None

        #if not self.has_change_permission(request, obj):
        #    raise PermissionDenied

        if obj is None:
            raise Http404(_('%(name)s object with primary key %(key)r does not exist.') % {'name': force_unicode(opts.verbose_name), 'key': escape(object_id)})

        if request.method == 'POST' and request.POST.has_key("_saveasnew"):
            return self.add_view(request, form_url='../add/')

        ModelForm = self.get_form(request, obj)
        formsets = []

        form = ModelForm(instance=obj)

        prefixes = {}
        for FormSet in self.get_view_formsets(request, obj):
            prefix = FormSet.get_default_prefix()
            prefixes[prefix] = prefixes.get(prefix, 0) + 1
            if prefixes[prefix] != 1:
                prefix = "%s-%s" % (prefix, prefixes[prefix])
            formset = FormSet(instance=obj, prefix=prefix)
            formsets.append(formset)

        adminForm = helpers.AdminForm(form, self.get_fieldsets(request, obj), self.prepopulated_fields)
        media = self.media + adminForm.media

        inline_admin_formsets = []
        for inline, formset in zip(self.view_inline_instances, formsets):
            fieldsets = list(inline.get_fieldsets(request, obj))
            inline_admin_formset = helpers.InlineAdminFormSet(inline, formset, fieldsets)
            inline_admin_formsets.append(inline_admin_formset)
            media = media + inline_admin_formset.media

        registry = []
        for r in self.admin_site._registry:
            registry.append(r._meta.verbose_name.lower())

        context = {
            'title': _('View %s') % force_unicode(opts.verbose_name),
            'adminform': adminForm,
            'object_id': object_id,
            'original': obj,
            'cloneable': obj.cloneable(),
            'is_popup': request.REQUEST.has_key('_popup'),
            'tabbed': self.tabbed,
            'tab_name': self.tab_name,
            'media': mark_safe(media),
            'inline_admin_formsets': inline_admin_formsets,
            'errors': helpers.AdminErrorList(form, formsets),
            'root_path': reverse('admin:index'),
            'app_label': opts.app_label,
            'registry': registry,
        }

        context.update(extra_context or {})
        return self.render_view_view(request, context, obj=obj)