pytask/profile/views.py
changeset 69 c6bca38c1cbf
parent 63 e346fd52baff
child 140 8fcde6f8f750
equal deleted inserted replaced
68:5ff1fc726848 69:c6bca38c1cbf
       
     1 from django.shortcuts import render_to_response, redirect
       
     2 from django.http import Http404
       
     3 
       
     4 from django.contrib.auth.decorators import login_required
       
     5 from django.core.context_processors import csrf
       
     6 from django.views.decorators.csrf import csrf_protect
       
     7 
       
     8 from pytask.profile.forms import EditProfileForm
       
     9 from pytask.profile.utils import get_notification
       
    10 
       
    11 @login_required
       
    12 def view_profile(request):
       
    13     """ Display the profile information.
       
    14     """
       
    15 
       
    16     user = request.user
       
    17     profile = user.get_profile()
       
    18 
       
    19     context = {"user": user,
       
    20                "profile": profile,
       
    21               }
       
    22     return render_to_response("profile/view.html", context)
       
    23 
       
    24 @login_required
       
    25 def edit_profile(request):
       
    26     """ Make only a few fields editable.
       
    27     """
       
    28 
       
    29     user = request.user
       
    30     profile = user.get_profile()
       
    31 
       
    32     context = {"user": user,
       
    33                "profile": profile,
       
    34               }
       
    35 
       
    36     context.update(csrf(request))
       
    37 
       
    38     if request.method == "POST":
       
    39         form = EditProfileForm(request.POST, instance=profile)
       
    40 
       
    41         if form.is_valid():
       
    42             form.save()
       
    43             return redirect("/profile/view")
       
    44         else:
       
    45             context.update({"form":form})
       
    46             return render_to_response("profile/edit.html", context)
       
    47     else:
       
    48         form = EditProfileForm(instance=profile)
       
    49         context.update({"form":form})
       
    50         return render_to_response("profile/edit.html", context)
       
    51 
       
    52 @login_required
       
    53 def browse_notifications(request):
       
    54     """ get the list of notifications that are not deleted and display in
       
    55     datetime order."""
       
    56 
       
    57     user = request.user
       
    58 
       
    59     active_notifications = user.notification_sent_to.filter(is_deleted=False).order_by('sent_date').reverse()
       
    60 
       
    61     context = {'user':user,
       
    62                'notifications':active_notifications,
       
    63               }                               
       
    64 
       
    65     return render_to_response('profile/browse_notifications.html', context)
       
    66 
       
    67 @login_required
       
    68 def view_notification(request, nid):
       
    69     """ get the notification depending on nid.
       
    70     Display it.
       
    71     """
       
    72 
       
    73     user = request.user
       
    74     newest, newer, notification, older, oldest = get_notification(nid, user)
       
    75 
       
    76     if not notification:
       
    77         raise Http404
       
    78 
       
    79     notification.is_read = True
       
    80     notification.save()
       
    81 
       
    82     context = {'user':user,
       
    83                'notification':notification,
       
    84                'newest':newest,
       
    85                'newer':newer,
       
    86                'older':older,
       
    87                'oldest':oldest,
       
    88               }
       
    89 
       
    90     return render_to_response('profile/view_notification.html', context)
       
    91 
       
    92 @login_required
       
    93 def delete_notification(request, nid):
       
    94     """ check if the user owns the notification and delete it.
       
    95     """
       
    96 
       
    97     user = request.user
       
    98     newest, newer, notification, older, oldest = get_notification(nid, user)
       
    99 
       
   100     if not notification:
       
   101         raise Http404
       
   102 
       
   103     notification.is_deleted = True
       
   104     notification.save()
       
   105 
       
   106     context = {'user':user,
       
   107                'notification':notification,
       
   108                'newest':newest,
       
   109                'newer':newer,
       
   110                'older':older,
       
   111                'oldest':oldest,
       
   112               }
       
   113 
       
   114     if older:
       
   115         redirect_url = "/profile/notf/view/nid=%s"%older.uniq_key
       
   116     else:
       
   117         redirect_url = "/profile/notf/browse"
       
   118 
       
   119     return redirect(redirect_url)
       
   120 
       
   121 @login_required
       
   122 def unread_notification(request, nid):
       
   123 
       
   124     """ check if the user owns the notification and delete it.
       
   125     """
       
   126 
       
   127     user = request.user
       
   128     newest, newer, notification, older, oldest = get_notification(nid, user)
       
   129 
       
   130     if not notification:
       
   131         raise Http404
       
   132 
       
   133     notification.is_read = False
       
   134     notification.save()
       
   135 
       
   136     context = {'user':user,
       
   137                'notification':notification,
       
   138                'newest':newest,
       
   139                'newer':newer,
       
   140                'older':older,
       
   141                'oldest':oldest,
       
   142               }
       
   143 
       
   144     if older:
       
   145         redirect_url = "/profile/notf/view/nid=%s"%older.uniq_key
       
   146     else:
       
   147         redirect_url = "/profile/notf/browse"
       
   148 
       
   149     return redirect(redirect_url)