Commit 9425d009 authored by David Seaward's avatar David Seaward
Browse files

Re-apply missing changes after merge with production.


Signed-off-by: David Seaward's avatarDavid Seaward <david.seaward@puri.sm>
parent 1f89ffe2
Pipeline #55108 failed with stages
in 2 minutes and 8 seconds
import json
import logging
from django.conf import settings
from django.contrib.auth import logout
from django.urls import reverse
from django.utils.decorators import method_decorator
from django.views.decorators.debug import sensitive_post_parameters
from registration.backends.simple.views import RegistrationView
from cart.models import ChosenReward
from purist.models import AccountType
from .forms import CartRegistrationFormWithCaptcha, CartRegistrationForm
from limitmonitor.common import forced_update
from limitmonitor.subscription import SubscriptionManager, SubscriptionStatus
import json
import logging
from purist.models import AccountType
from .forms import CartRegistrationFormWithCaptcha, CartRegistrationForm
log = logging.getLogger(__name__)
logger = logging.getLogger(__name__)
class CartRegistrationView(RegistrationView):
......@@ -80,6 +82,7 @@ class CartRegistrationView(RegistrationView):
if self.bundle == 'Basic':
user.account_type = AccountType.BASIC
user.quota = settings.STORAGE_QUOTA_BASIC
try:
new_sub = SubscriptionManager.create_subscription(
......@@ -95,7 +98,7 @@ class CartRegistrationView(RegistrationView):
except Exception as e:
message = "Failed to create subscription for basic user {}.".format(user)
logging.exception(message)
logger.exception(message, e)
elif self.bundle == 'Complete':
user.account_type = AccountType.COMPLETE
......
from enum import Enum
import strictyaml
from django.conf import settings
from django.utils import timezone
from purist.models import AccountType, User
from middleware.common import get_woo_connection
from invitation.models import Invitation
import strictyaml
from middleware.common import get_woo_connection
from purist.models import AccountType, User
def get_available_bundles():
with open(settings.WOO_BUNDLES_INFO_FILE, 'r') as stream:
bundles = strictyaml.load(stream.read()).data
return bundles
......@@ -78,8 +79,8 @@ class SubscriptionManager:
variation_id = None
for k, v in bundles.items():
if (
v['type'] == account_type.name and
v['period'] == billing_period.value
v['type'] == account_type.name and
v['period'] == billing_period.value
):
variation_id = k
break
......@@ -120,7 +121,8 @@ class SubscriptionManager:
order = user.woo_post_json('orders', order_data)
if order is None:
message = "Cannot create subscription for user {} because there was an error creating the associated order.".format(user)
message = "Cannot create subscription for user {} because there was an error creating the associated order.".format(
user)
raise Exception(message)
# Set billing period for subscription.
......@@ -136,20 +138,20 @@ class SubscriptionManager:
"quantity": 1,
}
],
"next_payment_date": next_payment_date.strftime(
'%Y-%m-%d %H:%M:%S'),
"billing_period": bill_p
"next_payment_date": next_payment_date.strftime('%Y-%m-%d %H:%M:%S'),
"billing_period": bill_p,
'parent_id': order['id'],
'customer_id': order['customer_id'],
'status': status.value
}
subs_data['parent_id'] = order['id']
subs_data['customer_id'] = order['customer_id']
subs_data['status'] = status.value
if billing_email:
subs_data['billing'] = order_data['billing']
sub = user.woo_post_json('subscriptions', subs_data)
if sub is None:
message = "Cannot create subscription for user {} because there was an error posting the subscription command.".format(user)
message = "Cannot create subscription for user {} because there was an error posting the subscription command.".format(
user)
raise Exception(message)
new_sub = Subscription.parse_subscription(sub, user)
......@@ -172,7 +174,7 @@ class SubscriptionManager:
)
@classmethod
def search_subscriptions(cls, user, status=None):
def search_subscriptions(cls, user, status=None, product_id=None):
result = []
query = 'subscriptions?customer={}'.format(user.get_woocommerce_id())
......@@ -224,13 +226,10 @@ class SubscriptionManager:
implemented.
"""
if cls.woo is None:
cls.woo = get_woo_connection()
query = 'orders?customer={}&status=pending&product={}'.format(
user.wcid, product_id)
r = cls.woo.get(query)
r = cls.woo_get(query)
if r.status_code == 200:
orders = r.json()
if len(orders) > 0:
......
import csv
import logging
from decimal import Decimal
from celery.utils.log import get_task_logger
from django.conf import settings
from django.utils import timezone
......@@ -10,17 +11,12 @@ from limitmonitor.common import active_subscription
from limitmonitor.models import LimitMeasure
from purist.models import User
logger = logging.getLogger(__name__)
logger = get_task_logger(__name__)
def purge_users_pending_cart():
pending_carts = ChosenReward.objects.filter(is_pending=True)
now = timezone.now()
total_pending = len(pending_carts)
count_pending = 0
for pending_cart in pending_carts:
try:
......@@ -34,35 +30,31 @@ def purge_users_pending_cart():
paid = sub is not None
if time_pending.total_seconds() > 60 * 60 * 24 and not paid:
count_pending += 1
# Delete LDAP entry
try:
ldap_user = LdapPerson.objects.filter(uid=username)
ldap_user.delete()
except Exception as e:
logger.exception("Could not delete user {} from LDAP.".format(username), e)
logger.exception("Could not delete user {} from LDAP.".format(username))
# Delete WooCommerce account
try:
user.delete_woocommerce_account()
except Exception as e:
logger.exception("Could not delete user {} from WooCommerce.".format(username), e)
logger.exception("Could not delete user {} from WooCommerce.".format(username))
# Finally delete user
try:
user.delete()
except Exception as e:
logger.exception("Could not delete user {} from Django.".format(username), e)
logger.exception("Could not delete user {} from Django.".format(username))
except Exception as e:
logger.exception("Could not delete pending user {}.".format(username), e)
logger.info("Purged {count} of {total} pending users.".format(count=count_pending, total=total_pending))
logger.exception("Could not delete pending user {}.".format(username))
def import_service_storage_data(service):
now = timezone.now()
file_end = now.strftime('_%Y%m%d.csv')
filename = service.lower() + file_end
......@@ -115,7 +107,6 @@ def import_service_storage_data(service):
def import_services_storage_data():
for s in settings.MEASURED_SERVICES:
serv_name = settings.LM_SERVICES.get_name_by_code(s)
import_service_storage_data(serv_name)
......@@ -221,6 +221,10 @@ LM_PARSERS = purist.limitmonitor.ParserContainer
#
# LOGGING
#
if DEBUG:
log_level = 'DEBUG'
else:
log_level = 'ERROR'
LOGGING = {
'version': 1,
......@@ -235,7 +239,7 @@ LOGGING = {
'loggers': {
'': {
'handlers': ['console'],
'level': 'DEBUG',
'level': log_level,
'propagate': True,
},
},
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment