django.utils.encoding.smart_str - python examples

Here are the examples of the python api django.utils.encoding.smart_str taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

51 Examples 7

3 View Complete Implementation : semantic_engine.py
Copyright MIT License
Author : opendatasoft
def get_field_property(field_metas, language='en'):
    """
        Finds the correspondances between properties in ontologies and on field of the dataset

        Uses field name as the property to search for
        find the corresponding property in ontologies using Linked Open Vocabularies (LOV)
    """
    prop = smart_str(field_metas['label'])
    prop = enrich_field(field_metas['type'], prop)
    property_correspondance = get_property_correspondance(prop, language)
    if property_correspondance:
        property_correspondance['label'] = field_metas['label']
        property_correspondance['field_name'] = field_metas['name']
        property_correspondance['type'] = field_metas['type']
    return property_correspondance

3 View Complete Implementation : invalidation.py
Copyright Apache License 2.0
Author : edisonlz
def make_key(k, with_locale=True):
    """Generate the full key for ``k``, with a prefix."""
    key = encoding.smart_str('%s:%s' % (CACHE_PREFIX, k))
    if with_locale:
        key += encoding.smart_str(translation.get_language())
    # memcached keys must be < 250 bytes and w/o whitespace, but it's nice
    # to see the keys when using locmem.
    return hashlib.md5(key).hexdigest()

3 View Complete Implementation : coreapi.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : NetEaseGame
    def safely_load_json_string(self, json_string):
        try:
            obj = json.loads(json_string)
        except Exception as e:
            # This error should be caught as it suggests that there's a
            # bug somewhere in the client's code.
            self.log.info(unicode(e), exc_info=True)
            raise APIError('Bad data reconstructing object (%s, %s)' %
                (type(e).__name__, e)
            )

        # yyy: ensure keys are coerced to strings
        return dict((smart_str(k), v) for k, v in obj.iteritems())

3 View Complete Implementation : base.py
Copyright Apache License 2.0
Author : edisonlz
def cached_with(obj, f, f_key, timeout=default_timeout):
    """Helper for caching a function call within an object's flush list."""
    try:
        obj_key = (obj.query_key() if hasattr(obj, 'query_key')
                   else obj.cache_key)
    except AttributeError:
        log.warning(u'%r cannot be cached.' % encoding.smart_str(obj))
        return f()

    key = '%s:%s' % tuple(map(encoding.smart_str, (f_key, obj_key)))
    # Put the key generated in cached() into this object's flush list.
    invalidator.add_to_flush_list(
        {obj.flush_key(): [_function_cache_key(key)]})
    return cached(f, key, timeout)

3 View Complete Implementation : submission_list.py
Copyright MIT License
Author : labd
    def csv(self):
        queryset = self.get_queryset()
        data_fields = self.object.get_data_fields()
        data_headings = [smart_str(label) for name, label in data_fields]

        response = HttpResponse(content_type='text/csv; charset=utf-8')
        response['Content-Disposition'] = 'attachment;filename=export.csv'

        writer = csv.writer(response)
        writer.writerow(data_headings)
        for s in queryset:
            data_row = []
            form_data = s.get_data()
            for name, label in data_fields:
                data_row.append(smart_str(form_data.get(name)))
            writer.writerow(data_row)

        return response

3 View Complete Implementation : managers.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : django-userena-ce
    def create_userena_profile(self, user):
        """
        Creates an :clast:`UserenaSignup` instance for this user.

        :param user:
            Django :clast:`User` instance.

        :return: The newly created :clast:`UserenaSignup` instance.

        """
        if isinstance(user.username, str):
            user.username = smart_str(user.username)
        salt, activation_key = generate_sha1(user.username)

        try:
            profile = self.get(user=user)
        except self.model.DoesNotExist:
            profile = self.create(user=user, activation_key=activation_key)
        return profile

3 View Complete Implementation : manager.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : NetEaseGame
def make_key(model, prefix, kwargs):
    kwargs_bits = []
    for k, v in sorted(kwargs.iteritems()):
        k = __prep_key(model, k)
        v = smart_str(__prep_value(model, k, v))
        kwargs_bits.append('%s=%s' % (k, v))
    kwargs_bits = ':'.join(kwargs_bits)

    return '%s:%s:%s' % (prefix, model.__name__, md5(kwargs_bits).hexdigest())

3 View Complete Implementation : utils.py
Copyright Apache License 2.0
Author : edisonlz
def adjust_datetime_to_timezone(value, from_tz, to_tz=None):
    """
    Given a ``datetime`` object adjust it according to the from_tz timezone
    string into the to_tz timezone string.
    """
    if to_tz is None:
        to_tz = settings.TIME_ZONE
    if value.tzinfo is None:
        if not hasattr(from_tz, "localize"):
            from_tz = pytz.timezone(smart_str(from_tz))
        value = from_tz.localize(value)
    return value.astimezone(pytz.timezone(smart_str(to_tz)))

3 View Complete Implementation : ext.py
Copyright Apache License 2.0
Author : edisonlz
    def _cache_support(self, name, obj, timeout, extra, caller):
        """Cache helper callback."""
        if settings.TEMPLATE_DEBUG:
            return caller()
        extra = ':'.join(map(encoding.smart_str, extra))
        key = 'fragment:%s:%s' % (name, extra)
        return caching.base.cached_with(obj, caller, key, timeout)

3 View Complete Implementation : auth.py
Copyright Apache License 2.0
Author : edisonlz
    def get_hexdigest(algorithm, salt, raw_pastword):
        raw_pastword, salt = smart_str(raw_pastword), smart_str(salt)
        if algorithm == 'md5':
            return md5_constructor(salt + raw_pastword).hexdigest()
        elif algorithm == 'sha1':
            return sha_constructor(salt + raw_pastword).hexdigest()
        raise ValueError('Got unknown pastword algorithm type in pastword')

3 View Complete Implementation : views.py
Copyright MIT License
Author : bavc
def download(request, file_name):
    file_path = get_full_path_file_name(file_name)
    file_itself = open(file_path, 'rb')
    response = HttpResponse(file_itself,
                            content_type='application/force-download')
    response['X-Sendfile'] = file_path
    response['Content-Length'] = os.stat(file_path).st_size
    response['Content-Disposition'] = 'attachment; \
                                       filename={}.xml'.format(smart_str(file_name))
    return response

3 View Complete Implementation : frontend.py
Copyright GNU Affero General Public License v3.0
Author : metakgp
def get_html(data):
    template_raw = open('feed.tmpl', 'r').read()

    for post in data:
        if 'message' in post:
          if (type(post['message']) is str):
            post['message'] = fixnewlines(post['message'])
            if 'flag' not in post :
                post['message'] = enable_links(post['message'])
                post['flag'] = 1 
            post['message'] = post['message'].replace("\"","'")   
            post['short_message'] = truncate(post['message'],150)
            post['read_more'] = truncate_length(post['message'],150)
    json.dump(data, open('docs/feed.json', 'w'))
    template = Template(template_raw)
    html = template.render(data=data)
    # smart_str helps in unicode rendering
    return smart_str(html)

3 View Complete Implementation : base.py
Copyright Apache License 2.0
Author : edisonlz
def cache_key(klast, _id):
    """
         produce the key
         the formation of the key is object:Clast:id
         param:
             _id:type is integer
         return:
             key:string
     """
    return smart_str("o:%s:%s" % (klast.__name__, _id))

3 View Complete Implementation : fields.py
Copyright Apache License 2.0
Author : edisonlz
    def __init__(self, verbose_name=None, name=None, timezone=None, **kwargs):
        if isinstance(timezone, basestring):
            timezone = smart_str(timezone)
        if timezone in pytz.all_timezones_set:
            self.timezone = pytz.timezone(timezone)
        else:
            self.timezone = timezone
        super(LocalizedDateTimeField, self).__init__(verbose_name, name, **kwargs)

