<commit_before># -*- coding: utf8 -*-

import default_settings
from flask.ext.plugins import Plugin
from flask import current_app as app
from pybossa.model.user import User
from sqlalchemy import event
from flask.ext.login import current_user, login_required
from gravatar import Gravatar

__plugin__ = "PyBossaGravatar"
__version__ = "0.2.2"


class PyBossaGravatar(Plugin):
    """A PyBossa plugin for Gravatar integration."""
    
    
    def setup(self):
        """Setup the plugin."""
        self.load_config()
        self.gravatar = Gravatar(app)
        self.setup_event_listener()
        self.setup_url_rule()
    
    
    def load_config(self):
        """Configure the plugin."""
        settings = [key for key in dir(default_settings) if key.isupper()]
        
        for s in settings:
            if not app.config.get(s):
                app.config[s] = getattr(default_settings, s)
    
    
    def setup_event_listener(self):
        """Setup event listener."""

        @event.listens_for(User, 'before_insert')
        def add_user_event(mapper, conn, target):
            """Set gravatar by default for new users."""
            self.gravatar.set(target, update_repo=False)
    
    
    def setup_url_rule(self):
        """Setup URL rule."""
        
        @app.route('/account/set-gravatar', methods=['POST'])
        @login_required
        def set_gravatar():  # pragma: no cover
            """Set gravatar for the current user."""
            self.gravatar.set(current_user)
            <commit_msg>Fix URL rule for setting Gravatar<commit_after># -*- coding: utf8 -*-

import default_settings
from flask import current_app as app
from flask import redirect, url_for, flash, Blueprint, request, abort
from flask.ext.plugins import Plugin
from flask.ext.babel import gettext
from flask.ext.login import current_user, login_required
from pybossa.model.user import User
from pybossa.core import user_repo
from pybossa.auth import ensure_authorized_to
from sqlalchemy import event
from gravatar import Gravatar

__plugin__ = "PyBossaGravatar"
__version__ = "0.2.2"


class PyBossaGravatar(Plugin):
    """A PyBossa plugin for Gravatar integration."""
    
    
    def setup(self):
        """Setup the plugin."""
        self.load_config()
        self.gravatar = Gravatar(app)
        self.setup_event_listener()
        self.setup_url_rule()
    
    
    def load_config(self):
        """Configure the plugin."""
        settings = [key for key in dir(default_settings) if key.isupper()]
        
        for s in settings:
            if not app.config.get(s):
                app.config[s] = getattr(default_settings, s)
    
    
    def setup_event_listener(self):
        """Setup event listener."""

        @event.listens_for(User, 'before_insert')
        def add_user_event(mapper, conn, target):
            """Set gravatar by default for new users."""
            self.gravatar.set(target, update_repo=False)
    
    
    def setup_url_rule(self):
        """Setup URL rule."""
        
        @app.route('/account/<name>/update/gravatar/set')
        @login_required
        def set_gravatar(name):  # pragma: no cover
            """Set gravatar for a user."""
            user = user_repo.get_by(name=name)
            if not user:
                abort(404)
            
            ensure_authorized_to('update', user)
            
            self.gravatar.set(user)
            flash(gettext('Your avatar has been updated! It may \
                          take some minutes to refresh...'), 'success')
            
            return redirect(url_for('account.update_profile', name=user.name))
            
{'fraction_non_alphanumeric': 0.10308153804199618, 'fraction_numerical': 0.0029997272975184073, 'mean_word_length': 2.3015301530153014, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2618099', 'n_tokens_mistral': 1080, 'n_tokens_neox': 1038, 'n_words': 282}