<commit_before># -*- coding: utf-8  -*-
from django import forms
from django.contrib.auth import authenticate
from django.utils.translation import ugettext as _

from accounts.models import CommonProfile
from bulb.models import Point
from clubs.models import College, city_choices, college_choices, section_choices, gender_choices
from core.models import StudentClubYear
import core.utils
from userena.forms import SignupForm


class EnjazSignupForm(SignupForm):
    # FIXME: Dirty 'verbose_name' hack?
    ar_first_name = forms.CharField(label=CommonProfile._meta.get_field('ar_first_name').verbose_name,
                                max_length=30)
    ar_middle_name = forms.CharField(label=CommonProfile._meta.get_field('ar_middle_name').verbose_name,
                                max_length=30)
    ar_last_name = forms.CharField(label=CommonProfile._meta.get_field('ar_last_name').verbose_name,
                                max_length=30)
    en_first_name = forms.CharField(label=CommonProfile._meta.get_field('en_first_name').verbose_name,
                                max_length=30)
    en_middle_name = forms.CharField(label=CommonProfile._meta.get_field('en_middle_name').verbose_name,
                                max_length=30)
    en_last_name = forms.CharField(label=CommonProfile._meta.get_field('en_last_name').verbose_name,
                                max_length=30)
    badge_number = forms.IntegerField(label=CommonProfile._meta.get_field('badge_number').verbose_name, required=False)
    city = forms.CharField(label=u"المدينة", max_length=1, widget=forms.Select(choices=city_choices))

    def __init__(self, *args, **kw):
        super(EnjazSignupForm, self).__init__(*args, **kw)
        # We don't want usernames (We could have inherited userena's
        # SignupFormOnlyEmail, but it's more tricky to modify.)
        del self.fields['username']

    def clean(self):
        # Call the parent class's clean function.
        cleaned_data = super(EnjazSignupForm, self).clean()

        # Remove spaces at the start and end of all text fields.
        for field in cleaned_data:
            if isinstance(cleaned_data[field], unicode):
                cleaned_data[field] = cleaned_data[field].strip()

        # Make sure that the email is of the university or the hospital.
        if 'email' in cleaned_data and not \
           cleaned_data['email'].endswith('ksau-hs.edu.sa') and \
           not cleaned_data['email'].endswith('ngha.med.sa'):
            email_msg = u"أدخل عنوانا جامعيا"
            self._errors['email'] = self.error_class([email_msg])
            del cleaned_data['email']

        # Make sure that the mobile numbers contain only digits and
        # pluses:
        if 'mobile_number' in cleaned_data:
            mobile_number_msg = ""
            cleaned_data['mobile_number'] = core.utils.hindi_to_arabic(cleaned_data['mobile_number'])
            if len(cleaned_data['mobile_number']) < 10:
                mobile_number_msg = u"الرقم الذي أدخلت ناقص"
            if not all([number in '1234567890+' for number in cleaned_data['mobile_number']]):
                mobile_number_msg = u"أدخل أرقاما فقط"
            if mobile_number_msg:
                self._errors['mobile_number'] = self.error_class([mobile_number_msg])
                del cleaned_data['mobile_number']

        return cleaned_data

    def save(self):
        # All username names should be lower-case.
        self.cleaned_data['email'] = self.cleaned_data['email'].lower()
        self.cleaned_data['username'] = self.cleaned_data['email'].split('@')[0]
        self.cleaned_data['username'] = self.cleaned_data['username']
        new_user = super(EnjazSignupForm, self).save()
        return new_user