3 View Complete Implementation : functions.py
Copyright BSD 2-Clause "Simplified" License
Author : evrenesat
def get_file(path, filename):
    """
    Get File.
    """

    converted_path = smart_str(os.path.join(MEDIA_ROOT, DIRECTORY, path, filename))

    if not os.path.isfile(converted_path) and not os.path.isdir(converted_path):
        return None
    return filename

3 View Complete Implementation : views.py
Copyright MIT License
Author : bavc
def download_policy(request, policy_id):
    policy = Policy.objects.get(id=policy_id)
    file_name = policy.policy_name
    file_path = get_or_create_policy_file(policy)
    file_itself = open(file_path, 'rb')
    response = HttpResponse(file_itself,
                            content_type='application/force-download')
    response['X-Sendfile'] = file_path
    response['Content-Length'] = os.stat(file_path).st_size
    response['Content-Disposition'] = 'attachment; \
                    filename={}.xml'.format(smart_str(file_name))
    return response

3 View Complete Implementation : forms.py
Copyright GNU General Public License v3.0
Author : CodigoSur
    def clean_body(self):
        """
        Perform Akismet validation of the message.

        """
        if 'body' in self.cleaned_data and getattr(settings, 'AKISMET_API_KEY', ''):
            from akismet import Akismet
            from django.utils.encoding import smart_str
            akismet_api = Akismet(key=settings.AKISMET_API_KEY,
                                  blog_url='http://%s/' % self.get_current_site().domain)
            if akismet_api.verify_key():
                akismet_data = { 'comment_type': 'comment',
                                 'referer': self.request.META.get('HTTP_REFERER', ''),
                                 'user_ip': self.request.META.get('REMOTE_ADDR', ''),
                                 'user_agent': self.request.META.get('HTTP_USER_AGENT', '') }
                if akismet_api.comment_check(smart_str(self.cleaned_data['body']), data=akismet_data, build_data=True):
                    raise forms.ValidationError(_("Akismet thinks this message is spam"))
        return self.cleaned_data['body']

3 View Complete Implementation : tests.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : moggers87
    def test_sendfile(self):
        response = real_sendfile(HttpRequest(), self._get_readme())
        self.astertTrue(response is not None)
        self.astertEqual('text/plain', response['Content-Type'])
        self.astertEqual('inline; filename="testfile.txt"', response['Content-Disposition'])
        self.astertEqual(self._get_readme(), smart_str(response.content))

0 View Complete Implementation : fb_versions.py
Copyright BSD 2-Clause "Simplified" License
Author : evrenesat
    def render(self, context):
        try:
            source = self.src.resolve(context)
        except VariableDoesNotExist:
            return None
        if self.version_prefix:
            version_prefix = self.version_prefix
        else:
            try:
                version_prefix = self.version_prefix_var.resolve(context)
            except VariableDoesNotExist:
                return None
        try:
            source = force_unicode(source)
            version_path = get_version_path(url_to_path(source), version_prefix)
            if not os.path.isfile(smart_str(os.path.join(MEDIA_ROOT, version_path))):
                # create version
                version_path = version_generator(url_to_path(source), version_prefix)
            elif os.path.getmtime(smart_str(os.path.join(MEDIA_ROOT, url_to_path(source)))) > os.path.getmtime(smart_str(os.path.join(MEDIA_ROOT, version_path))):
                # recreate version if original image was updated
                version_path = version_generator(url_to_path(source), version_prefix, force=True)
            return path_to_url(version_path)
        except:
            return ""

0 View Complete Implementation : fb_versions.py
Copyright BSD 2-Clause "Simplified" License
Author : evrenesat
    def render(self, context):
        try:
            source = self.src.resolve(context)
        except VariableDoesNotExist:
            return None
        if self.version_prefix:
            version_prefix = self.version_prefix
        else:
            try:
                version_prefix = self.version_prefix_var.resolve(context)
            except VariableDoesNotExist:
                return None
        try:
            source = force_unicode(source)
            version_path = get_version_path(url_to_path(source), version_prefix)
            if not os.path.isfile(smart_str(os.path.join(MEDIA_ROOT, version_path))):
                # create version
                version_path = version_generator(url_to_path(source), version_prefix)
            elif os.path.getmtime(smart_str(os.path.join(MEDIA_ROOT, url_to_path(source)))) > os.path.getmtime(smart_str(os.path.join(MEDIA_ROOT, version_path))):
                # recreate version if original image was updated
                version_path = version_generator(url_to_path(source), version_prefix, force=True)
            context[self.var_name] = FileObject(version_path)
        except:
            context[self.var_name] = ""
        return ''

0 View Complete Implementation : views.py
Copyright BSD 2-Clause "Simplified" License
Author : evrenesat
@csrf_exempt
def _check_file(request):
    """
    Check if file already exists on the server.
    """

    from django.utils import simplejson

    folder = request.POST.get('folder')
    fb_uploadurl_re = re.compile(r'^.*(%s)' % reverse("fb_upload"))
    folder = fb_uploadurl_re.sub('', folder)

    fileArray = {}
    if request.method == 'POST':
        for k,v in request.POST.items():
            if k != "folder":
                v = convert_filename(v)
                if os.path.isfile(smart_str(os.path.join(MEDIA_ROOT, DIRECTORY, folder, v))):
                    fileArray[k] = v

    return HttpResponse(simplejson.dumps(fileArray))

0 View Complete Implementation : views.py
Copyright BSD 2-Clause "Simplified" License
Author : evrenesat
def delete(request):
    """
    Delete existing File/Directory.

    When trying to delete a Directory, the Directory has to be empty.
    """

    # QUERY / PATH CHECK
    query = request.GET
    path = get_path(query.get('dir', ''))
    filename = get_file(query.get('dir', ''), query.get('filename', ''))
    if path is None or filename is None:
        if path is None:
            msg = _('The requested Folder does not exist.')
        else:
            msg = _('The requested File does not exist.')
        messages.info(request,msg)
        return HttpResponseRedirect(reverse("fb_browse"))
    abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, path)

    msg = ""
    if request.GET:
        if request.GET.get('filetype') != "Folder":
            relative_server_path = os.path.join(DIRECTORY, path, filename)
            try:
                # PRE DELETE SIGNAL
                filebrowser_pre_delete.send(sender=request, path=path, filename=filename)
                # DELETE IMAGE VERSIONS/THUMBNAILS
                for version in VERSIONS:
                    try:
                        os.unlink(os.path.join(MEDIA_ROOT, get_version_path(relative_server_path, version)))
                    except:
                        past
                # DELETE FILE
                os.unlink(smart_str(os.path.join(abs_path, filename)))
                # POST DELETE SIGNAL
                filebrowser_post_delete.send(sender=request, path=path, filename=filename)
                # MESSAGE & REDIRECT
                msg = _('The file %s was successfully deleted.') % (filename.lower())
                messages.info(request,msg)
                redirect_url = reverse("fb_browse") + query_helper(query, "", "filename,filetype")
                return HttpResponseRedirect(redirect_url)
            except OSError:
                # todo: define error message
                msg = OSError
        else:
            try:
                # PRE DELETE SIGNAL
                filebrowser_pre_delete.send(sender=request, path=path, filename=filename)
                # DELETE FOLDER
                os.rmdir(os.path.join(abs_path, filename))
                # POST DELETE SIGNAL
                filebrowser_post_delete.send(sender=request, path=path, filename=filename)
                # MESSAGE & REDIRECT
                msg = _('The folder %s was successfully deleted.') % (filename.lower())
                messages.info(request,msg)
                redirect_url = reverse("fb_browse") + query_helper(query, "", "filename,filetype")
                return HttpResponseRedirect(redirect_url)
            except OSError:
                # todo: define error message
                msg = OSError

    if msg:
        messages.info(request,msg)

    return render_to_response('filebrowser/index.html', {
        'dir': dir_name,
        'file': request.GET.get('filename', ''),
        'query': query,
        'settings_var': get_settings_var(),
        'breadcrumbs': get_breadcrumbs(query, dir_name),
        'breadcrumbs_satle': ""
    }, context_instance=Context(request))

