diff --git a/.dockerignore b/.dockerignore index 6b8710a7..a715c9d7 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1 +1,2 @@ .git +.env diff --git a/.gitignore b/.gitignore index 2d923e99..a41d813d 100644 --- a/.gitignore +++ b/.gitignore @@ -44,3 +44,6 @@ secret-key !.gitkeep *.orig .vscode/settings.json +2024-* +2025-* +2023-* diff --git a/Dockerfile b/Dockerfile index 50b81cbb..4c1a9a66 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,5 @@ -FROM python:3.10.0-alpine3.15 +# FROM python:3.10.0-alpine3.15 +FROM python:3.5-alpine3.12 WORKDIR /usr/src/app @@ -7,12 +8,25 @@ RUN apk add --update --no-cache \ build-base \ openldap-dev \ python3-dev \ - libpq-dev \ + postgresql-dev \ + jpeg-dev \ + libxml2-dev \ + libxslt-dev \ + libmemcached-dev \ + zlib-dev \ && rm -rf /var/cache/apk/* +## For alpine 3.15 replace postgresql-dev with libpq-dev + # FIX https://github.com/python-ldap/python-ldap/issues/432 RUN echo 'INPUT ( libldap.so )' > /usr/lib/libldap_r.so COPY requirements.txt ./ -RUN pip install --no-cache-dir -r requirements.txt + +# Pillow seems to need LIBRARY_PATH set as follows: (see: https://github.com/python-pillow/Pillow/issues/1763#issuecomment-222383534) +RUN LIBRARY_PATH=/lib:/usr/lib /bin/sh -c "pip install --no-cache-dir -r requirements.txt" + COPY ./ . +COPY entrypoint.sh / + +ENTRYPOINT ["/entrypoint.sh" ] diff --git a/build-image.sh b/build-image.sh new file mode 100755 index 00000000..64a67fd6 --- /dev/null +++ b/build-image.sh @@ -0,0 +1,23 @@ +#!/bin/sh + +if [ $# -lt 1 ]; then + echo "$0 imageversion [push]" + echo "Version could be: $(git describe --always)" + echo "If push is specified, also push to our harbor" + exit 1 +fi + +tagprefix=harbor.k8s.ungleich.ch/ungleich-public/dynamicweb +version=$1; shift + +tag=${tagprefix}:${version} + +set -ex + +docker build -t "${tag}" . + +push=$1; shift + +if [ "$push" ]; then + docker push "${tag}" +fi diff --git a/datacenterlight/management/commands/check_vm_templates.py b/datacenterlight/management/commands/check_vm_templates.py index db36fde8..d30e5b0d 100644 --- a/datacenterlight/management/commands/check_vm_templates.py +++ b/datacenterlight/management/commands/check_vm_templates.py @@ -1,8 +1,9 @@ from django.core.management.base import BaseCommand from opennebula_api.models import OpenNebulaManager from datacenterlight.models import VMTemplate +from datacenterlight.cms_models import DCLCalculatorPluginModel from membership.models import CustomUser - +from utils.tasks import send_plain_email_task from django.conf import settings from time import sleep import datetime @@ -21,6 +22,7 @@ class Command(BaseCommand): def handle(self, *args, **options): result_dict = {} + error_dict = {} user_email = options['user_email'] if 'user_email' in options else "" if user_email: @@ -31,8 +33,12 @@ class Command(BaseCommand): PROJECT_PATH = os.path.abspath(os.path.dirname(__name__)) if not os.path.exists("%s/outputs" % PROJECT_PATH): os.mkdir("%s/outputs" % PROJECT_PATH) - for vm_template in VMTemplate.objects.all(): - vm_name = 'test-%s' % vm_template.name + public_templates_plugin = DCLCalculatorPluginModel.objects.filter(cmsplugin_ptr_id=23356).first() + ipv6_templates_plugin = DCLCalculatorPluginModel.objects.filter(cmsplugin_ptr_id=21943).first() + templates = public_templates_plugin.vm_templates_to_show + ipv6_templates_plugin.vm_templates_to_show + vm_templates = VMTemplate.objects.filter(opennebula_vm_template_id__in=templates) + for vm_template in vm_templates: + vm_name = 'test-%s-%s' % (vm_template.vm_type, vm_template.name) vm_id = manager.create_vm( template_id=vm_template.opennebula_vm_template_id, specs=specs, @@ -52,6 +58,7 @@ class Command(BaseCommand): %s %s''' % (vm_name, vm_template.opennebula_vm_template_id, vm_template.vm_type) + error_dict[vm_name] = result_dict[vm_name] self.stdout.write(self.style.ERROR(result_dict[vm_name])) sleep(1) date_str = datetime.datetime.strftime( @@ -62,4 +69,12 @@ class Command(BaseCommand): 'w', encoding='utf-8') as f: f.write(json.dumps(result_dict)) + if error_dict: + email_data = { + 'subject': 'Check VM Templates ERROR', + 'from_email': settings.DCL_SUPPORT_FROM_ADDRESS, + 'to': [settings.ADMIN_EMAIL], + 'body': json.dumps(error_dict), + } + send_plain_email_task.delay(email_data) self.stdout.write(self.style.SUCCESS("Done")) diff --git a/datacenterlight/management/commands/fix_generic_stripe_plan_product_names.py b/datacenterlight/management/commands/fix_generic_stripe_plan_product_names.py new file mode 100644 index 00000000..2773009d --- /dev/null +++ b/datacenterlight/management/commands/fix_generic_stripe_plan_product_names.py @@ -0,0 +1,54 @@ +from django.core.management.base import BaseCommand +from datacenterlight.tasks import handle_metadata_and_emails +from datacenterlight.models import StripePlan +from opennebula_api.models import OpenNebulaManager +from membership.models import CustomUser +from hosting.models import GenericProduct +import logging +import json +import sys +import stripe + +logger = logging.getLogger(__name__) + + +class Command(BaseCommand): + help = '''Stripe plans created before version 3.4 saved the plan name like generic-{subscription_id}-amount. This + command aims at replacing this with the actual product name + ''' + + def handle(self, *args, **options): + cnt = 0 + self.stdout.write( + self.style.SUCCESS( + 'In Fix generic stripe plan product names' + ) + ) + plans_to_change = StripePlan.objects.filter(stripe_plan_id__startswith='generic') + for plan in plans_to_change: + response = input("Press 'y' to continue: ") + + # Check if the user entered 'y' + if response.lower() == 'y': + plan_name = plan.stripe_plan_id + first_index_hyphen = plan_name.index("-") + 1 + product_id = plan_name[ + first_index_hyphen:(plan_name[first_index_hyphen:].index("-")) + first_index_hyphen] + gp = GenericProduct.objects.get(id=product_id) + if gp: + cnt += 1 + # update stripe + sp = stripe.Plan.retrieve(plan_name) + pr = stripe.Product.retrieve(sp.product) + pr.name = gp.product_name + pr.save() + # update local + spl = StripePlan.objects.get(stripe_plan_id=plan_name) + spl.stripe_plan_name = gp.product_name + spl.save() + print("%s. %s => %s" % (cnt, plan_name, gp.product_name)) + else: + print("Invalid input. Please try again.") + sys.exit() + + print("Done") \ No newline at end of file diff --git a/datacenterlight/templatetags/custom_tags.py b/datacenterlight/templatetags/custom_tags.py index 120cabbf..fc39eec7 100644 --- a/datacenterlight/templatetags/custom_tags.py +++ b/datacenterlight/templatetags/custom_tags.py @@ -73,6 +73,7 @@ def get_line_item_from_hosting_order_charge(hosting_order_id): :return: """ try: + print("Hositng order id = %s" % hosting_order_id) hosting_order = HostingOrder.objects.get(id = hosting_order_id) if hosting_order.stripe_charge_id: return mark_safe(""" @@ -144,7 +145,7 @@ def get_line_item_from_stripe_invoice(invoice): """.format( vm_id=vm_id if vm_id > 0 else "", ip_addresses=mark_safe(get_ip_addresses(vm_id)) if vm_id > 0 else - mark_safe(get_product_name(plan_name)), + mark_safe(get_product_name(plan_name)) if plan_name.startswith("generic-") else plan_name, period=mark_safe("%s — %s" % ( datetime.datetime.fromtimestamp(start_date).strftime('%Y-%m-%d'), datetime.datetime.fromtimestamp(end_date).strftime('%Y-%m-%d'))), @@ -160,8 +161,7 @@ def get_product_name(plan_name): product_name = "" if plan_name and plan_name.startswith("generic-"): first_index_hyphen = plan_name.index("-") + 1 - product_id = plan_name[first_index_hyphen: - (plan_name[first_index_hyphen:].index("-")) + first_index_hyphen] + product_id = plan_name[first_index_hyphen:(plan_name[first_index_hyphen:].index("-")) + first_index_hyphen] try: product = GenericProduct.objects.get(id=product_id) product_name = product.product_name diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 5c333a63..c708ff6a 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -42,6 +42,8 @@ from .utils import ( get_cms_integration, create_vm, clear_all_session_vars, validate_vat_number ) +from datacenterlight.templatetags.custom_tags import get_product_name + logger = logging.getLogger(__name__) @@ -199,6 +201,8 @@ class IndexView(CreateView): ssd_size=storage, pricing_name=vm_pricing_name ) + if request.user.id == 51: + print("User is test") specs = { 'cpu': cores, 'memory': memory, @@ -586,6 +590,9 @@ class OrderConfirmationView(DetailView, FormView): context = {} # this is amount to be charge/subscribed before VAT and discount # and expressed in chf. To convert to cents, multiply by 100 + print("******************************") + print("User = {}, ID = {}".format(request.user, request.user.id)) + print("******************************") amount_to_charge = 0 vm_specs = None if (('specs' not in request.session or 'user' not in request.session) @@ -611,7 +618,7 @@ class OrderConfirmationView(DetailView, FormView): # TODO check when we go through this case (to me, it seems useless) card_id = self.request.session.get('card_id') logger.debug("NO id_payment_method, using card: %s" % card_id) - card_detail = UserCardDetail.objects.get(id=card_id) + card_detail = UserCardDetail.get_ucd_from_card_id(card_id=card_id) context['cc_last4'] = card_detail.last4 context['cc_brand'] = card_detail.brand context['cc_exp_year'] = card_detail.exp_year @@ -645,6 +652,8 @@ class OrderConfirmationView(DetailView, FormView): pricing_name=vm_specs['pricing_name'], vat_rate=user_country_vat_rate * 100 ) + if request.user.id == 51: + print("User is test") vm_specs["price"] = price vm_specs["price_after_discount"] = price - discount["amount"] amount_to_charge = price @@ -850,7 +859,7 @@ class OrderConfirmationView(DetailView, FormView): return show_error(msg, self.request) elif 'card_id' in request.session: card_id = request.session.get('card_id') - user_card_detail = UserCardDetail.objects.get(id=card_id) + user_card_detail = UserCardDetail.get_ucd_from_card_id(card_id=card_id) card_details_dict = { 'last4': user_card_detail.last4, 'brand': user_card_detail.brand, @@ -900,15 +909,21 @@ class OrderConfirmationView(DetailView, FormView): 2 ) ) - plan_name = "generic-{0}-{1:.2f}".format( + stripe_plan_id = "generic-{0}-{1:.2f}".format( request.session['generic_payment_details']['product_id'], amount_to_be_charged ) - stripe_plan_id = plan_name + try: + product = GenericProduct.objects.get(id=request.session['generic_payment_details']['product_id']) + plan_name = product.product_name + except Exception as ex: + logger.debug("Errori {}" % str(ex)) + plan_name = get_product_name(stripe_plan_id) recurring_interval = request.session['generic_payment_details']['recurring_interval'] if recurring_interval == "year": - plan_name = "{}-yearly".format(plan_name) - stripe_plan_id = plan_name + stripe_plan_id = "{}-yearly".format(stripe_plan_id) + plan_name = "{} (yearly)".format(plan_name) + logger.debug("Plan name = {}, Stripe Plan id = {}".format(plan_name, stripe_plan_id)) else: template = request.session.get('template') specs = request.session.get('specs') @@ -1001,6 +1016,8 @@ class OrderConfirmationView(DetailView, FormView): # due to some reason. So, we would want to dissociate this card # here. # ... + logger.debug("In 1 ***") + logger.debug("stripe_subscription_obj == %s" % stripe_subscription_obj) msg = subscription_result.get('error') return show_error(msg, self.request) elif stripe_subscription_obj.status == 'incomplete': @@ -1194,9 +1211,10 @@ def get_or_create_custom_user(request, stripe_api_cus_id): def set_user_card(card_id, stripe_api_cus_id, custom_user, card_details_response): + logger.debug(":: set_user_card") if card_id: logger.debug("card_id %s was in request" % card_id) - user_card_detail = UserCardDetail.objects.get(id=card_id) + user_card_detail = UserCardDetail.get_ucd_from_card_id(card_id=card_id) card_details_dict = { 'last4': user_card_detail.last4, 'brand': user_card_detail.brand, @@ -1208,21 +1226,24 @@ def set_user_card(card_id, stripe_api_cus_id, custom_user, stripe_source_id=user_card_detail.card_id ) else: - logger.debug("card_id was NOT in request, using " + logger.debug(" card_id was NOT in request, using " "card_details_response") ucd = UserCardDetail.get_or_create_user_card_detail( stripe_customer=custom_user.stripecustomer, card_details=card_details_response ) + logger.debug(" ucd = %s" % ucd) UserCardDetail.save_default_card_local( custom_user.stripecustomer.stripe_id, ucd.card_id ) + logger.debug(" after save_default_card_local") card_details_dict = { 'last4': ucd.last4, 'brand': ucd.brand, 'card_id': ucd.card_id } + logger.debug("card_detail_dict = %s" % card_details_dict) return card_details_dict @@ -1416,6 +1437,30 @@ def do_provisioning(request, stripe_api_cus_id, card_details_response, """ logger.debug("do_provisioning") + + try: + params = { + 'request': request, + 'stripe_api_cus_id': stripe_api_cus_id, + 'card_details_response': card_details_response, + 'stripe_subscription_obj': stripe_subscription_obj, + 'stripe_onetime_charge': stripe_onetime_charge, + 'gp_details': gp_details, + 'specs': specs, + 'vm_template_id': vm_template_id, + 'template': template, + 'billing_address_data': billing_address_data, + 'real_request': real_request + } + print("Input Parameters:") + for key, value in params.items(): + try: + print("{}: {}".format(key, value)) + except Exception as e: + print("{}: [Error printing value: {}]".format(key, str(e))) + except Exception as e: + print("Error printing parameters: {}".format(str(e))) + user = request.get('user', None) # Create user if the user is not logged in and if he is not already @@ -1427,6 +1472,7 @@ def do_provisioning(request, stripe_api_cus_id, card_details_response, card_details_dict = set_user_card(card_id, stripe_api_cus_id, custom_user, card_details_response) + logger.debug("after set_user_card %s" % card_details_dict) # Save billing address billing_address_data.update({ @@ -1449,6 +1495,7 @@ def do_provisioning(request, stripe_api_cus_id, card_details_response, vat_number=billing_address_data['vat_number'] ) billing_address.save() + logger.debug("billing_address saved") order = HostingOrder.create( price=request['generic_payment_details']['amount'], @@ -1456,6 +1503,7 @@ def do_provisioning(request, stripe_api_cus_id, card_details_response, billing_address=billing_address, vm_pricing=VMPricing.get_default_pricing() ) + logger.debug("hosting order created") # Create a Hosting Bill HostingBill.create(customer=stripe_cus, @@ -1512,7 +1560,9 @@ def do_provisioning(request, stripe_api_cus_id, card_details_response, ["%s=%s" % (k, v) for (k, v) in context.items()]), 'reply_to': [context['email']], } + logger.debug("Sending email") send_plain_email_task.delay(email_data) + logger.debug("After Sending email") recurring_text = _(" This is a monthly recurring plan.") if gp_details['recurring_interval'] == "year": recurring_text = _(" This is an yearly recurring plan.") @@ -1536,6 +1586,7 @@ def do_provisioning(request, stripe_api_cus_id, card_details_response, ), 'reply_to': ['info@ungleich.ch'], } + logger.debug("Before Sending customer email") send_plain_email_task.delay(email_data) redirect_url = reverse('datacenterlight:index') logger.debug("Sent user/admin emails") @@ -1579,7 +1630,7 @@ def do_provisioning(request, stripe_api_cus_id, card_details_response, def get_error_response_dict(msg, request): - logger.error(msg) + logger.debug("Init get_error_response_dict {}".format(msg)) response = { 'status': False, 'redirect': "{url}#{section}".format( diff --git a/dynamicweb/settings/base.py b/dynamicweb/settings/base.py index f03042dc..b7705e17 100644 --- a/dynamicweb/settings/base.py +++ b/dynamicweb/settings/base.py @@ -777,3 +777,9 @@ if DEBUG: from .local import * # flake8: noqa else: from .prod import * # flake8: noqa + +# Try to load dynamic configuration, if it exists +try: + from .dynamic import * # flake8: noqa +except ImportError: + pass diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 00000000..e207a457 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +set -uex + +cd /usr/src/app/ +cat > dynamicweb/settings/dynamic.py < 0 and subscription.default_tax_rates[0].jurisdiction and subscription.default_tax_rates[0].jurisdiction.lower() == 'fi': + fi_subs.append(subscription) + elif len(subscription.default_tax_rates) > 0: + print("subscription %s belongs to %s" % (subscription.id, subscription.default_tax_rates[0].jurisdiction)) + else: + print("subscription %s does not have a tax rate" % subscription.id) + if subscriptions.has_more: + print("FETCHING MORE") + subscriptions = stripe.Subscription.list(limit=100, starting_after=subscriptions.data[-1]) + else: + break + logger.debug("There are %s FI subscription that need VAT rate update" % len(fi_subs)) + + # CSV column headers + csv_headers = [ + "customer_name", + "customer_email", + "stripe_customer_id", + "subscription_id", + "subscription_name", + "amount", + "vat_rate" + ] + # CSV file name + csv_filename = "fi_subscriptions_change_2024.csv" + # Write subscription data to CSV file + with open(csv_filename, mode='w', newline='') as csv_file: + writer = csv.DictWriter(csv_file, fieldnames=csv_headers) + writer.writeheader() + + for subscription in fi_subs: + subscription_id = subscription["id"] + stripe_customer_id = subscription.get("customer", "") + vat_rate = subscription.get("tax_percent", "") + c_user = CustomUser.objects.get( + id=StripeCustomer.objects.filter(stripe_id=stripe_customer_id)[0].user.id) + if c_user: + customer_name = c_user.name.encode('utf-8') + customer_email = c_user.email + items = subscription.get("items", {}).get("data", []) + for item in items: + subscription_name = item.get("plan", {}).get("id", "") + amount = item.get("plan", {}).get("amount", "") + + # Convert amount to a proper format (e.g., cents to dollars) + amount_in_chf = amount / 100 # Adjust this conversion as needed + + # Writing to CSV + writer.writerow({ + "customer_name": customer_name, + "customer_email": customer_email, + "stripe_customer_id": stripe_customer_id, + "subscription_id": subscription_id, + "subscription_name": subscription_name, + "amount": amount_in_chf, + "vat_rate": vat_rate # Fill in VAT rate if available + }) + else: + print("No customuser for %s %s" % (stripe_customer_id, subscription_id)) + + + if MAKE_MODIFS: + print("Making modifications now") + tax_rate_obj = stripe.TaxRate.create( + display_name="VAT", + description="VAT for %s" % country_to_change, + jurisdiction=country_to_change, + percentage=new_rate, + inclusive=False, + ) + stripe_tax_rate = StripeTaxRate.objects.create( + display_name=tax_rate_obj.display_name, + description=tax_rate_obj.description, + jurisdiction=tax_rate_obj.jurisdiction, + percentage=tax_rate_obj.percentage, + inclusive=False, + tax_rate_id=tax_rate_obj.id + ) + + for fi_sub in fi_subs: + fi_sub.default_tax_rates = [stripe_tax_rate.tax_rate_id] + fi_sub.save() + logger.debug("Default tax rate updated for %s" % fi_sub.id) + else: + print("Not making any modifications because MAKE_MODIFS=False") + + except Exception as e: + print(" *** Error occurred. Details {}".format(str(e))) diff --git a/hosting/models.py b/hosting/models.py index b51d8616..12edba65 100644 --- a/hosting/models.py +++ b/hosting/models.py @@ -699,15 +699,116 @@ class UserCardDetail(AssignPermissionsMixin, models.Model): @staticmethod def save_default_card_local(stripe_api_cus_id, card_id): + print("save_default_card_local {}, {}".format(stripe_api_cus_id, card_id)) stripe_cust = StripeCustomer.objects.get(stripe_id=stripe_api_cus_id) - user_card_detail = UserCardDetail.objects.get( - stripe_customer=stripe_cust, card_id=card_id + print(" stripe_cust={}".format(stripe_cust)) + user_card_detail = UserCardDetail.get_ucd_from_stripe_cust_n_card_id( + stripe_cust, card_id ) + print(" user_card_detail={}".format(user_card_detail.__dict__)) for card in stripe_cust.usercarddetail_set.all(): card.preferred = False card.save() user_card_detail.preferred = True user_card_detail.save() + print(" save_default_card_local DONE") + + @staticmethod + def get_ucd_from_card_id(card_id): + try: + user_card_details = UserCardDetail.objects.filter( + card_id=card_id + ).order_by('-id') + + if user_card_details.count() > 1: + # Log a warning about the duplicate entries + logger.warning( + "Multiple UserCardDetail objects found for card_id={}. " + "Found {} objects. Using the latest one.".format( + card_id, user_card_details.count() + ) + ) + # Use the first object found + user_card_detail = user_card_details.first() + elif user_card_details.count() == 1: + # Exactly one object found, proceed as intended + user_card_detail = user_card_details.first() + else: + # No object found for the given customer and card_id. + # Depending on expected behavior, you might want to raise an error or handle this case. + # If the original get() call happened here, it would raise DoesNotExist. + logger.error( + "No UserCardDetail found for card_id={}.".format(card_id) + ) + raise UserCardDetail.DoesNotExist("No UserCardDetail found for card {}".format(card_id)) + if user_card_details.count() > 1: + # Log a warning about the duplicate entries + logger.warning( + "Multiple UserCardDetail objects found for card_id={}. " + "Found {} objects. Using the first one.".format( + card_id, user_card_details.count() + ) + ) + # Use the first object found + user_card_detail = user_card_details.first() + elif user_card_details.count() == 1: + # Exactly one object found, proceed as intended + user_card_detail = user_card_details.first() + else: + # No object found for the given customer and card_id. + # Depending on expected behavior, you might want to raise an error or handle this case. + # If the original get() call happened here, it would raise DoesNotExist. + logger.error( + "No UserCardDetail found for card_id={}.".format(card_id) + ) + raise UserCardDetail.DoesNotExist("No UserCardDetail found for card {}".format(card_id)) + except Exception as e: + # Catch other potential exceptions during the filter/get process if necessary + logger.error("An unexpected error occurred while fetching UserCardDetail: {}".format(e)) + raise + return user_card_detail + + @staticmethod + def get_ucd_from_stripe_cust_n_card_id(stripe_cust, card_id): + try: + user_card_details = UserCardDetail.objects.filter( + stripe_customer=stripe_cust, card_id=card_id + ).order_by('-id') + + if user_card_details.count() > 1: + # Log a warning about the duplicate entries + logger.warning( + "Multiple UserCardDetail objects found for stripe_customer_id={} and card_id={}. " + "Found {} objects. Using the first one.".format( + stripe_cust.id, card_id, user_card_details.count() + ) + ) + # Use the first object found + user_card_detail = user_card_details.first() + elif user_card_details.count() == 1: + # Exactly one object found, proceed as intended + user_card_detail = user_card_details.first() + else: + # No object found for the given customer and card_id. + # Depending on expected behavior, you might want to raise an error or handle this case. + # If the original get() call happened here, it would raise DoesNotExist. + logger.error( + "No UserCardDetail found for stripe_customer_id={} and card_id={}.".format( + stripe_cust.id, card_id + ) + ) + raise UserCardDetail.DoesNotExist( + "No UserCardDetail found for customer {}, card {}".format( + stripe_cust.id, card_id + ) + ) + + except Exception as e: + # Catch other potential exceptions during the filter/get process if necessary + logger.error("An unexpected error occurred while fetching UserCardDetail: {}".format(e)) + raise + return user_card_detail + @staticmethod def get_user_card_details(stripe_customer, card_details): diff --git a/hosting/templates/hosting/invoices.html b/hosting/templates/hosting/invoices.html index 69ed4577..347b1ff4 100644 --- a/hosting/templates/hosting/invoices.html +++ b/hosting/templates/hosting/invoices.html @@ -87,7 +87,7 @@ {% for ho, stripe_charge_data in invs_charge %} - {{ ho | get_line_item_from_hosting_order_charge }} + {{ ho.id | get_line_item_from_hosting_order_charge }} {% endfor %} diff --git a/hosting/views.py b/hosting/views.py index 3ac8c023..c2470707 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -810,7 +810,7 @@ class PaymentVMView(LoginRequiredMixin, FormView): card_id = form.cleaned_data.get('card') customer = owner.stripecustomer try: - user_card_detail = UserCardDetail.objects.get(id=card_id) + user_card_detail = UserCardDetail.get_ucd_from_card_id(card_id=card_id) if not request.user.has_perm( 'view_usercarddetail', user_card_detail ): @@ -1014,7 +1014,7 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView, FormView): context['cc_exp_month'] = card_details_response['exp_month'] else: card_id = self.request.session.get('card_id') - card_detail = UserCardDetail.objects.get(id=card_id) + card_detail = UserCardDetail.get_ucd_from_card_id(card_id=card_id) context['cc_last4'] = card_detail.last4 context['cc_brand'] = card_detail.brand context['cc_exp_year'] = card_detail.exp_year @@ -1128,7 +1128,7 @@ class OrdersHostingDetailView(LoginRequiredMixin, DetailView, FormView): return JsonResponse(response) else: card_id = request.session.get('card_id') - user_card_detail = UserCardDetail.objects.get(id=card_id) + user_card_detail = UserCardDetail.get_ucd_from_card_id(card_id=card_id) card_details_dict = { 'last4': user_card_detail.last4, 'brand': user_card_detail.brand, @@ -1339,7 +1339,7 @@ class InvoiceListView(LoginRequiredMixin, TemplateView): ).order_by('-created_at') stripe_chgs = [] for ho in hosting_orders: - stripe_chgs.append({ho.id: stripe.Charge.retrieve(ho.stripe_charge_id)}) + stripe_chgs.append({ho: stripe.Charge.retrieve(ho.stripe_charge_id)}) paginator_charges = Paginator(stripe_chgs, 10) try: @@ -1544,7 +1544,12 @@ class VirtualMachinesPlanListView(LoginRequiredMixin, ListView): ordering = '-id' def get_queryset(self): - owner = self.request.user + username = self.request.GET.get('username') + if self.request.user.is_admin and username: + user = CustomUser.objects.get(username=username) + else: + user = self.request.user + owner = user manager = OpenNebulaManager(email=owner.username, password=owner.password) try: diff --git a/requirements.txt b/requirements.txt index 8d04a189..73cdf987 100644 --- a/requirements.txt +++ b/requirements.txt @@ -25,7 +25,7 @@ django-compressor==2.0 django-debug-toolbar==1.4 python-dotenv==0.10.3 django-extensions==1.6.7 -django-filer==2.1.2 +django-filer==1.2.0 django-filter==0.13.0 django-formtools==1.0 django-guardian==1.4.4