# Path: antioch/core/models.py
# class URLField(models.CharField):
# class Object(models.Model):
#     class Meta:
# class Relationship(models.Model):
#     class Meta:
# class Observation(models.Model):
#     class Meta:
# class Alias(models.Model):
#     class Meta:
# class Repository(models.Model):
#     class Meta:
# class Verb(models.Model):
#     class Meta:
# class VerbName(models.Model):
#     class Meta:
# class Property(models.Model):
#     class Meta:
# class Permission(models.Model):
#     class Meta:
# class Access(models.Model):
#     class Meta:
# class Player(models.Model):
#     class Meta:
# class Task(models.Model):
#     class Meta:
#     def __str__(self):
#     def __str__(self):
#     def annotated(self):
#     def name(self):
#     def __str__(self):
#     def __str__(self):
#     def __str__(self):
#     def actor(self):
#     def entity(self):
#     def origin(self):
#     def __str__(self):
#     def __str__(self):
#     def is_authenticated(self):
#     def is_active(self):
#     def is_staff(self):
#     def is_superuser(self):
#     def has_module_perms(self, app):
#     def has_perm(self, perm):
#     def email(self):
# 
# Path: antioch/plugins/signup/forms.py
# class SignupForm(forms.ModelForm):
#     class Meta:
#     def clean(self):
# 
# Path: antioch/plugins/signup/tasks.py
# def addplayer(name, passwd, enabled=True):
# def enableplayer(player_id):
# 
# Path: antioch/plugins/signup/models.py
# class RegisteredPlayer(Player):
#     email = models.CharField(max_length=255)
#     activation_key = models.CharField(max_length=255)


import hashlib, random, logging
from antioch.core import models
from antioch.plugins.signup import forms, tasks
from antioch.plugins.signup.models import RegisteredPlayer
from registration.signals import user_registered, user_activated
from registration.backends.default.views import ActivationView as BaseActivationView
from registration.backends.default.views import RegistrationView as BaseRegistrationView
"""





log = logging.getLogger(__name__)

def get_activation_key(username):
    salt = hashlib.sha1(str(random.random())).hexdigest()[:5]
    if isinstance(username, str):
        username = username.encode('utf-8')
    return hashlib.sha1(salt+username).hexdigest()

class RegistrationView(BaseRegistrationView):
    form_class = forms.SignupForm
    
    def register(self, request, **cleaned_data):
        user_id = tasks.addplayer.delay(
            name    = cleaned_data['character_name'],
            passwd    = cleaned_data['passwd'],
            enabled    = False,
        ).get(timeout=5)
        
        assert user_id is not None, "Didn't get an entity ID back from addplayer"
        
        player = models.Player.objects.get(avatar_id=user_id)
        activation_key = get_activation_key(cleaned_data['character_name'])
        
        # it's a hack, but it works well

# Path: antioch/core/models.py
# class URLField(models.CharField):
# class Object(models.Model):
#     class Meta:
# class Relationship(models.Model):
#     class Meta:
# class Observation(models.Model):
#     class Meta:
# class Alias(models.Model):
#     class Meta:
# class Repository(models.Model):
#     class Meta:
# class Verb(models.Model):
#     class Meta:
# class VerbName(models.Model):
#     class Meta:
# class Property(models.Model):
#     class Meta:
# class Permission(models.Model):
#     class Meta:
# class Access(models.Model):
#     class Meta:
# class Player(models.Model):
#     class Meta:
# class Task(models.Model):
#     class Meta:
#     def __str__(self):
#     def __str__(self):
#     def annotated(self):
#     def name(self):
#     def __str__(self):
#     def __str__(self):
#     def __str__(self):
#     def actor(self):
#     def entity(self):
#     def origin(self):
#     def __str__(self):
#     def __str__(self):
#     def is_authenticated(self):
#     def is_active(self):
#     def is_staff(self):
#     def is_superuser(self):
#     def has_module_perms(self, app):
#     def has_perm(self, perm):
#     def email(self):
# 
# Path: antioch/plugins/signup/forms.py
# class SignupForm(forms.ModelForm):
#     class Meta:
#     def clean(self):
# 
# Path: antioch/plugins/signup/tasks.py
# def addplayer(name, passwd, enabled=True):
# def enableplayer(player_id):
# 
# Path: antioch/plugins/signup/models.py
# class RegisteredPlayer(Player):
#     email = models.CharField(max_length=255)
#     activation_key = models.CharField(max_length=255)


# Path: antioch/plugins/signup/views.py
import hashlib, random, logging
from antioch.core import models
from antioch.plugins.signup import forms, tasks
from antioch.plugins.signup.models import RegisteredPlayer
from registration.signals import user_registered, user_activated
from registration.backends.default.views import ActivationView as BaseActivationView
from registration.backends.default.views import RegistrationView as BaseRegistrationView

"""





log = logging.getLogger(__name__)

def get_activation_key(username):
    salt = hashlib.sha1(str(random.random())).hexdigest()[:5]
    if isinstance(username, str):
        username = username.encode('utf-8')
    return hashlib.sha1(salt+username).hexdigest()

class RegistrationView(BaseRegistrationView):
    form_class = forms.SignupForm
    
    def register(self, request, **cleaned_data):
        user_id = tasks.addplayer.delay(
            name    = cleaned_data['character_name'],
            passwd    = cleaned_data['passwd'],
            enabled    = False,
        ).get(timeout=5)
        
        assert user_id is not None, "Didn't get an entity ID back from addplayer"
        
        player = models.Player.objects.get(avatar_id=user_id)
        activation_key = get_activation_key(cleaned_data['character_name'])
        
        # it's a hack, but it works well

        player.__class__ = models.RegisteredPlayer