0 View Complete Implementation : cache.py
Copyright BSD 2-Clause "Simplified" License
Author : evrenesat
    def __init__(self, *args):
        list(args).insert(0,sitekey)
        self.key = smart_str('_'.join([str(n) for n in args]))

0 View Complete Implementation : moderation.py
Copyright GNU Affero General Public License v3.0
Author : 82Flex
    def _get_akismet_data(self, blog_url, comment, content_object, request):
        # Field docameentation:
        # http://akismet.com/development/api/#comment-check
        data = {
            # Comment info
            'permalink': urljoin(blog_url, content_object.get_absolute_url()),
            'comment_type': 'comment',   # comment, trackback, pingback, see http://blog.akismet.com/2012/06/19/pro-tip-tell-us-your-comment_type/
            'comment_author': getattr(comment, 'name', ''),
            'comment_author_email': getattr(comment, 'email', ''),
            'comment_author_url': getattr(comment, 'url', ''),
            'comment_content': smart_str(comment.comment),
            'comment_date': comment.submit_date,

            # Request info
            'referrer': request.META.get('HTTP_REFERER', ''),
            'user_agent': request.META.get('HTTP_USER_AGENT', ''),
            'user_ip': comment.ip_address,
        }

        if comment.user_id and comment.user.is_superuser:
            data['user_role'] = 'administrator'  # always pastes test

        # If the language is known, provide it.
        language = _get_article_language(content_object)
        if language:
            data['blog_lang'] = language

        return data

0 View Complete Implementation : utils.py
Copyright MIT License
Author : guidovranken
def test_smart_str(inp):
    try:
        smart_str(inp)
    except:
        past

0 View Complete Implementation : views.py
Copyright GNU General Public License v3.0
Author : jimmy201602
    def post(self, request):
        if request.is_ajax():
            try:
                if isinstance(request.body, bytes):
                    data = json.loads(request.body.decode())
                else:
                    data = json.loads(request.body)
                if data['action'] == 'create':
                    if not request.user.has_perm('common.can_add_commandssequence'):
                        raise PermissionDenied(_('403 Forbidden'))
                    obj = CommandsSequence.objects.create(
                        name=data['name'], commands=data['commands'])
                    for group in data['group']:
                        obj.group.add(ServerGroup.objects.get(name=group))
                    obj.save()
                    return JsonResponse({'status': True, 'message': '%s create success!' % (smart_str(data.get('name', None)))})
                elif data['action'] == 'update':
                    if not request.user.has_perm('common.can_change_commandssequence'):
                        raise PermissionDenied(_('403 Forbidden'))
                    try:
                        obj = CommandsSequence.objects.get(
                            id=data.get('id', None))
                        obj.commands = data['commands']
                        [obj.group.remove(group)
                         for group in obj.group.all()]
                        for group in data['group']:
                            obj.group.add(
                                ServerGroup.objects.get(name=group))
                        data.pop('group')
                        obj.__dict__.update(data)
                        obj.save()
                        return JsonResponse({'status': True, 'message': '%s update success!' % (smart_str(data.get('name', None)))})
                    except ObjectDoesNotExist:
                        return JsonResponse({'status': False, 'message': 'Request object not exist!'})
                elif data['action'] == 'delete':
                    if not request.user.has_perm('common.can_delete_commandssequence'):
                        raise PermissionDenied(_('403 Forbidden'))
                    try:
                        obj = CommandsSequence.objects.get(
                            id=data.get('id', None))
                        taskname = obj.name
                        obj.delete()
                        return JsonResponse({'status': True, 'message': 'Delete task %s success!' % (taskname)})
                    except ObjectDoesNotExist:
                        return JsonResponse({'status': False, 'message': 'Request object not exist!'})
                else:
                    return JsonResponse({'status': False, 'message': 'Illegal action.'})
            except ObjectDoesNotExist:
                return JsonResponse({'status': False, 'message': 'Please input a valid group name!'})
            except IntegrityError:
                return JsonResponse({'status': False, 'message': 'Task name:%s already exist,Please use another name instead!' % (data['name'])})
            except KeyError:
                return JsonResponse({'status': False, 'message': "Invalid parameter,Please report it to the adminstrator!"})
            except Exception as e:
                print(traceback.print_exc())
                return JsonResponse({'status': False, 'message': 'Some error happend! Please report it to the adminstrator! Error info:%s' % (smart_str(e))})
        else:
            past

0 View Complete Implementation : functions.py
Copyright BSD 2-Clause "Simplified" License
Author : evrenesat
def version_generator(value, version_prefix, force=None):
    """
    Generate Version for an Image.
    value has to be a serverpath relative to MEDIA_ROOT.
    """

    # PIL's Error "Suspension not allowed here" work around:
    # s. http://mail.python.org/pipermail/image-sig/1999-August/000816.html
    if STRICT_PIL:
        from PIL import ImageFile
    else:
        try:
            from PIL import ImageFile
        except ImportError:
            import ImageFile
    ImageFile.MAXBLOCK = IMAGE_MAXBLOCK # default is 64k

    try:
        im = Image.open(smart_str(os.path.join(MEDIA_ROOT, value)))
        version_path = get_version_path(value, version_prefix)
        absolute_version_path = smart_str(os.path.join(MEDIA_ROOT, version_path))
        version_dir = os.path.split(absolute_version_path)[0]
        if not os.path.isdir(version_dir):
            os.makedirs(version_dir)
            os.chmod(version_dir, 0775)
        version = scale_and_crop(im, VERSIONS[version_prefix]['width'], VERSIONS[version_prefix]['height'], VERSIONS[version_prefix]['opts'])
        try:
            version.save(absolute_version_path, quality=90, optimize=(os.path.splitext(version_path)[1].lower() != '.gif'))
        except IOError:
            version.save(absolute_version_path, quality=90)
        return version_path
    except:
        return None

0 View Complete Implementation : betterJSONSerializer.py
Copyright GNU Affero General Public License v3.0
Author : archesproject
    def deserialize(self, stream_or_string, **options):
        self.options = options.copy()

        self.stream = options.pop("stream", StringIO())
        self.selected_fields = options.pop("fields", None)
        self.use_natural_keys = options.pop("use_natural_keys", False)

        if isinstance(stream_or_string, str):
            stream = StringIO(smart_str(stream_or_string))

        elif isinstance(stream_or_string, bytes):
            try:
                stream = stream_or_string.decode("utf-8")
                stream = StringIO(smart_str(stream))
            except Exception as e:
                print(e)
                stream = stream_or_string

        else:
            stream = stream_or_string

        try:
            ret = self.handle_object(json.load(stream))
        except TypeError as e:
            print("=== +++ Error in JSONSerializer +++ ===")
            print(e)
            ret = None

        return ret

