<commit_before>from frasco.ext import *
from frasco.upload import url_for_upload
from frasco.helpers import url_for
from flask import current_app, request
import sqlalchemy as sqla
import hashlib
import urllib
import math
import random
import base64
import requests


def svg_to_base64_data(svg):
    return 'data:image/svg+xml;base64,' + base64.b64encode(svg)


class UserAvatarModelMixin(object):
    avatar_filename = sqla.Column(sqla.String)

    @property
    def avatar_url(self):
        return url_for_avatar(self)


class FrascoUsersAvatars(Extension):
    name = "frasco_users_avatars"
    defaults = {"url": None,
                "avatar_size": 80,
                "add_flavatar_route": False,
                "try_gravatar": True,
                "force_gravatar": False,
                "gravatar_size": None,
                "gravatar_email_column": None,
                "gravatar_default": "mm",
                "force_flavatar": False,
                "flavatar_size": "100%",
                "flavatar_name_column": None,
                "flavatar_font_size": 80,
                "flavatar_text_dy": "0.32em",
                "flavatar_length": 1,
                "flavatar_text_color": "#ffffff",
                "flavatar_bg_colors": ["#5A8770", "#B2B7BB", "#6FA9AB", "#F5AF29", "#0088B9", "#F18636", "#D93A37", "#A6B12E", "#5C9BBC", "#F5888D", "#9A89B5", "#407887", "#9A89B5", "#5A8770", "#D33F33", "#A2B01F", "#F0B126", "#0087BF", "#F18636", "#0087BF", "#B2B7BB", "#72ACAE", "#9C8AB4", "#5A8770", "#EEB424", "#407887"]}

    def _init_app(self, app, state):
        app.add_template_global(url_for_avatar)

        def flavatar(name, bgcolorstr=None):
            if bgcolorstr is None:
                bgcolorstr = request.args.get('bgcolorstr')
            return generate_first_letter_avatar_svg(
                name, bgcolorstr, request.args.get('size')), 200, {'Content-Type': 'image/svg+xml'}

        @app.route('/avatar/<hash>/<name>')
        def avatar(hash, name):
            if state.options['try_gravatar']:
                size = state.options['gravatar_size'] or state.options["avatar_size"]
                try:
                    r = requests.get(url_for_gravatar(hash, size=size, default=404))
                    if r.status_code != 404:
                        return r.content, 200, {'Content-Type': r.headers['content-type']}
                except Exception:
                    pass
            return flavatar(name, hash)

        if state.options['add_flavatar_route']:
            app.add_url_rule('/flavatar/<name>.svg', 'flavatar', flavatar)
            app.add_url_rule('/flavatar/<name>/<bgcolorstr>.svg', 'flavatar', flavatar)


def url_for_avatar(user):
    state = get_extension_state('frasco_users_avatars')
    if getattr(user, 'avatar_filename', None):
        return url_for_upload(user.avatar_filename)

    hash = None
    username = getattr(user, state.options["flavatar_name_column"] or 'username', None)
    if username:
        if isinstance(username, unicode):
            username = username.lower().encode('utf-8')
        else:
            username = username.lower()
        hash = hashlib.md5(username).hexdigest()
    email = getattr(user, state.options["gravatar_email_column"] or 'email', None)
    if email:
        if isinstance(email, unicode):
            email = email.lower().encode('utf-8')
        else:
            email = email.lower()
        hash = hashlib.md5(email).hexdigest()

    if state.options["force_flavatar"] and (email or username):
        if state.options['add_flavatar_route']:
            return url_for('flavatar', name=username, bgcolorstr=hash, _external=True)<commit_msg>slugify username before using in avatr url<commit_after>from frasco.ext import *
from frasco.upload import url_for_upload
from frasco.helpers import url_for
from frasco.utils import slugify
from flask import current_app, request
import sqlalchemy as sqla
import hashlib
import urllib
import math
import random
import base64
import requests


def svg_to_base64_data(svg):
    return 'data:image/svg+xml;base64,' + base64.b64encode(svg)


class UserAvatarModelMixin(object):
    avatar_filename = sqla.Column(sqla.String)

    @property
    def avatar_url(self):
        return url_for_avatar(self)


class FrascoUsersAvatars(Extension):
    name = "frasco_users_avatars"
    defaults = {"url": None,
                "avatar_size": 80,
                "add_flavatar_route": False,
                "try_gravatar": True,
                "force_gravatar": False,
                "gravatar_size": None,
                "gravatar_email_column": None,
                "gravatar_default": "mm",
                "force_flavatar": False,
                "flavatar_size": "100%",
                "flavatar_name_column": None,
                "flavatar_font_size": 80,
                "flavatar_text_dy": "0.32em",
                "flavatar_length": 1,
                "flavatar_text_color": "#ffffff",
                "flavatar_bg_colors": ["#5A8770", "#B2B7BB", "#6FA9AB", "#F5AF29", "#0088B9", "#F18636", "#D93A37", "#A6B12E", "#5C9BBC", "#F5888D", "#9A89B5", "#407887", "#9A89B5", "#5A8770", "#D33F33", "#A2B01F", "#F0B126", "#0087BF", "#F18636", "#0087BF", "#B2B7BB", "#72ACAE", "#9C8AB4", "#5A8770", "#EEB424", "#407887"]}

    def _init_app(self, app, state):
        app.add_template_global(url_for_avatar)

        def flavatar(name, bgcolorstr=None):
            if bgcolorstr is None:
                bgcolorstr = request.args.get('bgcolorstr')
            return generate_first_letter_avatar_svg(
                name, bgcolorstr, request.args.get('size')), 200, {'Content-Type': 'image/svg+xml'}

        @app.route('/avatar/<hash>/<name>')
        def avatar(hash, name):
            if state.options['try_gravatar']:
                size = state.options['gravatar_size'] or state.options["avatar_size"]
                try:
                    r = requests.get(url_for_gravatar(hash, size=size, default=404))
                    if r.status_code != 404:
                        return r.content, 200, {'Content-Type': r.headers['content-type']}
                except Exception:
                    pass
            return flavatar(name, hash)

        if state.options['add_flavatar_route']:
            app.add_url_rule('/flavatar/<name>.svg', 'flavatar', flavatar)
            app.add_url_rule('/flavatar/<name>/<bgcolorstr>.svg', 'flavatar', flavatar)


def url_for_avatar(user):
    state = get_extension_state('frasco_users_avatars')
    if getattr(user, 'avatar_filename', None):
        return url_for_upload(user.avatar_filename)

    hash = None
    username = getattr(user, state.options["flavatar_name_column"] or 'username', None)
    if username:
        if isinstance(username, unicode):
            username = username.lower().encode('utf-8')
        else:
            username = username.lower()
        username = slugify(username)
        hash = hashlib.md5(username).hexdigest()
    email = getattr(user, state.options["gravatar_email_column"] or 'email', None)
    if email:
        if isinstance(email, unicode):
            email = email.lower().encode('utf-8')
        else:
            email = email.lower()
        hash = hashlib.md5(email).hexdigest()

    if state.options["force_flavatar"] and (email or username):
        if state.options['add_flavatar_route']:
            return url_for('flavatar', name=username, bgcolorstr=hash, _external=True)
{'fraction_non_alphanumeric': 0.13268998793727382, 'fraction_numerical': 0.03645623911003887, 'mean_word_length': 2.6030902945436987, 'pattern_counts': {'":': 32, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '6238458', 'n_tokens_mistral': 2592, 'n_tokens_neox': 2358, 'n_words': 467}