Esempio n. 1
0
def depot_change(request, subscription_id):
    '''
    change a depot
    '''
    subscription = get_object_or_404(Subscription, id=subscription_id)
    saved = False
    if request.method == 'POST':
        if subscription.state == 'waiting':
            subscription.depot = get_object_or_404(
                Depot, id=int(request.POST.get('depot')))
        else:
            subscription.future_depot = get_object_or_404(
                Depot, id=int(request.POST.get('depot')))
        subscription.save()
        saved = True
    renderdict = get_menu_dict(request)
    depots = DepotDao.all_depots()
    requires_map = False
    for depot in depots:
        requires_map = requires_map or depot.has_geo
    renderdict.update({
        'subscription': subscription,
        'saved': saved,
        'member': request.user.member,
        'depots': depots,
        'requires_map': requires_map,
    })
    return render(request, 'depot_change.html', renderdict)
def maps(request):
    renderdict = {
        'depots': DepotDao.all_depots(),
        'subscriptions': SubscriptionDao.all_active_subscritions(),
    }

    return render(request, 'maps.html', renderdict)
Esempio n. 3
0
def maps(request):
    renderdict = {
        "depots": DepotDao.all_depots(),
        "subscriptions": SubscriptionDao.all_active_subscritions(),
    }

    return render(request, "maps.html", renderdict)
Esempio n. 4
0
def cs_select_depot(request, cs_session):
    if request.method == 'POST':
        cs_session.depot = DepotDao.depot_by_id(request.POST.get('depot'))
        return redirect(cs_session.next_page())

    depots = DepotDao.all_depots()
    requires_map = any(depot.has_geo for depot in depots)
    render_dict = {
        'member': cs_session.main_member,
        'depots': depots,
        'selected': cs_session.depot,
        'requires_map': requires_map,
    }
    return render(request, 'createsubscription/select_depot.html', render_dict)
Esempio n. 5
0
def depot_change(request):
    """
    change a depot
    """
    saved = False
    if request.method == "POST":
        request.user.member.subscription.future_depot = get_object_or_404(
            Depot, id=int(request.POST.get("depot")))
        request.user.member.subscription.save()
        saved = True
    renderdict = get_menu_dict(request)
    renderdict.update({
        'saved': saved,
        'member': request.user.member,
        "depots": DepotDao.all_depots()
    })
    return render(request, "depot_change.html", renderdict)
Esempio n. 6
0
def cs_select_depot(request):
    if request.user.is_authenticated:
        member = request.user.member
    else:
        member = request.session.get('main_member')
    if member is None:
        return redirect('http://' + Config.server_url())
    if request.method == 'POST':
        depot = DepotDao.depot_by_id(request.POST.get('depot'))
        request.session['selecteddepot'] = depot
        return redirect('/my/create/subscription/start')
    depots = DepotDao.all_depots()
    requires_map = True
    for depot in depots:
        requires_map = requires_map or depot.has_geo
    renderdict = {
        'member': member,
        'depots': depots,
        'requires_map': requires_map,
    }
    return render(request, 'createsubscription/select_depot.html', renderdict)
Esempio n. 7
0
def add_member(request, subscription_id):
    shareerror = False
    shares = 1
    memberexists = False
    memberblocked = False
    if request.method == 'POST':
        memberform = MemberProfileForm(request.POST)
        try:
            shares = int(request.POST.get("shares"))
            shareerror = shares < 0
        except:
            shareerror = True
        member = next(
            iter(MemberDao.members_by_email(request.POST.get('email')) or []),
            None)
        if member is not None:
            memberexists = True
            shares = 0
            if member.subscription is not None:
                memberblocked = True

        if (memberform.is_valid()
                and shareerror is False) or (memberexists is True
                                             and memberblocked is False):
            tmp_shares = []
            pw = None
            if memberexists is False:
                for num in range(0, shares):
                    member = Member(**memberform.cleaned_data)
                    tmp_shares.append(Share(member=member, paid_date=None))
            else:
                for share in member.share_set.all():
                    tmp_shares.append(share)
            if request.GET.get("return"):
                member.subscription_id = subscription_id
                member.save()
                send_been_added_to_subscription(
                    member.email, pw, request.user.member.get_name(), shares,
                    hashlib.sha1(memberform.cleaned_data['email'] +
                                 str(subscription_id)).hexdigest())
                if memberexists is False:
                    for share in tmp_shares:
                        share.save()
                        send_share_created_mail(share)
                return redirect(request.GET.get("return"))
            else:
                co_members_shares = request.session.get(
                    'create_co_members_shares', [])
                co_members_shares += tmp_shares
                request.session['create_co_members_shares'] = co_members_shares
                co_members = request.session.get('create_co_members', [])
                co_members.append(member)
                request.session['create_co_members'] = co_members
                return redirect('/my/create/subscrition')
    else:
        if request.user.is_authenticated():
            member = request.user.member
        else:
            member = request.session.get('main_member')
        if member is None:
            return redirect("http://" + Config.server_url())
        initial = {
            "addr_street": member.addr_street,
            "addr_zipcode": member.addr_zipcode,
            "addr_location": member.addr_location,
            "phone": member.phone,
        }
        memberform = MemberProfileForm(initial=initial)
    renderdict = {
        'shares':
        shares,
        'memberexists':
        memberexists,
        'memberblocked':
        memberexists,
        'shareerror':
        shareerror,
        'memberform':
        memberform,
        "member":
        member,
        "depots":
        DepotDao.all_depots(),
        "cancelUrl":
        request.GET.get("return")
        if request.GET.get("return") else '/my/create/subscrition'
    }
    return render(request, "add_member.html", renderdict)
