# Universal Subtitles, universalsubtitles.org
# 
# Copyright (C) 2010 Participatory Culture Foundation
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# 
# This program 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 Affero General Public License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see 
# http://www.gnu.org/licenses/agpl-3.0.html.

from django import forms
from auth.models import CustomUser as User, UserLanguage
from django.core.mail import EmailMessage
from django.forms.models import inlineformset_factory
from django.utils.translation import ugettext_lazy as _
from utils.validators import MaxFileSizeValidator
from django.conf import settings

UserLanguageFormset = inlineformset_factory(User, UserLanguage, extra=1)

class SendMessageForm(forms.Form):
    email = forms.EmailField()
    message = forms.CharField()
    user = forms.ModelChoiceField(User.objects)
    
    def __init__(self, sender, *args, **kwargs):
        self.sender = sender
        super(SendMessageForm, self).__init__(*args, **kwargs)
    
    def clean_user(self):
        user = self.cleaned_data['user']
        if not user.email:
            raise forms.ValidationError(_(u'This user has not email'))
        return user
    
    def clean(self):
        if not self.sender.is_authenticated():
            raise forms.ValidationError(_(u'You are not authenticated.'))
        return self.cleaned_data
    
    def send(self):
        user = self.cleaned_data.get('user')
        email = self.cleaned_data.get('email')
        headers = {'Reply-To': email}
        subject = _('Personal message from %(sender)s on universalsubtitles.org') % {'sender': self.sender.username}
        EmailMessage(subject, self.cleaned_data.get('message'), email, \
                     [user.email], headers=headers).send()

    def get_errors(self):
        from django.utils.encoding import force_unicode        
        output = {}
        for key, value in self.errors.items():
            output[key] = '/n'.join([force_unicode(i) for i in value])
        return output
                         
class EditUserForm(forms.ModelForm):
    current_password = forms.CharField(widget=forms.PasswordInput, required=False)
    new_password = forms.CharField(widget=forms.PasswordInput, required=False)
    new_password_verify = forms.CharField(widget=forms.PasswordInput,
                                          required=False,
                                          label=_(u'Confirm new password:'))
    picture = forms.ImageField(validators=[MaxFileSizeValidator(settings.AVATAR_MAX_SIZE)])
    
    def __init__(self, *args, **kwargs):
        super(EditUserForm, self).__init__(*args, **kwargs)
        self.fields['email'].required = True
    
    class Meta:
        model = User
        fields = ('first_name', 'last_name', 'email', 'homepage', 'preferred_language', 'picture', 
                  'changes_notification', 'biography')
        
    def clean(self):
        current, new, verify = map(self.cleaned_data.get,
                    ('current_password', 'new_password', 'new_password_verify'))
        if current and not self.instance.check_password(current):
            raise forms.ValidationError(_(u'Invalid password.'))
        if new and new != verify:
            raise forms.ValidationError(_(u'The two passwords did not match.'))
        if 'picture' in self.cleaned_data and not self.cleaned_data.get('picture'):
            del self.cleaned_data['picture']
        return self.cleaned_data
    
    def clean_email(self):
        value = self.cleaned_data['email']
        if value:
            try:
                User.objects.exclude(pk=self.instance.pk).get(email=value)
                raise forms.ValidationError(_(u'This email is used already.'))
            except User.DoesNotExist:
                pass
        return value
    
    def save(self, commit=True):
        password = self.cleaned_data.get('new_password')
        email = self.cleaned_data.get('email')
        if password:
            self.instance.set_password(password)
        if email:
            self.instance.email = email
            self.instance.save()
        return super(EditUserForm, self).save(commit)
        
@@ -3935,352 +3935,8 @@
    %0A
-    def clean_email(self):%0A        value = self.cleaned_data%5B'email'%5D%0A        if value:%0A            try:%0A                User.objects.exclude(pk=self.instance.pk).get(email=value)%0A                raise forms.ValidationError(_(u'This email is used already.'))%0A            except User.DoesNotExist:%0A                pass%0A        return value%0A    %0A
     

fix edit profile form