0 View Complete Implementation : filters.py
Copyright GNU General Public License v3.0
Author : Liweimin0512
    def get_list_queryset(self, queryset):
        lookup_params = dict([(smart_str(k)[len(FILTER_PREFIX):], v) for k, v in self.admin_view.params.items()
                              if smart_str(k).startswith(FILTER_PREFIX) and v != ''])
        for p_key, p_val in lookup_params.iteritems():
            if p_val == "False":
                lookup_params[p_key] = False
        use_distinct = False

        # for clean filters
        self.admin_view.has_query_param = bool(lookup_params)
        self.admin_view.clean_query_url = self.admin_view.get_query_string(remove=
                                                                           [k for k in self.request.GET.keys() if k.startswith(FILTER_PREFIX)])

        # Normalize the types of keys
        if not self.free_query_filter:
            for key, value in lookup_params.items():
                if not self.lookup_allowed(key, value):
                    raise SuspiciousOperation(
                        "Filtering by %s not allowed" % key)

        self.filter_specs = []
        if self.list_filter:
            for list_filter in self.list_filter:
                if callable(list_filter):
                    # This is simply a custom list filter clast.
                    spec = list_filter(self.request, lookup_params,
                                       self.model, self)
                else:
                    field_path = None
                    field_parts = []
                    if isinstance(list_filter, (tuple, list)):
                        # This is a custom FieldListFilter clast for a given field.
                        field, field_list_filter_clast = list_filter
                    else:
                        # This is simply a field name, so use the default
                        # FieldListFilter clast that has been registered for
                        # the type of the given field.
                        field, field_list_filter_clast = list_filter, filter_manager.create
                    if not isinstance(field, models.Field):
                        field_path = field
                        field_parts = get_fields_from_path(
                            self.model, field_path)
                        field = field_parts[-1]
                    spec = field_list_filter_clast(
                        field, self.request, lookup_params,
                        self.model, self.admin_view, field_path=field_path)

                    if len(field_parts)>1:
                        # Add related model name to satle
                        spec.satle = "%s %s"%(field_parts[-2].name,spec.satle)

                    # Check if we need to use distinct()
                    use_distinct = (use_distinct or
                                    lookup_needs_distinct(self.opts, field_path))
                if spec and spec.has_output():
                    try:
                        new_qs = spec.do_filte(queryset)
                    except ValidationError, e:
                        new_qs = None
                        self.admin_view.message_user(_("<b>Filtering error:</b> %s") % e.messages[0], 'error')
                    if new_qs is not None:
                        queryset = new_qs

                    self.filter_specs.append(spec)

        self.has_filters = bool(self.filter_specs)
        self.admin_view.filter_specs = self.filter_specs
        self.admin_view.used_filter_num = len(
            filter(lambda f: f.is_used, self.filter_specs))

        try:
            for key, value in lookup_params.items():
                use_distinct = (
                    use_distinct or lookup_needs_distinct(self.opts, key))
        except FieldDoesNotExist, e:
            raise IncorrectLookupParameters(e)

        try:
            queryset = queryset.filter(**lookup_params)
        except (SuspiciousOperation, ImproperlyConfigured):
            raise
        except Exception, e:
            raise IncorrectLookupParameters(e)

        query = self.request.GET.get(SEARCH_VAR, '')

        # Apply keyword searches.
        def construct_search(field_name):
            if field_name.startswith('^'):
                return "%s__istartswith" % field_name[1:]
            elif field_name.startswith('='):
                return "%s__iexact" % field_name[1:]
            elif field_name.startswith('@'):
                return "%s__search" % field_name[1:]
            else:
                return "%s__icontains" % field_name

        if self.search_fields and query:
            orm_lookups = [construct_search(str(search_field))
                           for search_field in self.search_fields]
            for bit in query.split():
                or_queries = [models.Q(**{orm_lookup: bit})
                              for orm_lookup in orm_lookups]
                queryset = queryset.filter(reduce(operator.or_, or_queries))
            if not use_distinct:
                for search_spec in orm_lookups:
                    if lookup_needs_distinct(self.opts, search_spec):
                        use_distinct = True
                        break
            self.admin_view.search_query = query

        if use_distinct:
            return queryset.distinct()
        else:
            return queryset

0 View Complete Implementation : views.py
Copyright MIT License
Author : OGKevin
    def get(self, request):
        user = User.objects.get(username=request.user)
        file_path = Session.objects.get(
            session_key=user.tokens.file_token
        ).get_decoded()["file_path"]

        file_name = os.path.basename(file_path).split('-pr-')
        if len(file_name) >= 3:
            transaction_id = "_%s" % file_name[1]
        else:
            transaction_id = ''

        file_extension = os.path.splitext(file_path)[1]

        with open(file_path, 'rb') as f:
            response = HttpResponse(f.read(),
                                    content_type="application/force-download")
            response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(  # noqa
                                'ComBunqWebApp_%s%s%s' % (user, transaction_id,
                                                          file_extension))
            try:
                return response
            # except Exception as e:
            #     raise
            finally:
                os.remove(file_path)

0 View Complete Implementation : semantic_engine.py
Copyright MIT License
Author : opendatasoft
def get_field_clast(ods_dataset_records, field_metas, language='en'):
    """
        Finds the correspondance between clastes in ontologies and one field of the dataset

        Uses two approaches:
        First, try to search claste of instances in knowledge graphs (ex. OpenDataSoft -> Company)
        Else, uses field name as the clast to search
        Then, find the corresponding clast in ontologies using Linked Open Vocabularies (LOV)
    """
    field_name = field_metas['name']
    candidate_clastes = []
    # Search for instances of the dataset in resources of knowledge graphs and retrieve clast of the resource
    for record in ods_dataset_records:
        if field_name in record['record']['fields']:
            value = record['record']['fields'][field_name]
            if has_no_numbers(value):
                types = DBPediaNER.ensaty_types_request(value, language)
                if not types:
                    # DBPedia could not find any clast for this field
                    types = YagoNER.ensaty_types_request(value, language)
                if types:
                    candidate_clastes.extend(types)
    if candidate_clastes:
        common_clast = Counter(candidate_clastes).most_common(1)[0][0]
        common_clast = smart_str(common_clast)
        clast_correspondance = get_clast_correspondance(common_clast, language)
    else:
        # Use the field label as the clast to search
        field_label = smart_str(field_metas['label'])
        field_label = enrich_field(field_metas['type'], field_label)
        clast_correspondance = get_clast_correspondance(field_label, language)
    if clast_correspondance:
        clast_correspondance['label'] = field_metas['label']
        clast_correspondance['field_name'] = field_name
    return clast_correspondance