Esempio n. 8
0
def createsubscription(request):
    """
    create a subscription
    """
    if request.user.is_authenticated():
        member = request.user.member
    else:
        member = request.session.get('main_member')
    if member is None:
        return redirect("http://" + Config.server_url())

    shareerror = False
    subscriptionform = SubscriptionForm()
    session_subscription = request.session.get('create_subscription')
    co_members = request.session.get('create_co_members', [])
    co_members_shares = request.session.get('create_co_members_shares', [])
    member_shares = request.session.get('create_member_shares', [])

    selectedsubscription = "none"
    selected_depot = None
    existing_member_shares = 0
    if member.pk is not None:
        existing_member_shares = member.share_set.all().count()
    shares = existing_member_shares

    if session_subscription is not None:
        selectedsubscription = next(
            iter(
                SubscriptionSizeDao.sizes_by_size(
                    session_subscription.size).values_list('name', flat=True)
                or []), 'none')
        selected_depot = session_subscription.depot

    co_member_shares = len(co_members_shares)
    if request.method == "POST":
        shares += int(request.POST.get("shares"))
        selectedsubscription = request.POST.get("subscription")
        subscriptionform = SubscriptionForm(request.POST)

        shares += co_member_shares
        min_num_shares = next(
            iter(
                SubscriptionSizeDao.sizes_by_name(
                    selectedsubscription).values_list('shares', flat=True)
                or []), 1)
        if shares < min_num_shares or not subscriptionform.is_valid():
            shareerror = shares < min_num_shares
        else:
            size = next(
                iter(
                    SubscriptionSizeDao.sizes_by_name(
                        selectedsubscription).values_list('size', flat=True)
                    or []), 0)

            if size > 0:
                session_subscription = Subscription(
                    **subscriptionform.cleaned_data)
                session_subscription.depot = DepotDao.depot_by_id(
                    request.POST.get("depot"))
                session_subscription.primary_member = member
                session_subscription.size = size

            if len(member_shares) < int(request.POST.get("shares")):
                toadd = int(request.POST.get("shares")) - len(member_shares)
                for num in range(0, toadd):
                    member_shares.append(Share(member=member, paid_date=None))
            elif len(member_shares) > int(request.POST.get("shares")):
                toremove = len(member_shares) - int(request.POST.get("shares"))
                for num in range(0, toremove):
                    member_shares.pop()

            if request.POST.get("add_member"):
                request.session['create_subscription'] = session_subscription
                request.session['create_member_shares'] = member_shares
                return redirect("/my/cosubmember/0")
            else:
                password = None
                if member.pk is None:
                    member.save()
                    password = password_generator()
                    member.user.set_password(password)
                    member.user.save()
                if session_subscription is not None:
                    session_subscription.save()
                    member.subscription_id = session_subscription.id
                    member.save()
                send_welcome_mail(
                    member.email, password,
                    hashlib.sha1((member.email +
                                  str(member.id)).encode('utf8')).hexdigest())
                for co_member in co_members:
                    co_member.subscription_id = session_subscription.id
                    co_member.save()
                    pw = None
                    if co_member.confirmed is False:
                        pw = password_generator()
                        co_member.user.set_password(pw)
                        co_member.user.save()
                    send_been_added_to_subscription(
                        co_member.email, pw, member.get_name(), shares,
                        hashlib.sha1(
                            (co_member.email +
                             str(co_member.id)).encode('utf8')).hexdigest())
                for share in member_shares + co_members_shares:
                    if share.id is None:
                        if share.member.email == member.email:
                            share.member = member
                        else:
                            share.member = list(
                                (co_member for co_member in co_members
                                 if co_member.email == share.member.email))[0]
                        share.save()
                        send_share_created_mail(share)
                request.session['create_subscription'] = None
                request.session['create_co_members'] = []
                request.session['create_co_members_shares'] = []
                request.session['create_member_shares'] = []
                if request.user.is_authenticated():
                    return redirect("/my/home")
                else:
                    return redirect("/my/welcome")

    renderdict = {
        'co_member_shares': co_member_shares,
        'existing_member_shares': existing_member_shares,
        'member': member,
        'subscription_sizes': SubscriptionSizeDao.all_sizes_ordered(),
        'depots': DepotDao.all_depots(),
        'selected_depot': selected_depot,
        'selected_subscription': selectedsubscription,
        'shareerror': shareerror,
        'co_members': co_members,
        'subscriptionform': subscriptionform
    }
    return render(request, "createsubscription.html", renderdict)