views.py 3.78 KB
Newer Older
1
from django.conf import settings
2
from django.contrib.auth.decorators import login_required
3
from django.http import FileResponse, HttpResponseRedirect
4
from django.shortcuts import render
5
from django.urls import reverse
6

7
from .models import Limit, ExternalCredit
8 9
from invitation.models import Invitation
from purist.models import AccountType
10
from cart.views import CartRegistrationView
11 12


13
@login_required
14 15 16 17
def userlimit(request):
    username = request.user.get_username()
    limits = Limit.objects.filter(user__username=username)

18 19
    # get flags for each limit
    has_limit = {}
20
    action_function = {}
21 22
    none_limit = True
    for limit in limits:
David Seaward's avatar
David Seaward committed
23 24
        label = limit.service_label().upper()
        has_limit[label] = limit.is_active
25 26
        # Default action for services
        action_function[label] = '.'
27 28 29 30 31
        if limit.is_active:
            none_limit = False

    has_limit["NONE"] = none_limit  # true if no limits are active

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    action_function["TUNNEL"] = reverse("toggle_tunnel")

    # Prepare context variables for Group service
    invitations = {'list': []}
    inv_all = request.user.invitation_set.all()
    invitations['full'] = len(inv_all) >= 4
    invitations['no_members'] = True
    invitations['links_not_used'] = False
    for inv in inv_all:
        inv_dict = {}
        inv_dict['link'] = request.build_absolute_uri(
            reverse('register_invitation', kwargs={'msg_hash': inv.msg_hash})
        )
        inv_dict['consumed'] = inv.consumed
        if inv.guest is not None:
            inv_dict['guest'] = inv.guest.username
            invitations['no_members'] = False
        else:
            inv_dict['guest'] = '---'
        inv_dict['expired'] = inv.expired()
        if not inv.expired() and not inv.consumed:
            invitations['links_not_used'] = True
        invitations['list'].append(inv_dict)
55 56 57
    ec = ExternalCredit.objects.filter(
        account_name=request.user.get_identity())
    haspaid = len(ec) > 0
58
    render_data = {
59
        "DEBUG_CHANGE_PASSWORD": settings.DEBUG_CHANGE_PASSWORD,
60 61 62
        "username": username,
        "site_title": settings.SITE_TITLE,
        "site_byline": settings.SITE_BYLINE,
63 64
        "site_provider": settings.SITE_PROVIDER,
        "site_provider_link": settings.SITE_PROVIDER_LINK,
65
        "limits": limits,
66
        "has_limit": has_limit,
67
        "link_profile_ordered_dict": settings.LINK_PROFILE_ORDERED_DICT,
68 69
        "action_function": action_function,
        "invitations": invitations,
70
        "haschosenreward": hasattr(request.user, 'chosenreward'),
71
        "complete_signup_url": CartRegistrationView().get_success_url(request.user),
72
        "haspaid": haspaid,
73 74 75
    }

    return render(request, 'limitmonitor/userlimit.html', render_data)
76 77 78 79


@login_required
def ovpn_userfile(request):
80 81
    user_identity = request.user.get_identity()
    filepath = settings.OVPN_FILEPATH.replace("{USER_IDENTITY}", user_identity)
82

83 84 85
    response = FileResponse(open(filepath, 'rb'), content_type='application/octet-stream')
    response['Content-Disposition'] = 'attachment; filename="purist.ovpn"'
    return response
86

87

88 89 90 91 92 93 94 95 96 97
@login_required
def toggle_tunnel(request):
    tunnel_limit = request.user.limit_set.filter(
        service=settings.LM_SERVICES.TUNNEL).first()
    if tunnel_limit.service_activable_by_user():
        if not request.user.has_tunnel_account():
            request.user.generate_tunnel_account()
        tunnel_limit.is_active = not tunnel_limit.is_active
        tunnel_limit.save()
    return HttpResponseRedirect(reverse('profile'))
98 99 100 101 102 103 104 105 106 107


@login_required
def new_invitation(request):
    if request.user.account_type == AccountType.GROUP:
        invs_count = len(request.user.invitation_set.all())
        if invs_count < 4:
            new_inv = Invitation.create(request.user)
            new_inv.save()
    return HttpResponseRedirect(reverse('profile'))