views.py 1.11 KB
Newer Older
1
2
3
"""Views provided by the announcements package"""
from django.http import HttpResponse, HttpResponseBadRequest
from django.views.decorators.http import require_POST
Joost Rijneveld's avatar
Joost Rijneveld committed
4
5


6
@require_POST
Joost Rijneveld's avatar
Joost Rijneveld committed
7
def close_announcement(request):
8
9
10
11
12
13
14
    """Close an announcement

    :param: request
    :return: Http 204 No Content if successful
    """
    if 'id' not in request.POST:
        return HttpResponseBadRequest("no id specified")
15
16
17
18
19
    try:
        announcement_id = int(request.POST['id'])
    except ValueError:
        return HttpResponseBadRequest("no integer id specified")

Joost Rijneveld's avatar
Joost Rijneveld committed
20
21
22
23
    # if we do not have a list of closed announcements yet:
    if 'closed_announcements' not in request.session:
        request.session['closed_announcements'] = []  # cannot use sets here :(
    # duplicates should never occur anyway, but it does not hurt to check
24
25
    if announcement_id not in request.session['closed_announcements']:
        request.session['closed_announcements'].append(announcement_id)
Joost Rijneveld's avatar
Joost Rijneveld committed
26
27
28
    # needs to be explicitly marked since we only edited an existing object
    request.session.modified = True
    return HttpResponse(status=204)  # 204: No Content