class StudentSignupForm(EnjazSignupForm):
    """
    A form that includes extra student-related fields.
    """
    student_id = forms.IntegerField(label=CommonProfile._meta.get_field('student_id').verbose_name, required=False)
    # Since the mobile number starts with a zero, store it as a
    # string.
    mobile_number = forms.CharField(label=CommonProfile._meta.get_field('mobile_number').verbose_name)
    section = forms.CharField(label=u"القسم", max_length=2, widget=forms.Select(choices=section_choices), required=False)
    college = forms.CharField(label=CommonProfile._meta.get_field('college').verbose_name, max_length=1, widget=forms.Select(choices=college_choices))
    gender = forms.CharField(label=u"الجنس", max_length=1, widget=forms.Select(choices=gender_choices))

    def clean(self):
        # Call the parent class's clean function.
        cleaned_data = super(StudentSignupForm, self).clean()

        # Since Jeddah and Al-Hasa have only one campus, the section
        # equals the city.
        if 'city' in cleaned_data and \
           cleaned_data['city'] in ['J', 'A']:
            cleaned_data['section'] = cleaned_data['city']

        # Make sure that the college/section choice is actually valid.
        try:
            self.college = College.objects.get(
                name=cleaned_data['college'],
                city=cleaned_data['city'],
                section=cleaned_data['section'],
                gender=cleaned_data['gender'])
        except College.DoesNotExist:
            college_msg = u"ليست كلية مسجلة."
            # Add an error message to specific fields.
            self._errors['college'] = self.error_class([college_msg])
            self._errors['section'] = self.error_class([college_msg])
            self._errors['gender'] = self.error_class([college_msg])
            # Remove invalid fields
            del cleaned_data['college']
            del cleaned_data['section']
            del cleaned_data['gender']

        return cleaned_data

    def save(self):
        # Save the parent form and get the user
        new_user = super(StudentSignupForm, self).save()

        # Add initial Bulb balance.
        current_year = StudentClubYear.objects.get_current()
        Point.objects.create(year=current_year,
                             user=new_user,
                             note=u"رصيد مبدئي.",
                             value=1)

        CommonProfile.objects.create(user=new_user,
                                     is_student=True,
                                     ar_first_name=self.cleaned_data['ar_first_name'],
                                     ar_middle_name=self.cleaned_data['ar_middle_name'],
                                     ar_last_name=self.cleaned_data['ar_last_name'],
                                     en_first_name=self.cleaned_data['en_first_name'],
                                     en_middle_name=self.cleaned_data['en_middle_name'],
                                     en_last_name=self.cleaned_data['en_last_name'],
                                     badge_number=self.cleaned_data.get('badge_number'),
                                     city=self.cleaned_data['city'],
                                     student_id=self.cleaned_data.get('student_id'),
                                     mobile_number=self.cleaned_data['mobile_number'],
                                     college=self.college,
                                     job_description="")
        return new_user

