# Path: remo/profiles/forms.py
# 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)
#         today = get_date()
# 
#         if 'start_date' in cdata:
#             if cdata['start_date'] < today:
#                 msg = u'Start date cannot be in the past.'
#                 self._errors['start_date'] = self.error_class([msg])
# 
#         if 'expected_date' in cdata:
#             start_date = cdata['start_date']
#             expected_date = cdata['expected_date']
#             max_period = start_date + timedelta(weeks=MAX_UNAVAILABILITY_PERIOD)
# 
#             if expected_date < tomorrow:
#                 msg = (u'Return day cannot be earlier than {0}'
#                        .format(tomorrow.strftime('%d %B %Y')))
#                 self._errors['expected_date'] = self.error_class([msg])
#             if expected_date < start_date:
#                 msg = u'Return date cannot be before start date.'
#                 self._errors['expected_date'] = self.error_class([msg])
#             if expected_date > max_period:
#                 msg = (u'The maximum period for unavailability is until {0}.'
#                        .format(max_period.strftime('%d %B %Y')))
#                 sop = mark_safe(msg + (' For more information please check '
#                                        'the %s') % LEAVING_SOP_URL)
#                 self._errors['expected_date'] = self.error_class([sop])
# 
#         if ('is_replaced' in cdata
#                 and cdata['is_replaced'] and not cdata['replacement_rep']):
#             msg = 'Please select a replacement Rep during your absence.'
#             self._errors['replacement_rep'] = self.error_class([msg])
# 
#         return cdata
# 
#     class Meta:
#         model = UserStatus
#         fields = ['start_date', 'expected_date', 'replacement_rep']


import feedparser
import json
import logging
import requests
import forms
import utils
from django import http
from django.db.models import Q
from django.conf import settings
from django.contrib import messages
from django.contrib.auth.models import User
from django.core.cache import cache
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST
from django.http import Http404, HttpResponse, HttpResponseBadRequest
from django.shortcuts import redirect, render
from django.views import generic
from django.views.decorators.cache import cache_control, never_cache
from django_statsd.clients import statsd
from mozilla_django_oidc.views import OIDCAuthenticationCallbackView
from raven.contrib.django.models import client
from remo.base.decorators import PermissionMixin, permission_check
from remo.base.forms import EmailMentorForm
from remo.featuredrep.models import FeaturedRep
from remo.profiles.forms import UserStatusForm
from remo.profiles.models import UserProfile, UserStatus
    return render(request, 'settings.jinja', {'user': user,
                                              'settingsform': form})


@never_cache
@permission_check(permissions=['profiles.add_userstatus',
                               'profiles.change_userstatus'],
                  filter_field='display_name', owner_field='user',
                  model=UserProfile)
def edit_availability(request, display_name):
    """Edit availability settings."""

    user = request.user
    args = {}
    created = False

    if user.groups.filter(Q(name='Mozillians') | Q(name='Alumni')).exists():
        raise Http404()

    try:
        status = (UserStatus.objects.filter(user=user)
                  .filter(is_unavailable=True).latest('created_on'))
    except UserStatus.DoesNotExist:
        status = UserStatus(user=user)
        created = True

    initial_data = {'is_replaced': False}
    if not created:
        initial_data['expected_date'] = (status.expected_date
                                         .strftime('%d %B %Y'))

# Path: remo/profiles/forms.py
# 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)
#         today = get_date()
# 
#         if 'start_date' in cdata:
#             if cdata['start_date'] < today:
#                 msg = u'Start date cannot be in the past.'
#                 self._errors['start_date'] = self.error_class([msg])
# 
#         if 'expected_date' in cdata:
#             start_date = cdata['start_date']
#             expected_date = cdata['expected_date']
#             max_period = start_date + timedelta(weeks=MAX_UNAVAILABILITY_PERIOD)
# 
#             if expected_date < tomorrow:
#                 msg = (u'Return day cannot be earlier than {0}'
#                        .format(tomorrow.strftime('%d %B %Y')))
#                 self._errors['expected_date'] = self.error_class([msg])
#             if expected_date < start_date:
#                 msg = u'Return date cannot be before start date.'
#                 self._errors['expected_date'] = self.error_class([msg])
#             if expected_date > max_period:
#                 msg = (u'The maximum period for unavailability is until {0}.'
#                        .format(max_period.strftime('%d %B %Y')))
#                 sop = mark_safe(msg + (' For more information please check '
#                                        'the %s') % LEAVING_SOP_URL)
#                 self._errors['expected_date'] = self.error_class([sop])
# 
#         if ('is_replaced' in cdata
#                 and cdata['is_replaced'] and not cdata['replacement_rep']):
#             msg = 'Please select a replacement Rep during your absence.'
#             self._errors['replacement_rep'] = self.error_class([msg])
# 
#         return cdata
# 
#     class Meta:
#         model = UserStatus
#         fields = ['start_date', 'expected_date', 'replacement_rep']


# Path: remo/base/views.py
import feedparser
import json
import logging
import requests
import forms
import utils
from django import http
from django.db.models import Q
from django.conf import settings
from django.contrib import messages
from django.contrib.auth.models import User
from django.core.cache import cache
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST
from django.http import Http404, HttpResponse, HttpResponseBadRequest
from django.shortcuts import redirect, render
from django.views import generic
from django.views.decorators.cache import cache_control, never_cache
from django_statsd.clients import statsd
from mozilla_django_oidc.views import OIDCAuthenticationCallbackView
from raven.contrib.django.models import client
from remo.base.decorators import PermissionMixin, permission_check
from remo.base.forms import EmailMentorForm
from remo.featuredrep.models import FeaturedRep
from remo.profiles.forms import UserStatusForm
from remo.profiles.models import UserProfile, UserStatus

    return render(request, 'settings.jinja', {'user': user,
                                              'settingsform': form})


@never_cache
@permission_check(permissions=['profiles.add_userstatus',
                               'profiles.change_userstatus'],
                  filter_field='display_name', owner_field='user',
                  model=UserProfile)
def edit_availability(request, display_name):
    """Edit availability settings."""

    user = request.user
    args = {}
    created = False

    if user.groups.filter(Q(name='Mozillians') | Q(name='Alumni')).exists():
        raise Http404()

    try:
        status = (UserStatus.objects.filter(user=user)
                  .filter(is_unavailable=True).latest('created_on'))
    except UserStatus.DoesNotExist:
        status = UserStatus(user=user)
        created = True

    initial_data = {'is_replaced': False}
    if not created:
        initial_data['expected_date'] = (status.expected_date
                                         .strftime('%d %B %Y'))

    status_form = UserStatusForm(request.POST or None,