Beispiel #1
0
    def save(self):
        if not self.is_valid():
            raise Exception('The form must be valid in order to save')

        username = self.cleaned_data['username']
        user = User.objects.get(username=username)
        if self.cleaned_data['created_ts']:
            created_ts = self.cleaned_data['created_ts']
        else:
            created_ts = localtime(now())
        if self.cleaned_data['s_id']:
            s_id = self.cleaned_data['id']

        created_by_user = self.cleaned_data['created_by']
        created_by = User.objects.get(username=created_by_user)
        resource = self.cleaned_data['resource']
        allowance = self.cleaned_data['allowance']
        start_date = self.cleaned_data['start_date']
        end_date = self.cleaned_data['end_date']
        monthly_rate = self.cleaned_data['monthly_rate']
        overage_rate = self.cleaned_data['overage_rate']
        if self.cleaned_data['paid_by']:
            paid_by = self.cleaned_data['paid_by']
        else:
            paid_by = None

        if s_id:
            sub = ResourceSubscription.objects.get(id=s_id)
            sub.end_date = end_date
        else:
            sub = ResourceSubscription(created_ts=created_ts, created_by=created_by, resource=resource, allowance=allowance, start_date=start_date, end_date=end_date, monthly_rate=monthly_rate, overage_rate=overage_rate, paid_by=paid_by, membership=user.membership)

        sub.save()

        return sub
Beispiel #2
0
    def save(self):
        if not self.is_valid():
            raise Exception('The form must be valid in order to save')

        username = self.cleaned_data['username']
        user = User.objects.get(username=username)
        if self.cleaned_data['created_ts']:
            created_ts = self.cleaned_data['created_ts']
        else:
            created_ts = localtime(now())
        if self.cleaned_data['s_id']:
            s_id = self.cleaned_data['id']

        created_by_user = self.cleaned_data['created_by']
        created_by = User.objects.get(username=created_by_user)
        resource = self.cleaned_data['resource']
        allowance = self.cleaned_data['allowance']
        start_date = self.cleaned_data['start_date']
        end_date = self.cleaned_data['end_date']
        monthly_rate = self.cleaned_data['monthly_rate']
        overage_rate = self.cleaned_data['overage_rate']
        if self.cleaned_data['paid_by']:
            paid_by = self.cleaned_data['paid_by']
        else:
            paid_by = None

        if s_id:
            sub = ResourceSubscription.objects.get(id=s_id)
            sub.end_date = end_date
        else:
            sub = ResourceSubscription(created_ts=created_ts, created_by=created_by, resource=resource, allowance=allowance, start_date=start_date, end_date=end_date, monthly_rate=monthly_rate, overage_rate=overage_rate, paid_by=paid_by, membership=user.membership)

        sub.save()

        return sub
def a_subscription(membership,
                   resource=None,
                   start_date=today,
                   monthly_rate=100,
                   overage_rate=20):
    if resource is None:
        resource = Resource.objects.day_resource
    return ResourceSubscription(membership=membership,
                                resource=resource,
                                start_date=start_date,
                                monthly_rate=monthly_rate,
                                overage_rate=overage_rate)
