<commit_before># -*- coding: utf-8 -*-
#
# gPodder - A media aggregator and podcast client
# Copyright (c) 2005-2009 <NAME> and the gPodder Team
#
# gPodder is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# gPodder is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

from functools import wraps
import inspect

from couchdbkit import ResourceConflict

from django.http import Http404
from django.shortcuts import render
from django.http import HttpResponseForbidden, HttpResponseNotAllowed


def requires_token(token_name, denied_template=None):
    """
    returns a decorator that checks if the security token in the 'token' GET
    parameter matches the requires token for the resource. The protected
    resource is indicated by
    * the username parameter passed to the decorated function
    * token_name passed to this method

    The decorated method is returned, if
    * no token is required for the resource
    * the token in the 'token' GET parameter matches the required token

    If the passed token does not match
    * the denied_template is rendered and returned if given
    * HttpResponseForbidden is returned, if denied_template is not given
    """
    def decorator(fn):
        @wraps(fn)
        def tmp(request, username, *args, **kwargs):

            from mygpo.users.models import User
            user = User.get_user(username)
            if not user:
                raise Http404

            token = user.get_token(token_name)
            u_token = request.GET.get('token', '')

            if token == '' or token == u_token:
                return fn(request, username, *args, **kwargs)

            else:
                if denied_template:
                    return render(request, denied_template, {
                        'other_user': user
                    })

                else:
                    return HttpResponseForbidden()

        return tmp
    return decorator


def allowed_methods(methods):
    def decorator(fn):
        @wraps(fn)
        def tmp(request, *args, **kwargs):
            if request.method in methods:
                return fn(request, *args, **kwargs)
            else:
                return HttpResponseNotAllowed(methods)

        return tmp

    return decorator


class repeat_on_conflict(object):
    """ Repeats an update operation in case of a ResourceConflict

    In case of a CouchDB ResourceConflict, reloads the parameter with the given
    name and repeats the function call until it succeeds.  When calling the
    function, the parameter that should be reloaded must be given as a
    keyword-argument """

    ARGSPEC = '__repeat_argspec__'

    def __init__(self, obj_names=[], reload_f=None):
        self.obj_names = obj_names
        self.reload_f = reload_f or self.default_reload

    def default_reload(self, obj):
        # if the object knows its DB, use this one
        if obj._db:
            return obj._db.get(obj._id)<commit_msg>fix wrap doc when reloading from DB<commit_after># -*- coding: utf-8 -*-
#
# gPodder - A media aggregator and podcast client
# Copyright (c) 2005-2009 <NAME> and the gPodder Team
#
# gPodder is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# gPodder is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

from functools import wraps
import inspect

from couchdbkit import ResourceConflict

from django.http import Http404
from django.shortcuts import render
from django.http import HttpResponseForbidden, HttpResponseNotAllowed


def requires_token(token_name, denied_template=None):
    """
    returns a decorator that checks if the security token in the 'token' GET
    parameter matches the requires token for the resource. The protected
    resource is indicated by
    * the username parameter passed to the decorated function
    * token_name passed to this method

    The decorated method is returned, if
    * no token is required for the resource
    * the token in the 'token' GET parameter matches the required token

    If the passed token does not match
    * the denied_template is rendered and returned if given
    * HttpResponseForbidden is returned, if denied_template is not given
    """
    def decorator(fn):
        @wraps(fn)
        def tmp(request, username, *args, **kwargs):

            from mygpo.users.models import User
            user = User.get_user(username)
            if not user:
                raise Http404

            token = user.get_token(token_name)
            u_token = request.GET.get('token', '')

            if token == '' or token == u_token:
                return fn(request, username, *args, **kwargs)

            else:
                if denied_template:
                    return render(request, denied_template, {
                        'other_user': user
                    })

                else:
                    return HttpResponseForbidden()

        return tmp
    return decorator


def allowed_methods(methods):
    def decorator(fn):
        @wraps(fn)
        def tmp(request, *args, **kwargs):
            if request.method in methods:
                return fn(request, *args, **kwargs)
            else:
                return HttpResponseNotAllowed(methods)

        return tmp

    return decorator


class repeat_on_conflict(object):
    """ Repeats an update operation in case of a ResourceConflict

    In case of a CouchDB ResourceConflict, reloads the parameter with the given
    name and repeats the function call until it succeeds.  When calling the
    function, the parameter that should be reloaded must be given as a
    keyword-argument """

    ARGSPEC = '__repeat_argspec__'

    def __init__(self, obj_names=[], reload_f=None):
        self.obj_names = obj_names
        self.reload_f = reload_f or self.default_reload

    def default_reload(self, obj):
        # if the object knows its DB, use this one
        if obj._db:
            doc = obj._db.get(obj._id)
            return obj.__class__.wrap(doc)
{'fraction_non_alphanumeric': 0.06476163124641011, 'fraction_numerical': 0.004595060310166571, 'mean_word_length': 2.571794871794872, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6844697', 'n_tokens_mistral': 1842, 'n_tokens_neox': 1752, 'n_words': 759}