# Path: pytask/profile/forms.py
# class EditProfileForm(forms.ModelForm):
# 
#     class Meta:
#         model = Profile
#         fields = ['full_name', 'aboutme', 'gender', 'dob', 'address', 'phonenum']
# 
#     def clean_aboutme(self):
#         """ Empty not allowed """
# 
#         data = self.cleaned_data['aboutme']
#         if not data.strip():
#             raise forms.ValidationError("Please write something about\
#                                         yourself")
# 
#         return data
# 
#     def clean_address(self):
#         """ Empty not allowed """
# 
#         data = self.cleaned_data['address']
#         if not data.strip():
#             raise forms.ValidationError("Please enter an address")
#         
#         return data
# 
#     def clean_phonenum(self):
#         """ should be of 10 digits """
# 
#         data = self.cleaned_data['phonenum']
# 
#         if (not data.strip()) or \
#            (data.strip("1234567890")) or \
#            (len(data)!= 10):
#                raise forms.ValidationError("This is not a valid phone number")
# 
#         return data
# 
# Path: pytask/profile/utils.py
# def get_notification(nid, user):
#     """ if notification exists, and belongs to the current user, return it.
#     else return None.
#     """
# 
#     user_notifications = user.notification_sent_to.filter(is_deleted=False).order_by('sent_date')
#     current_notifications = user_notifications.filter(pk=nid)
#     if user_notifications:
#         current_notification = current_notifications[0]
# 
#         try:
#             newer_notification = current_notification.get_next_by_sent_date(sent_to=user, is_deleted=False)
#             newest_notification = user_notifications.reverse()[0]
#             if newest_notification == newer_notification:
#                 newest_notification = None
#         except Notification.DoesNotExist:
#             newest_notification, newer_notification = None, None
# 
#         try:
#             older_notification = current_notification.get_previous_by_sent_date(sent_to=user, is_deleted=False)
#             oldest_notification = user_notifications[0]
#             if oldest_notification == older_notification:
#                 oldest_notification = None
#         except:
#             oldest_notification, older_notification = None, None
# 
#         return newest_notification, newer_notification, current_notification, older_notification, oldest_notification
# 
#     else:
#         return None, None, None, None, None
# 
# Path: pytask/profile/utils.py
# def get_user(uid):
# 
#     user = shortcuts.get_object_or_404(User, pk=uid)
# 
#     if user.is_active:
#         return user
#     else:
#         raise Http404


from urllib2 import urlparse
from django import http
from django import shortcuts
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.core.context_processors import csrf
from django.core.urlresolvers import reverse
from django.template import loader
from django.template import RequestContext
from django.utils import simplejson as json
from pytask.profile.forms import EditProfileForm
from pytask.profile.utils import get_notification
from pytask.profile.utils import get_user
@login_required
def delete_notification(request, notification_id):
    """ check if the user owns the notification and delete it.
    """

    user = request.user
    newest, newer, notification, older, oldest = get_notification(
      notification_id, user)

    if not notification:
        raise http.Http404

    notification.is_deleted = True
    notification.save()

    if older:
        redirect_url = reverse('view_notification',
                               kwargs={'notification_id': older.id})
    else:
        redirect_url = reverse('browse_notifications')

    return shortcuts.redirect(redirect_url)

@login_required
def unread_notification(request, notification_id):

    """ check if the user owns the notification and delete it.
    """

    user = request.user

# Path: pytask/profile/forms.py
# class EditProfileForm(forms.ModelForm):
# 
#     class Meta:
#         model = Profile
#         fields = ['full_name', 'aboutme', 'gender', 'dob', 'address', 'phonenum']
# 
#     def clean_aboutme(self):
#         """ Empty not allowed """
# 
#         data = self.cleaned_data['aboutme']
#         if not data.strip():
#             raise forms.ValidationError("Please write something about\
#                                         yourself")
# 
#         return data
# 
#     def clean_address(self):
#         """ Empty not allowed """
# 
#         data = self.cleaned_data['address']
#         if not data.strip():
#             raise forms.ValidationError("Please enter an address")
#         
#         return data
# 
#     def clean_phonenum(self):
#         """ should be of 10 digits """
# 
#         data = self.cleaned_data['phonenum']
# 
#         if (not data.strip()) or \
#            (data.strip("1234567890")) or \
#            (len(data)!= 10):
#                raise forms.ValidationError("This is not a valid phone number")
# 
#         return data
# 
# Path: pytask/profile/utils.py
# def get_notification(nid, user):
#     """ if notification exists, and belongs to the current user, return it.
#     else return None.
#     """
# 
#     user_notifications = user.notification_sent_to.filter(is_deleted=False).order_by('sent_date')
#     current_notifications = user_notifications.filter(pk=nid)
#     if user_notifications:
#         current_notification = current_notifications[0]
# 
#         try:
#             newer_notification = current_notification.get_next_by_sent_date(sent_to=user, is_deleted=False)
#             newest_notification = user_notifications.reverse()[0]
#             if newest_notification == newer_notification:
#                 newest_notification = None
#         except Notification.DoesNotExist:
#             newest_notification, newer_notification = None, None
# 
#         try:
#             older_notification = current_notification.get_previous_by_sent_date(sent_to=user, is_deleted=False)
#             oldest_notification = user_notifications[0]
#             if oldest_notification == older_notification:
#                 oldest_notification = None
#         except:
#             oldest_notification, older_notification = None, None
# 
#         return newest_notification, newer_notification, current_notification, older_notification, oldest_notification
# 
#     else:
#         return None, None, None, None, None
# 
# Path: pytask/profile/utils.py
# def get_user(uid):
# 
#     user = shortcuts.get_object_or_404(User, pk=uid)
# 
#     if user.is_active:
#         return user
#     else:
#         raise Http404


# Path: pytask/profile/views.py
from urllib2 import urlparse
from django import http
from django import shortcuts
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.core.context_processors import csrf
from django.core.urlresolvers import reverse
from django.template import loader
from django.template import RequestContext
from django.utils import simplejson as json
from pytask.profile.forms import EditProfileForm
from pytask.profile.utils import get_notification
from pytask.profile.utils import get_user

@login_required
def delete_notification(request, notification_id):
    """ check if the user owns the notification and delete it.
    """

    user = request.user
    newest, newer, notification, older, oldest = get_notification(
      notification_id, user)

    if not notification:
        raise http.Http404

    notification.is_deleted = True
    notification.save()

    if older:
        redirect_url = reverse('view_notification',
                               kwargs={'notification_id': older.id})
    else:
        redirect_url = reverse('browse_notifications')

    return shortcuts.redirect(redirect_url)

@login_required
def unread_notification(request, notification_id):

    """ check if the user owns the notification and delete it.
    """

    user = request.user

    newest, newer, notification, older, oldest = get_notification(