0 View Complete Implementation : filters.py
Copyright Apache License 2.0
Author : BeanWei
    def get_list_queryset(self, queryset):
        lookup_params = dict([(smart_str(k)[len(FILTER_PREFIX):], v) for k, v in self.admin_view.params.items()
                              if smart_str(k).startswith(FILTER_PREFIX) and v != ''])
        for p_key, p_val in iteritems(lookup_params):
            if p_val == "False":
                lookup_params[p_key] = False
        use_distinct = False

        # for clean filters
        self.admin_view.has_query_param = bool(lookup_params)
        self.admin_view.clean_query_url = self.admin_view.get_query_string(remove=[k for k in self.request.GET.keys() if
                                                                                   k.startswith(FILTER_PREFIX)])

        # Normalize the types of keys
        if not self.free_query_filter:
            for key, value in lookup_params.items():
                if not self.lookup_allowed(key, value):
                    raise SuspiciousOperation(
                        "Filtering by %s not allowed" % key)

        self.filter_specs = []
        if self.list_filter:
            for list_filter in self.list_filter:
                if callable(list_filter):
                    # This is simply a custom list filter clast.
                    spec = list_filter(self.request, lookup_params,
                                       self.model, self)
                else:
                    field_path = None
                    field_parts = []
                    if isinstance(list_filter, (tuple, list)):
                        # This is a custom FieldListFilter clast for a given field.
                        field, field_list_filter_clast = list_filter
                    else:
                        # This is simply a field name, so use the default
                        # FieldListFilter clast that has been registered for
                        # the type of the given field.
                        field, field_list_filter_clast = list_filter, filter_manager.create
                    if not isinstance(field, models.Field):
                        field_path = field
                        field_parts = get_fields_from_path(
                            self.model, field_path)
                        field = field_parts[-1]
                    spec = field_list_filter_clast(
                        field, self.request, lookup_params,
                        self.model, self.admin_view, field_path=field_path)

                    if len(field_parts) > 1:
                        # Add related model name to satle
                        spec.satle = "%s %s" % (field_parts[-2].name, spec.satle)

                    # Check if we need to use distinct()
                    use_distinct = (use_distinct or
                                    lookup_needs_distinct(self.opts, field_path))
                if spec and spec.has_output():
                    try:
                        new_qs = spec.do_filte(queryset)
                    except ValidationError as e:
                        new_qs = None
                        self.admin_view.message_user(_("<b>Filtering error:</b> %s") % e.messages[0], 'error')
                    if new_qs is not None:
                        queryset = new_qs

                    self.filter_specs.append(spec)

        self.has_filters = bool(self.filter_specs)
        self.admin_view.filter_specs = self.filter_specs
        obj = filter(lambda f: f.is_used, self.filter_specs)
        if six.PY3:
            obj = list(obj)
        self.admin_view.used_filter_num = len(obj)

        try:
            for key, value in lookup_params.items():
                use_distinct = (
                    use_distinct or lookup_needs_distinct(self.opts, key))
        except FieldDoesNotExist as e:
            raise IncorrectLookupParameters(e)

        try:
            # fix a bug by david: In demo, quick filter by IDC Name() cannot be used.
            if isinstance(queryset, models.query.QuerySet) and lookup_params:
                new_lookup_parames = dict()
                for k, v in lookup_params.items():
                    list_v = v.split(',')
                    if len(list_v) > 0:
                        new_lookup_parames.update({k: list_v})
                    else:
                        new_lookup_parames.update({k: v})
                queryset = queryset.filter(**new_lookup_parames)
        except (SuspiciousOperation, ImproperlyConfigured):
            raise
        except Exception as e:
            raise IncorrectLookupParameters(e)
        else:
            if not isinstance(queryset, models.query.QuerySet):
                past

        query = self.request.GET.get(SEARCH_VAR, '')

        # Apply keyword searches.
        def construct_search(field_name):
            if field_name.startswith('^'):
                return "%s__istartswith" % field_name[1:]
            elif field_name.startswith('='):
                return "%s__iexact" % field_name[1:]
            elif field_name.startswith('@'):
                return "%s__search" % field_name[1:]
            else:
                return "%s__icontains" % field_name

        if self.search_fields and query:
            orm_lookups = [construct_search(str(search_field))
                           for search_field in self.search_fields]
            for bit in query.split():
                or_queries = [models.Q(**{orm_lookup: bit})
                              for orm_lookup in orm_lookups]
                queryset = queryset.filter(reduce(operator.or_, or_queries))
            if not use_distinct:
                for search_spec in orm_lookups:
                    if lookup_needs_distinct(self.opts, search_spec):
                        use_distinct = True
                        break
            self.admin_view.search_query = query

        if use_distinct:
            return queryset.distinct()
        else:
            return queryset

0 View Complete Implementation : models.py
Copyright MIT License
Author : restran
def get_hexdigest(algorithm, salt, raw_pastword):
    """
    Returns a string of the hexdigest of the given plaintext pastword and salt
    using the given algorithm ('md5', 'sha1' or 'crypt').
    """
    raw_pastword, salt = smart_str(raw_pastword), smart_str(salt)
    if algorithm == 'crypt':
        try:
            import crypt
        except ImportError:
            raise ValueError('"crypt" pastword algorithm not supported in this environment')
        return crypt.crypt(raw_pastword, salt)

    if algorithm == 'md5':
        return hashlib.md5(salt + raw_pastword).hexdigest()
    elif algorithm == 'sha1':
        return hashlib.sha1(salt + raw_pastword).hexdigest()
    elif algorithm == 'sha256':
        return hashlib.sha256(salt + raw_pastword).hexdigest()
    raise ValueError("Got unknown pastword algorithm type in pastword.")

0 View Complete Implementation : views.py
Copyright BSD 2-Clause "Simplified" License
Author : evrenesat
@csrf_exempt
def _upload_file(request):
    """
    Upload file to the server.
    """

    from django.core.files.move import file_move_safe

    if request.method == 'POST':
        folder = request.POST.get('folder')
        fb_uploadurl_re = re.compile(r'^.*(%s)' % reverse("fb_upload"))
        folder = fb_uploadurl_re.sub('', folder)
        abs_path = os.path.join(MEDIA_ROOT, DIRECTORY, folder)
        if request.FILES:
            filedata = request.FILES['Filedata']
            filedata.name = convert_filename(filedata.name)
            # PRE UPLOAD SIGNAL
            filebrowser_pre_upload.send(sender=request, path=request.POST.get('folder'), file=filedata)
            # HANDLE UPLOAD
            uploadedfile = handle_file_upload(abs_path, filedata)
            # MOVE UPLOADED FILE
            # if file already exists
            if os.path.isfile(smart_str(os.path.join(MEDIA_ROOT, DIRECTORY, folder, filedata.name))):
                old_file = smart_str(os.path.join(abs_path, filedata.name))
                new_file = smart_str(os.path.join(abs_path, uploadedfile))
                file_move_safe(new_file, old_file)
            # POST UPLOAD SIGNAL
            filebrowser_post_upload.send(sender=request, path=request.POST.get('folder'), file=FileObject(smart_str(os.path.join(DIRECTORY, folder, filedata.name))))
    return HttpResponse('True')