class NonStudentSignupForm(EnjazSignupForm):
    """
    A form that includes extra non-student-related fields.
    """
    # Since the mobile number starts with a zero, store it as a
    # string.  For non-students, this field is going to be optional.
    mobile_number = forms.CharField(label=CommonProfile._meta.get_field('mobile_number').verbose_name,<commit_msg>Add two points to new users.<commit_after># -*- coding: utf-8  -*-
from django import forms
from django.contrib.auth import authenticate
from django.utils.translation import ugettext as _

from accounts.models import CommonProfile
from bulb.models import Point
from clubs.models import College, city_choices, college_choices, section_choices, gender_choices
from core.models import StudentClubYear
import core.utils
from userena.forms import SignupForm


class EnjazSignupForm(SignupForm):
    # FIXME: Dirty 'verbose_name' hack?
    ar_first_name = forms.CharField(label=CommonProfile._meta.get_field('ar_first_name').verbose_name,
                                max_length=30)
    ar_middle_name = forms.CharField(label=CommonProfile._meta.get_field('ar_middle_name').verbose_name,
                                max_length=30)
    ar_last_name = forms.CharField(label=CommonProfile._meta.get_field('ar_last_name').verbose_name,
                                max_length=30)
    en_first_name = forms.CharField(label=CommonProfile._meta.get_field('en_first_name').verbose_name,
                                max_length=30)
    en_middle_name = forms.CharField(label=CommonProfile._meta.get_field('en_middle_name').verbose_name,
                                max_length=30)
    en_last_name = forms.CharField(label=CommonProfile._meta.get_field('en_last_name').verbose_name,
                                max_length=30)
    badge_number = forms.IntegerField(label=CommonProfile._meta.get_field('badge_number').verbose_name, required=False)
    city = forms.CharField(label=u"المدينة", max_length=1, widget=forms.Select(choices=city_choices))

    def __init__(self, *args, **kw):
        super(EnjazSignupForm, self).__init__(*args, **kw)
        # We don't want usernames (We could have inherited userena's
        # SignupFormOnlyEmail, but it's more tricky to modify.)
        del self.fields['username']

    def clean(self):
        # Call the parent class's clean function.
        cleaned_data = super(EnjazSignupForm, self).clean()

        # Remove spaces at the start and end of all text fields.
        for field in cleaned_data:
            if isinstance(cleaned_data[field], unicode):
                cleaned_data[field] = cleaned_data[field].strip()

        # Make sure that the email is of the university or the hospital.
        if 'email' in cleaned_data and not \
           cleaned_data['email'].endswith('ksau-hs.edu.sa') and \
           not cleaned_data['email'].endswith('ngha.med.sa'):
            email_msg = u"أدخل عنوانا جامعيا"
            self._errors['email'] = self.error_class([email_msg])
            del cleaned_data['email']

        # Make sure that the mobile numbers contain only digits and
        # pluses:
        if 'mobile_number' in cleaned_data:
            mobile_number_msg = ""
            cleaned_data['mobile_number'] = core.utils.hindi_to_arabic(cleaned_data['mobile_number'])
            if len(cleaned_data['mobile_number']) < 10:
                mobile_number_msg = u"الرقم الذي أدخلت ناقص"
            if not all([number in '1234567890+' for number in cleaned_data['mobile_number']]):
                mobile_number_msg = u"أدخل أرقاما فقط"
            if mobile_number_msg:
                self._errors['mobile_number'] = self.error_class([mobile_number_msg])
                del cleaned_data['mobile_number']

        return cleaned_data

    def save(self):
        # All username names should be lower-case.
        self.cleaned_data['email'] = self.cleaned_data['email'].lower()
        self.cleaned_data['username'] = self.cleaned_data['email'].split('@')[0]
        self.cleaned_data['username'] = self.cleaned_data['username']
        new_user = super(EnjazSignupForm, self).save()
        return new_user

class StudentSignupForm(EnjazSignupForm):
    """
    A form that includes extra student-related fields.
    """
    student_id = forms.IntegerField(label=CommonProfile._meta.get_field('student_id').verbose_name, required=False)
    # Since the mobile number starts with a zero, store it as a
    # string.
    mobile_number = forms.CharField(label=CommonProfile._meta.get_field('mobile_number').verbose_name)
    section = forms.CharField(label=u"القسم", max_length=2, widget=forms.Select(choices=section_choices), required=False)
    college = forms.CharField(label=CommonProfile._meta.get_field('college').verbose_name, max_length=1, widget=forms.Select(choices=college_choices))
    gender = forms.CharField(label=u"الجنس", max_length=1, widget=forms.Select(choices=gender_choices))

    def clean(self):
        # Call the parent class's clean function.
        cleaned_data = super(StudentSignupForm, self).clean()

        # Since Jeddah and Al-Hasa have only one campus, the section
        # equals the city.
        if 'city' in cleaned_data and \
           cleaned_data['city'] in ['J', 'A']:
            cleaned_data['section'] = cleaned_data['city']

        # Make sure that the college/section choice is actually valid.
        try:
            self.college = College.objects.get(
                name=cleaned_data['college'],
                city=cleaned_data['city'],
                section=cleaned_data['section'],
                gender=cleaned_data['gender'])
        except College.DoesNotExist:
            college_msg = u"ليست كلية مسجلة."
            # Add an error message to specific fields.
            self._errors['college'] = self.error_class([college_msg])
            self._errors['section'] = self.error_class([college_msg])
            self._errors['gender'] = self.error_class([college_msg])
            # Remove invalid fields
            del cleaned_data['college']
            del cleaned_data['section']
            del cleaned_data['gender']

        return cleaned_data

    def save(self):
        # Save the parent form and get the user
        new_user = super(StudentSignupForm, self).save()

        # Add initial Bulb balance.
        current_year = StudentClubYear.objects.get_current()
        Point.objects.create(year=current_year,
                             user=new_user,
                             note=u"رصيد مبدئي.",
                             value=1,
                             category='L')
        Point.objects.create(year=current_year,
                             user=new_user,
                             note=u"رصيد مبدئي.",
                             value=1,
                             category='G')

        CommonProfile.objects.create(user=new_user,
                                     is_student=True,
                                     ar_first_name=self.cleaned_data['ar_first_name'],
                                     ar_middle_name=self.cleaned_data['ar_middle_name'],
                                     ar_last_name=self.cleaned_data['ar_last_name'],
                                     en_first_name=self.cleaned_data['en_first_name'],
                                     en_middle_name=self.cleaned_data['en_middle_name'],
                                     en_last_name=self.cleaned_data['en_last_name'],
                                     badge_number=self.cleaned_data.get('badge_number'),
                                     city=self.cleaned_data['city'],
                                     student_id=self.cleaned_data.get('student_id'),
                                     mobile_number=self.cleaned_data['mobile_number'],
                                     college=self.college,
                                     job_description="")
        return new_user

class NonStudentSignupForm(EnjazSignupForm):
    """
    A form that includes extra non-student-related fields.
    """
    # Since the mobile number starts with a zero, store it as a
    # string.  For non-students, this field is going to be optional.
    mobile_number = forms.CharField(label=CommonProfile._meta.get_field('mobile_number').verbose_name,
{'fraction_non_alphanumeric': 0.09778206364513019, 'fraction_numerical': 0.004050144648023143, 'mean_word_length': 2.34106529209622, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '460978', 'n_tokens_mistral': 4497, 'n_tokens_neox': 4353, 'n_words': 849}