Beispiel #4
0
def confirm_membership(request, username, package, end_target, start_target, new_subs):
    user = get_object_or_404(User, username=username)
    membership = Membership.objects.for_user(user)
    subs = ast.literal_eval(new_subs)
    matches_package = user.membership.matching_package(subscriptions=subs)
    pkg = ast.literal_eval(package)
    match = None
    old_pkg = None
    if start_target != 'None':
        activity_target = datetime.strptime(start_target, '%Y-%m-%d')

    if user.membership.package_name():
        old_pkg = MembershipPackage.objects.get(name=user.membership.package_name())

    if len(membership.active_subscriptions(datetime.strptime(end_target, '%Y-%m-%d'))):
        ending_pkg = True
    else:
        ending_pkg = False

    if pkg:
        pkg_name = MembershipPackage.objects.get(id=pkg['package']).name
        if matches_package and matches_package.name != pkg_name:
            match = matches_package
    else:
        pkg_name = None

    if request.method == 'POST':
        try:
            with transaction.atomic():
                # Check to see if new membership package. If so, end all previous subscriptions
                membership = Membership.objects.for_user(user)
                if pkg:
                    # If there is a package, then make changes, else, we are ending all
                    if request.POST.get('match'):
                        pkg_name = MembershipPackage.objects.get(id=request.POST.get('match')).name
                    if len(membership.active_subscriptions()) == 0:
                        if settings.DEFAULT_BILLING_DAY == 0:
                            if membership.subscriptions_for_day(activity_target).count() == 0:
                                membership.change_bill_day(activity_target)
                        else:
                            membership.bill_day = settings.DEFAULT_BILLING_DAY
                            membership.save()

                    if user.membership.package_name() != pkg_name:
                        for s in user.membership.active_subscriptions():
                            if s.end_date == None:
                                s.end_date = end_target
                                s.save()
                        """When a membership is created, add the user to any opt-out mailing lists"""

                    # Review all subscriptions to see if adding or ending
                    for sub in subs:
                        sub_id = sub['s_id']
                        if sub_id != None:
                            if sub_id and sub['end_date']:
                                to_end = ResourceSubscription.objects.get(id=sub_id)
                                if to_end.end_date == None:
                                    end_date = sub['end_date']
                                    to_end.end_date = sub['end_date']
                                    to_end.save()
                                    slack = None
                        else:
                            """ If not ending then create subscriptions """
                            paid_by = None
                            created_ts = localtime(now())
                            created_by = request.user
                            resource = Resource.objects.get(id=sub['resource'])
                            allowance = sub['allowance']
                            start_date = sub['start_date']
                            starting_bill_day = start_date
                            package_name = pkg_name
                            if sub['end_date']:
                                end_date = sub['end_date']
                            else:
                                end_date = None
                            monthly_rate = sub['monthly_rate']
                            overage_rate = sub['overage_rate']
                            if sub['paid_by']:
                                p_username = sub['paid_by']
                                paid_by = User.objects.get(username=p_username)

                            """ Check to see if it is a unique resource and end if it is not """
                            already_have = membership.active_subscriptions().filter(resource=resource).filter(paid_by=paid_by)
                            if len(already_have) > 0:
                                p_id = already_have[0].id
                                prev_rs = ResourceSubscription.objects.get(id=p_id)
                                if not prev_rs.end_date:
                                    allowance = int(allowance) + prev_rs.allowance
                                    monthly_rate = int(monthly_rate) + prev_rs.monthly_rate
                                    prev_rs.end_date = datetime.strptime(start_date, '%Y-%m-%d') - timedelta(days=1)
                                    prev_rs.save()
                            # Save new resource
                            rs = ResourceSubscription(created_by=created_by, created_ts=created_ts, package_name=package_name, resource=resource, allowance=allowance, start_date=start_date, end_date=end_date, monthly_rate=monthly_rate, overage_rate=overage_rate, paid_by=paid_by, membership=membership)
                            rs.save()
                else:
                    arr = []
                    for a in user.membership.active_subscriptions():
                        arr.append(a.end_date)
                    if arr.count(arr[0]) == len(arr):
                        user.membership.end_all(end_target)
                    else:
                        for a in user.membership.active_subscriptions():
                            if a.end_date == None:
                                a.end_date = end_target
                                a.save()
                messages.success(request, "You have updated the subscriptions for %s" % username)
                return HttpResponseRedirect(reverse('staff:members:detail', kwargs={'username': username}) + '#tabs-1')
        except IntegrityError as e:
            print(('There was an ERROR: %s' % e.message))
            messages.error(request, 'There was an error setting new membership package')
    context = {
        'user': user,
        'package': pkg,
        'package_name': pkg_name,
        'old_pkg': old_pkg,
        'new_subs': subs,
        'end_target': end_target,
        'match': match,
        'ending_pkg': ending_pkg,
    }
    return render(request, 'staff/members/confirm.html', context)