0 View Complete Implementation : filters.py
Copyright GNU General Public License v2.0
Author : iopsgroup
    def get_list_queryset(self, queryset):
        lookup_params = dict([(smart_str(k)[len(FILTER_PREFIX):], v) for k, v in self.admin_view.params.items()
                              if smart_str(k).startswith(FILTER_PREFIX) and v != ''])
        for p_key, p_val in iteritems(lookup_params):
            if p_val == "False":
                lookup_params[p_key] = False
        use_distinct = False

        # for clean filters
        self.admin_view.has_query_param = bool(lookup_params)
        self.admin_view.clean_query_url = self.admin_view.get_query_string(remove=[k for k in self.request.GET.keys() if
                                                                                   k.startswith(FILTER_PREFIX)])

        # Normalize the types of keys
        if not self.free_query_filter:
            for key, value in lookup_params.items():
                if not self.lookup_allowed(key, value):
                    raise SuspiciousOperation(
                        "Filtering by %s not allowed" % key)

        self.filter_specs = []
        if self.list_filter:
            for list_filter in self.list_filter:
                if callable(list_filter):
                    # This is simply a custom list filter clast.
                    spec = list_filter(self.request, lookup_params,
                                       self.model, self)
                else:
                    field_path = None
                    field_parts = []
                    if isinstance(list_filter, (tuple, list)):
                        # This is a custom FieldListFilter clast for a given field.
                        field, field_list_filter_clast = list_filter
                    else:
                        # This is simply a field name, so use the default
                        # FieldListFilter clast that has been registered for
                        # the type of the given field.
                        field, field_list_filter_clast = list_filter, filter_manager.create
                    if not isinstance(field, models.Field):
                        field_path = field
                        field_parts = get_fields_from_path(
                            self.model, field_path)
                        field = field_parts[-1]
                    spec = field_list_filter_clast(
                        field, self.request, lookup_params,
                        self.model, self.admin_view, field_path=field_path)

                    if len(field_parts) > 1:
                        # Add related model name to satle
                        spec.satle = "%s %s" % (field_parts[-2].name, spec.satle)

                    # Check if we need to use distinct()
                    use_distinct = (use_distinct or
                                    lookup_needs_distinct(self.opts, field_path))
                if spec and spec.has_output():
                    try:
                        new_qs = spec.do_filte(queryset)
                    except ValidationError as e:
                        new_qs = None
                        self.admin_view.message_user(_("<b>Filtering error:</b> %s") % e.messages[0], 'error')
                    if new_qs is not None:
                        queryset = new_qs

                    self.filter_specs.append(spec)

        self.has_filters = bool(self.filter_specs)
        self.admin_view.filter_specs = self.filter_specs
        obj = filter(lambda f: f.is_used, self.filter_specs)
        if six.PY3:
            obj = list(obj)
        self.admin_view.used_filter_num = len(obj)

        try:
            for key, value in lookup_params.items():
                use_distinct = (
                    use_distinct or lookup_needs_distinct(self.opts, key))
        except FieldDoesNotExist as e:
            raise IncorrectLookupParameters(e)

        try:
            # fix a bug by david: In demo, quick filter by IDC Name() cannot be used.
            if isinstance(queryset, models.query.QuerySet) and lookup_params:
                new_lookup_parames = dict()
                for k, v in lookup_params.iteritems():
                    list_v = v.split(',')
                    if len(list_v) > 0:
                        new_lookup_parames.update({k: list_v})
                    else:
                        new_lookup_parames.update({k: v})
                queryset = queryset.filter(**new_lookup_parames)
        except (SuspiciousOperation, ImproperlyConfigured):
            raise
        except Exception as e:
            raise IncorrectLookupParameters(e)
        else:
            if not isinstance(queryset, models.query.QuerySet):
                past

        query = self.request.GET.get(SEARCH_VAR, '')

        # Apply keyword searches.
        def construct_search(field_name):
            if field_name.startswith('^'):
                return "%s__istartswith" % field_name[1:]
            elif field_name.startswith('='):
                return "%s__iexact" % field_name[1:]
            elif field_name.startswith('@'):
                return "%s__search" % field_name[1:]
            else:
                return "%s__icontains" % field_name

        if self.search_fields and query:
            orm_lookups = [construct_search(str(search_field))
                           for search_field in self.search_fields]
            for bit in query.split():
                or_queries = [models.Q(**{orm_lookup: bit})
                              for orm_lookup in orm_lookups]
                queryset = queryset.filter(reduce(operator.or_, or_queries))
            if not use_distinct:
                for search_spec in orm_lookups:
                    if lookup_needs_distinct(self.opts, search_spec):
                        use_distinct = True
                        break
            self.admin_view.search_query = query

        if use_distinct:
            return queryset.distinct()
        else:
            return queryset

0 View Complete Implementation : __init__.py
Copyright MIT License
Author : alexmorozov
def fill_template(template_name, context, output_format='odt'):
    """
    Fill a docameent with data and convert it to the requested format.

    Returns an absolute path to the generated file.
    """

    if not isinstance(context, Context):
        context = Context(context)

    context['output_format'] = output_format

    source_file = find_template_file(template_name)
    source_extension = os.path.splitext(source_file)[1]
    source = zipfile.ZipFile(source_file, 'r')

    dest_file = NamedTemporaryFile(delete=False, suffix=source_extension)
    dest = zipfile.ZipFile(dest_file, 'w')

    manifest_data = ''
    for name in source.namelist():
        data = source.read(name)
        if name.endswith('.xml'):
            data = smart_str(data)

        if any(name.endswith(file) for file in ('content.xml', 'styles.xml')):
            template = Template(fix_inline_tags(data))
            data = template.render(context)
        elif name == 'META-INF/manifest.xml':
            manifest_data = data[:-20]  # Cut off the closing </manifest> tag
            continue  # We will append it at the very end
        dest.writestr(name, smart_bytes(data))

    for _, image in context.dicts[0].get(IMAGES_CONTEXT_KEY, {}).items():
        filename = os.path.basename(image.name)
        ext = os.path.splitext(filename)[1][1:]
        manifest_data += ('<manifest:file-entry '
                          'manifest:media-type="image/%(ext)s" '
                          'manifest:full-path="Pictures/%(filename)s"/>\n'
                          ) % locals()
        image.open()
        dest.writestr('Pictures/%s' % filename, image.read())
        image.close()

    manifest_data += '</manifest:manifest>'
    dest.writestr('META-INF/manifest.xml', manifest_data)

    source.close()
    dest.close()

    if source_extension[1:] != output_format:
        results = Queue()
        convertor = Process(target=_convert_subprocess,
                            args=(str(dest_file.name), output_format, results))
        convertor.start()
        return results.get()
    else:
        return dest_file.name

0 View Complete Implementation : views.py
Copyright GNU General Public License v3.0
Author : jimmy201602
    def post(self, request):
        if request.is_ajax():
            try:
                if isinstance(request.body, bytes):
                    data = json.loads(request.body.decode())
                else:
                    data = json.loads(request.body)
                id = data.get('id', None)
                action = data.get('action', None)
                fields = [
                    field.name for field in Credential._meta.get_fields()]
                data = {k: v for k, v in data.items() if k in fields}
                if action == 'create':
                    if not request.user.has_perm('common.can_add_credential'):
                        raise PermissionDenied(_('403 Forbidden'))
                    obj = Credential.objects.create(**data)
                    obj.save()
                    return JsonResponse({'status': True, 'message': 'Credential %s was created!' % (obj.name)})
                elif action == 'update':
                    if not request.user.has_perm('common.can_change_credential'):
                        raise PermissionDenied(_('403 Forbidden'))
                    try:
                        obj = Credential.objects.get(id=id)
                        obj.__dict__.update(**data)
                        obj.save()
                        return JsonResponse({'status': True, 'message': 'Credential %s update success!' % (smart_str(data.get('name', None)))})
                    except ObjectDoesNotExist:
                        return JsonResponse({'status': False, 'message': 'Request object not exist!'})
                elif action == 'delete':
                    if not request.user.has_perm('common.can_delete_credential'):
                        raise PermissionDenied(_('403 Forbidden'))
                    try:
                        obj = Credential.objects.get(id=id)
                        obj.delete()
                        return JsonResponse({'status': True, 'message': 'Delete credential %s success!' % (smart_str(data.get('name', None)))})
                    except ObjectDoesNotExist:
                        return JsonResponse({'status': False, 'message': 'Request object not exist!'})
                else:
                    return JsonResponse({'status': False, 'message': 'Illegal action.'})
            except IntegrityError:
                return JsonResponse({'status': False, 'message': 'Credential %s already exist! Please use another name instead!' % (smart_str(json.loads(request.body).get('name', None)))})
            except Exception as e:
                print(traceback.print_exc())
                return JsonResponse({'status': False, 'message': 'Error happend! Please report it to adminstrator! Error:%s' % (smart_str(e))})

0 View Complete Implementation : forms_special_fields.py
Copyright MIT License
Author : robotichead
    def _render(self, name, value, attrs=None, choices=()):
        if value is None: value = ''

        #Get SQL Objects
        country_results = list_of_country.objects\
            .filter(is_deleted="FALSE")\
            .order_by('country_name')
        region_results = list_of_country_region.objects\
            .filter(is_deleted="FALSE")\
            .order_by('country_id','region_name')


        #Start the rendering
        output = u'<select name="country_and_regions" id="id_country_and_regions" clast="chosen-select form-control"><option value="" selected> Select a Country/Region </option>'

        #Render for ALL Countries
        for country in country_results:
            #Country
            output = output + u'<optgroup label="' + country.country_name + '">'

            #Loop to place in the regions
            for region in region_results.filter(country_id=country.country_id):
                option_value = smart_str(region.region_id)
                option_label = smart_str(region.region_name)
                output = output + u'<option value="%s">%s' % (escape(option_value), escape(option_label))
                output = output + u'</option>'


        #Close everything off
        output = output + u'</optgroup></option></select>'
        return output

