<commit_before># -*- coding: UTF-8 -*-
from assopy.clients import genro
from conference.models import Speaker

from django.contrib import auth
from django.db import models
from django.db import transaction
from django.utils.translation import ugettext_lazy as _

import logging
from datetime import date, datetime

log = logging.getLogger('assopy.models')

def _cache(f):
    """
    cache dei poveri, memorizzo nell'istanza il risultato di una richiesta
    remota (la cache dura una richiesta)
    """
    def wrapper(self, *args, **kwargs):
        key = '_c' + f.__name__
        try:
            r = getattr(self, key)
        except AttributeError:
            r = f(self, *args, **kwargs)
            setattr(self, key, r)
        return r
    return wrapper

def _gravatar(email, size=80, default='identicon', rating='r'):
    # import code for encoding urls and generating md5 hashes
    import urllib, hashlib

    gravatar_url = "http://www.gravatar.com/avatar/" + hashlib.md5(email.lower()).hexdigest() + "?"
    gravatar_url += urllib.urlencode({
        'default': default,
        'size': size,
        'rating': rating,
    })
    
    return gravatar_url

class UserManager(models.Manager):
    @transaction.commit_on_success
    def create_from_backend(self, rid, email, password=<PASSWORD>, verified=False):
        info = genro.user(rid)
        user = auth.models.User.objects.create_user('_' + info['user.username'], email, password)
        user.first_name = info['user.firstname']
        user.last_name = info['user.lastname']
        user.save()

        u = User(user=user)
        u.assopy_id = rid
        u.verified = verified
        u.save()
        log.debug('new local user created "%s"', user)
        return u

class User(models.Model):
    user = models.OneToOneField("auth.User", related_name='assopy_user')
    assopy_id = models.CharField(max_length=22, blank=True, unique=True)
    verified = models.BooleanField(default=False)
    photo = models.ImageField(null=True, upload_to='assopy/users')

    objects = UserManager()

    def photo_url(self):
        if self.photo:
            return self.photo.url
        try:
            return self.useridentity_set.exclude(photo='')[0]
        except IndexError:
            return _gravatar(self.user.email)

    @_cache
    def billing(self):
        return dict(genro.user(self.assopy_id))

    def setBilling(self, **kwargs):
        data = self.billing()
        data.update(kwargs)
        genro.setUser(self.assopy_id, data)

    def name(self):
        if self.assopy_id:
            u = self.billing()
            return '%s %s' % (u['firstname'], u['lastname'])
        else:<commit_msg>metodo profile per separare concettualmente i dati del profilo di quelli di fatturazione<commit_after># -*- coding: UTF-8 -*-
from assopy.clients import genro
from conference.models import Speaker

from django.contrib import auth
from django.db import models
from django.db import transaction
from django.utils.translation import ugettext_lazy as _

import logging
from datetime import date, datetime

log = logging.getLogger('assopy.models')

def _cache(f):
    """
    cache dei poveri, memorizzo nell'istanza il risultato di una richiesta
    remota (la cache dura una richiesta)
    """
    def wrapper(self, *args, **kwargs):
        key = '_c' + f.__name__
        try:
            r = getattr(self, key)
        except AttributeError:
            r = f(self, *args, **kwargs)
            setattr(self, key, r)
        return r
    return wrapper

def _gravatar(email, size=80, default='identicon', rating='r'):
    # import code for encoding urls and generating md5 hashes
    import urllib, hashlib

    gravatar_url = "http://www.gravatar.com/avatar/" + hashlib.md5(email.lower()).hexdigest() + "?"
    gravatar_url += urllib.urlencode({
        'default': default,
        'size': size,
        'rating': rating,
    })
    
    return gravatar_url

class UserManager(models.Manager):
    @transaction.commit_on_success
    def create_from_backend(self, rid, email, password=<PASSWORD>, verified=False):
        info = genro.user(rid)
        user = auth.models.User.objects.create_user('_' + info['user.username'], email, password)
        user.first_name = info['user.firstname']
        user.last_name = info['user.lastname']
        user.save()

        u = User(user=user)
        u.assopy_id = rid
        u.verified = verified
        u.save()
        log.debug('new local user created "%s"', user)
        return u

class User(models.Model):
    user = models.OneToOneField("auth.User", related_name='assopy_user')
    assopy_id = models.CharField(max_length=22, blank=True, unique=True)
    verified = models.BooleanField(default=False)
    photo = models.ImageField(null=True, upload_to='assopy/users')

    objects = UserManager()

    def photo_url(self):
        if self.photo:
            return self.photo.url
        try:
            return self.useridentity_set.exclude(photo='')[0]
        except IndexError:
            return _gravatar(self.user.email)

    @_cache
    def billing(self):
        return dict(genro.user(self.assopy_id))

    def setBilling(self, **kwargs):
        data = self.billing()
        data.update(kwargs)
        genro.setUser(self.assopy_id, data)

    @_cache
    def profile(self):
        billing = self.billing()
        data = {
            'photo': self.photo_url(),
            'name': self.name(),
            'www': billing['www'],
        }
        return data

    def name(self):
        if self.assopy_id:
            u = self.billing()
            return '%s %s' % (u['firstname'], u['lastname'])
        else:
{'fraction_non_alphanumeric': 0.11948464525238263, 'fraction_numerical': 0.0028238616307800918, 'mean_word_length': 2.644372990353698, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '1406347', 'n_tokens_mistral': 1762, 'n_tokens_neox': 1738, 'n_words': 423}