# Path: remo/base/templatetags/helpers.py
# @library.global_function
# def user_is_rep(user):
#     """Check if a user belongs to Rep group."""
#     return (user.groups.filter(name='Rep').exists()
#             and user.userprofile.registration_complete)
# 
# Path: remo/base/utils.py
# def get_date(days=0, weeks=0):
#     """Return a date in UTC timezone, given an offset in days and or weeks.
# 
#     The calculation is based on the current date and the
#     offset can be either positive or negative.
#     """
# 
#     return (timezone.now().date() + datetime.timedelta(days=days, weeks=weeks))


import happyforms
import re
from datetime import timedelta
from django import forms
from django.conf import settings
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.forms.extras.widgets import SelectDateWidget
from django.utils.safestring import mark_safe
from django.utils.timezone import now
from mozilla_django_oidc.auth import default_username_algo
from product_details import product_details
from pytz import common_timezones
from remo.base.templatetags.helpers import user_is_rep
from remo.base.utils import get_date
from remo.profiles.models import (FunctionalArea, MobilisingInterest, MobilisingSkill,
                                  UserProfile, UserStatus)


class UserStatusForm(happyforms.ModelForm):
    """Form for displaying info regarding the availability status of a user."""
    start_date = forms.DateField(input_formats=['%d %B %Y'])
    expected_date = forms.DateField(input_formats=['%d %B %Y'])
    is_replaced = forms.BooleanField(widget=forms.RadioSelect(
        choices=BOOLEAN_CHOICES, attrs={'id': 'id_is_replaced'}),
        required=False)

    def __init__(self, *args, **kwargs):
        super(UserStatusForm, self).__init__(*args, **kwargs)
        query = (User.objects.filter(
            groups__name='Rep', userprofile__registration_complete=True)
            .exclude(id=self.instance.user.id).order_by('first_name'))
        self.fields['replacement_rep'].queryset = query

        if self.instance.id:
            self.fields['expected_date'].widget = forms.HiddenInput()
            self.fields['start_date'].widget = forms.HiddenInput()
            self.fields['start_date'].required = False

    def clean(self):
        """Clean Form."""
        cdata = super(UserStatusForm, self).clean()

        if self.instance.id:
            cdata['start_date'] = self.instance.start_date
            return cdata


# Path: remo/base/templatetags/helpers.py
# @library.global_function
# def user_is_rep(user):
#     """Check if a user belongs to Rep group."""
#     return (user.groups.filter(name='Rep').exists()
#             and user.userprofile.registration_complete)
# 
# Path: remo/base/utils.py
# def get_date(days=0, weeks=0):
#     """Return a date in UTC timezone, given an offset in days and or weeks.
# 
#     The calculation is based on the current date and the
#     offset can be either positive or negative.
#     """
# 
#     return (timezone.now().date() + datetime.timedelta(days=days, weeks=weeks))


# Path: remo/profiles/forms.py
import happyforms
import re
from datetime import timedelta
from django import forms
from django.conf import settings
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.forms.extras.widgets import SelectDateWidget
from django.utils.safestring import mark_safe
from django.utils.timezone import now
from mozilla_django_oidc.auth import default_username_algo
from product_details import product_details
from pytz import common_timezones
from remo.base.templatetags.helpers import user_is_rep
from remo.base.utils import get_date
from remo.profiles.models import (FunctionalArea, MobilisingInterest, MobilisingSkill,
                                  UserProfile, UserStatus)



class UserStatusForm(happyforms.ModelForm):
    """Form for displaying info regarding the availability status of a user."""
    start_date = forms.DateField(input_formats=['%d %B %Y'])
    expected_date = forms.DateField(input_formats=['%d %B %Y'])
    is_replaced = forms.BooleanField(widget=forms.RadioSelect(
        choices=BOOLEAN_CHOICES, attrs={'id': 'id_is_replaced'}),
        required=False)

    def __init__(self, *args, **kwargs):
        super(UserStatusForm, self).__init__(*args, **kwargs)
        query = (User.objects.filter(
            groups__name='Rep', userprofile__registration_complete=True)
            .exclude(id=self.instance.user.id).order_by('first_name'))
        self.fields['replacement_rep'].queryset = query

        if self.instance.id:
            self.fields['expected_date'].widget = forms.HiddenInput()
            self.fields['start_date'].widget = forms.HiddenInput()
            self.fields['start_date'].required = False

    def clean(self):
        """Clean Form."""
        cdata = super(UserStatusForm, self).clean()

        if self.instance.id:
            cdata['start_date'] = self.instance.start_date
            return cdata


        tomorrow = get_date(days=1)