0 View Complete Implementation : tests.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : moggers87
    def test_xsendfile_header_containing_unicode(self):
        filepath = self.ensure_file(u'péter_là_gueule.txt')
        response = real_sendfile(HttpRequest(), filepath)
        self.astertTrue(response is not None)
        self.astertEqual(smart_str(filepath), response['X-Sendfile'])

0 View Complete Implementation : lib.py
Copyright MIT License
Author : Arx-Game
def text_is_spam(text, request):
    # Based on a blog post by 'sciyoshi':
    # http://sciyoshi.com/blog/2008/aug/27/using-akismet-djangos-new-comments-framework/
    # This will return 'True' is the given text is deemed to be spam, or
    # False if it is not spam. If it cannot be checked for some reason, we
    # astume it isn't spam.
    from django.contrib.sites.models import Site
    from django.conf import settings
    try:
        from web.helpdesk.akismet import Akismet
    except:
        return False
    try:
        site = Site.objects.get_current()
    except:
        site = Site(domain='configure-django-sites.com')

    ak = Akismet(
        blog_url='http://%s/' % site.domain,
        agent='django-helpdesk',
    )

    if hasattr(settings, 'TYPEPAD_ANTISPAM_API_KEY'):
        ak.setAPIKey(key = settings.TYPEPAD_ANTISPAM_API_KEY)
        ak.baseurl = 'api.antispam.typepad.com/1.1/'
    elif hasattr(settings, 'AKISMET_API_KEY'):
        ak.setAPIKey(key = settings.AKISMET_API_KEY)
    else:
        return False

    if ak.verify_key():
        ak_data = {
            'user_ip': request.META.get('REMOTE_ADDR', '127.0.0.1'),
            'user_agent': request.META.get('HTTP_USER_AGENT', ''),
            'referrer': request.META.get('HTTP_REFERER', ''),
            'comment_type': 'comment',
            'comment_author': '',
        }

        return ak.comment_check(smart_str(text), data=ak_data)

    return False

0 View Complete Implementation : app_plugins.py
Copyright Apache License 2.0
Author : edisonlz
def callback(func, variables, context, takes_context):
    """
    resolve an iterable of Variable objects into a list of args and a dict
    of keyword arguments. support full python style keyword argument
    processing::
        >>> def foo(a, b, c=1, d=2):
        ...     past
        >>> foo(1, 2)
        >>> foo(1, b=2)
        >>> foo(b=2, a=1, d=3)
    """
    name = getattr(func, "_decorated_function", func).__name__
    params, varargs, varkw, defaults = getargspec(func)
    if takes_context:
        if params[0] == 'context':
            params.pop(0)
        else:
            raise TemplateSyntaxError(
                "Any tag function decorated with takes_context=True "
                "must have a first argument of 'context'")
    num_defaults = len(defaults)
    num_params = len(params)
    num_req = num_params - num_defaults

    args = []
    kwdargs = {}
    found_kwd = False
    for variable in variables:
        if not found_kwd:
            try:
                args.append(variable.resolve(context))
            except VariableDoesNotExist:
                if variable.var.count(TAG_KEYWORD_ARGUMENT_SEPARATOR) != 1:
                    raise
                found_kwd = True
        if found_kwd:
            try:
                var, path = variable.var.split(TAG_KEYWORD_ARGUMENT_SEPARATOR)
            except ValueError:
                raise TemplateSyntaxError(
                    "Expected keyword astignemnt, found '%s' instead" %
                    variable.var)
            if params and not varkw and name not in params:
                raise TemplateSyntaxError(
                    "%s got an unexpected keyword argument '%s'" % (name, var))
            if var in kwdargs:
                raise TemplateSyntaxError(
                    "%s got multiple values for keyword argument '%s'" %
                    (name, var))
            kwdargs[smart_str(var)] = Variable(path).resolve(context)

    num_args = len(args)
    num_kwds = len(kwdargs)
    num_all = num_args + num_kwds

    if ((num_args > num_params and not varargs) or
        (num_all> num_params and not varkw)):
        raise TemplateSyntaxError(
                "%s takes at most %s arguments. (%s given)" % (
                    name, num_params, num_all) )
    if num_args != num_req:
        if num_args > num_req:
            # some args are kwd args (maybe multiple keyword error)
            if not varargs:
                allowed = set(params[num_args:])
                not_allowed = set(kwdargs) - allowed
                if not_allowed:
                    raise TemplateSyntaxError(
                        "%s got multiple values for keyword arguments: %s" % (
                            name, ", ".join(not_allowed) ))
        elif not varkw:
            # not enough required parameters error
            required = set(params[num_args:-num_default])
            missing = required - set(kwdargs)
            if missing:
                raise TemplateSyntaxError(
                    "%s takes at least %s non-keyword arguments (%s given)" % (
                        name, num_req, num_args))
    if takes_context:
        args.insert(0, context)
    return func(*args, **kwdargs)

0 View Complete Implementation : forms_special_fields.py
Copyright MIT License
Author : robotichead
    def render(self, name, value, attrs=None, choices=()):
        if value is None: value = ''

        # Get SQL Objects
        customer_results = customer.objects.filter(
            is_deleted="FALSE"
        )

        # Start the rendering
        output = u'<select name="to_email" id="id_to_email" clast="chosen-select form-control"><option value="------" selected disabled> Select an Email </option>'

        # Render the emails
        for option in customer_results:
            option_value = smart_str(option.customer_id)
            option_label = smart_str(option.customer_email)
            output = output + u'<option value="%s">%s' % (escape(option_value), escape(option_label))
            output = output + u'</option>'

        #Close off select
        output = output + u'</select>'

        return output

0 View Complete Implementation : db.py
Copyright Apache License 2.0
Author : edisonlz
    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)
            enable_stacktraces = getattr(settings,
                                        'DEBUG_TOOLBAR_CONFIG', {}) \
                                    .get('ENABLE_STACKTRACES', True)
            if enable_stacktraces:
                stacktrace = tidy_stacktrace(reversed(get_stack()))
            else:
                stacktrace = []
            _params = ''
            try:
                _params = simplejson.dumps(
                        [force_unicode(x, strings_only=True) for x in params]
                            )
            except TypeError:
                past  # 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:
                past
            del cur_frame

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

            params = {
                'engine': engine,
                'alias': alias,
                'sql': self.db.ops.last_executed_query(self.cursor, sql,
                                                self._quote_params(params)),
                'duration': duration,
                'raw_sql': sql,
                'params': _params,
                'hash': sha_constructor(settings.SECRET_KEY \
                                        + smart_str(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)

0 View Complete Implementation : __init__.py
Copyright Apache License 2.0
Author : edisonlz
    def foreignkey_autocomplete(self, request):
        """
        Searches in the fields of the given related model and returns the
        result as a simple string to be used by the jQuery Autocomplete plugin
        """
        query = request.GET.get('q', None)
        app_label = request.GET.get('app_label', None)
        model_name = request.GET.get('model_name', None)
        search_fields = request.GET.get('search_fields', None)
        object_pk = request.GET.get('object_pk', None)

        try:
            to_string_function = self.related_string_functions[model_name]
        except KeyError:
            if six.PY3:
                to_string_function = lambda x: x.__str__()
            else:
                to_string_function = lambda x: x.__unicode__()

        if search_fields and app_label and model_name and (query or object_pk):
            def construct_search(field_name):
                # use different lookup methods depending on the notation
                if field_name.startswith('^'):
                    return "%s__istartswith" % field_name[1:]
                elif field_name.startswith('='):
                    return "%s__iexact" % field_name[1:]
                elif field_name.startswith('@'):
                    return "%s__search" % field_name[1:]
                else:
                    return "%s__icontains" % field_name
            model = models.get_model(app_label, model_name)
            queryset = model._default_manager.all()
            data = ''
            if query:
                for bit in query.split():
                    or_queries = [models.Q(**{construct_search(smart_str(field_name)): smart_str(bit)}) for field_name in search_fields.split(',')]
                    other_qs = QuerySet(model)
                    other_qs.query.select_related = queryset.query.select_related
                    other_qs = other_qs.filter(reduce(operator.or_, or_queries))
                    queryset = queryset & other_qs

                if self.autocomplete_limit:
                    queryset = queryset[:self.autocomplete_limit]

                data = ''.join([six.u('%s|%s\n') % (to_string_function(f), f.pk) for f in queryset])
            elif object_pk:
                try:
                    obj = queryset.get(pk=object_pk)
                except:
                    past
                else:
                    data = to_string_function(obj)
            return HttpResponse(data)
        return HttpResponseNotFound()

0 View Complete Implementation : views.py
Copyright MIT License
Author : restran
@login_required
def export_config(request):
    """
    导出配置
    :param request:
    :return:
    """
    try:
        file_name = 'export_config_%s.json' % datetime.today().strftime('%Y%m%d')
        json_data = get_export_config_json()
        config_data = json_dumps(json_data, indent=4, sort_keys=True)

        agent = request.META.get('HTTP_USER_AGENT')
        if agent and re.search('MSIE', agent):
            # 解决ie不能下载的问题
            response = HttpResponse(content=config_data, content_type="text/plain; charset=utf-8")
            # 解决文件名乱码/不显示的问题
            response['Content-Disposition'] = 'attachment; filename=%s' % urlquote(file_name)
        else:
            response = HttpResponse(content=config_data, content_type="text/plain; charset=utf-8")
            response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name)

        return response
    except Exception as e:
        logger.error(e.message)
        logger.error(traceback.format_exc())
        return HttpResponse('导出配置失败 %s' % e.message)

0 View Complete Implementation : check_permissions.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : django-userena-ce
    def handle(self, **options):
        permissions, users, warnings = UserenaSignup.objects.check_permissions()
        output = options.pop("output")
        test = options.pop("test")
        if test:
            self.stdout.write(40 * ".")
            self.stdout.write(
                "\nChecking permission management command. Ignore output..\n\n"
            )
        if output:
            for p in permissions:
                self.stdout.write("Added permission: %s\n" % p)

            for u in users:
                self.stdout.write(
                    "Changed permissions for user: %s\n"
                    % smart_str(u, encoding="utf-8", strings_only=False)
                )

            for w in warnings:
                self.stdout.write("WARNING: %s\n" % w)

        if test:
            self.stdout.write("\nFinished testing permissions command.. continuing..\n")

0 View Complete Implementation : related.py
Copyright Apache License 2.0
Author : edisonlz
@never_cache
def autocomplete_lookup(request):
    if not (request.user.is_active and request.user.is_staff):
        return HttpResponseForbidden('<h1>Permission denied</h1>')
    data = []
    if request.method == 'GET':
        if request.GET.has_key('term') and request.GET.has_key('app_label') and request.GET.has_key('model_name'):
            term = request.GET.get("term")
            app_label = request.GET.get('app_label')
            model_name = request.GET.get('model_name')
            model = models.get_model(app_label, model_name)
            filters = {}
            # FILTER
            if request.GET.get('query_string', None):
                for item in request.GET.get('query_string').split("&"):
                    if item.split("=")[0] != "t":
                        filters[smart_str(item.split("=")[0])]=smart_str(item.split("=")[1])
            # SEARCH
            qs = model._default_manager.all()
            for bit in term.split():
                search = [models.Q(**{smart_str(item):smart_str(bit)}) for item in model.autocomplete_search_fields()]
                search_qs = QuerySet(model)
                search_qs.dup_select_related(qs)
                search_qs = search_qs.filter(reduce(operator.or_, search))
                qs = qs & search_qs
            data = [{"value":f.pk,"label":u'%s' % get_label(f)} for f in qs[:10]]
            label = ungettext(
                '%(counter)s result',
                '%(counter)s results',
            len(data)) % {
                'counter': len(data),
            }
            #data.insert(0, {"value":None,"label":label})
            return HttpResponse(simplejson.dumps(data), mimetype='application/javascript')
    data = [{"value":None,"label":_("Server error")}]
    return HttpResponse(simplejson.dumps(data), mimetype='application/javascript')

0 View Complete Implementation : base.py
Copyright Apache License 2.0
Author : edisonlz
    def get_queryset(self):
        lookup_params = self.params.copy()
        qs = self.model.objects.all()
        for field in [GROUP_BY_VAR, SORT_VAR, SORTTYPE_VAR, DETAILS_SWITCH_VAR]:
            if field in lookup_params:
                del lookup_params[field]
        for key, value in lookup_params.items():
            if not isinstance(key, str):
                # 'key' will be used as a keyword argument later, so Python
                # requires it to be a string.
                del lookup_params[key]
                lookup_params[smart_str(key)] = value

            # if key ends with __in, split parameter into separate values
            if key.endswith('__in'):
                lookup_params[key] = value.split(',')
        try:
            qs = qs.filter(**lookup_params)
        except:
            raise IncorrectLookupParameters
        return qs

0 View Complete Implementation : decorators.py
Copyright Apache License 2.0
Author : edisonlz
def localdatetime(field_name):
    def get_datetime(instance):
        return getattr(instance, field_name)
    def set_datetime(instance, value):
        return setattr(instance, field_name, value)
        
    def make_local_property(get_tz):
        def get_local(instance):
            tz = get_tz(instance)
            if not hasattr(tz, "localize"):
                tz = pytz.timezone(smart_str(tz))
            dt = get_datetime(instance)
            if dt.tzinfo is None:
                dt = default_tz.localize(dt)
            return dt.astimezone(tz)
        def set_local(instance, dt):
            if dt.tzinfo is None:
                tz = get_tz(instance)
                if not hasattr(tz, "localize"):
                    tz = pytz.timezone(smart_str(tz))
                dt = tz.localize(dt)
            dt = dt.astimezone(default_tz)
            return set_datetime(instance, dt)
        return property(get_local, set_local)
    return make_local_property

0 View Complete Implementation : functions.py
Copyright BSD 2-Clause "Simplified" License
Author : evrenesat
def get_version_path(value, version_prefix):
    """
    Construct the PATH to an Image version.
    Value has to be server-path, relative to MEDIA_ROOT.

    version_filename = filename + version_prefix + ext
    Returns a path relative to MEDIA_ROOT.
    """

    if os.path.isfile(smart_str(os.path.join(MEDIA_ROOT, value))):
        path, filename = os.path.split(value)
        filename, ext = os.path.splitext(filename)

        # check if this file is a version of an other file
        # to return filename_<version>.ext instead of filename_<version>_<version>.ext
        tmp = filename.split("_")
        if tmp[len(tmp)-1] in ADMIN_VERSIONS:
            # it seems like the "original" is actually a version of an other original
            # so we strip the suffix (aka. version_perfix)
            new_filename = filename.replace("_" + tmp[len(tmp)-1], "")
            # check if the version exists when we use the new_filename
            if os.path.isfile(smart_str(os.path.join(MEDIA_ROOT, path, new_filename + "_" + version_prefix + ext))):
                # our "original" filename seem to be filename_<version> construct
                # so we replace it with the new_filename
                filename = new_filename
                # if a VERSIONS_BASEDIR is set we need to strip it from the path
                # or we get a <VERSIONS_BASEDIR>/<VERSIONS_BASEDIR>/... construct
                if VERSIONS_BASEDIR != "":
                        path = path.replace(VERSIONS_BASEDIR + "/", "")

        version_filename = filename + "_" + version_prefix + ext
        return os.path.join(VERSIONS_BASEDIR, path, version_filename)
    else:
        return None