Commit b9da85cc authored by Birin Sanchez's avatar Birin Sanchez
Browse files

Add CLI command user_report that prints a basic report for the


specified user.
Signed-off-by: Birin Sanchez's avatarBirin Sanchez <birin.sanchez@puri.sm>
parent b383c5db
Pipeline #54898 failed with stages
in 1 minute and 33 seconds
......@@ -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,
},
},
......
from django.core.management.base import BaseCommand
from django.core.validators import EmailValidator, ValidationError
from django.conf import settings
from limitmonitor.subscription import SubscriptionManager, get_available_bundles
from purist.models import User
class Command(BaseCommand):
help = """Prints a report for the user specified."""
def add_arguments(self, parser):
parser.add_argument('email', type=str,
help='The report will be generated about this \
user.')
# parser.add_argument('--full',
# action='store_true',
# help='Deletes the user from middleware DB, LDAP \
# and WooCommerce.')
def handle(self, *args, **options):
email = options['email']
# full = options['full']
ev = EmailValidator()
try:
ev(email)
except ValidationError as e:
self.stdout.write(repr(e))
return
(username, domain) = email.split('@')
if domain != settings.SITE_DOMAIN:
msg = '{} does not belong to {} domain'
self.stdout.write(msg.format(email, settings.SITE_DOMAIN))
return
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
msg = 'User {} does not exist.'.format(email)
self.stdout.write(msg)
return
self.stdout.write('\n' + user.get_identity())
identity_lenght = str(len(user.get_identity()))
self.stdout.write(('{:=^' + identity_lenght + '}').format(''), ending='\n\n')
if hasattr(user, 'chosenreward'):
cart_status = 'Yes' if user.chosenreward.is_pending else 'No'
else:
cart_status = 'No ChosenReward object'
if user.tunnel_user and user.tunnel_password:
tunnel_status = 'Yes'
else:
tunnel_status = 'No'
user_acitve = 'Yes' if user.is_active else 'No'
account_deleted = 'Yes('+str(user.purge)+')' if user.purge else 'No'
locked_status = 'Yes' if user.is_locked else 'No'
self.stdout.write('Middleware information')
self.stdout.write('----------------------')
self.stdout.write(' Date joined: {:%Y-%m-%d %H:%M:%S}'.format(user.date_joined))
self.stdout.write('Last login date: {:%Y-%m-%d %H:%M:%S}'.format(user.last_login))
self.stdout.write(' Pending cart: {}'.format(cart_status))
self.stdout.write(' Recovery email: {}'.format(user.email))
self.stdout.write(' Billing email: {}'.format(user.billing_email))
self.stdout.write(' Account type: {}'.format(user.account_type.display))
self.stdout.write(' VPN activated: {}'.format(tunnel_status))
self.stdout.write(' Account active: {}'.format(user_acitve))
self.stdout.write('Account deleted: {}'.format(account_deleted))
self.stdout.write('')
services = user.limit_set.all()
self.stdout.write('Services information')
self.stdout.write('--------------------')
for service in services:
self.stdout.write('Service: {}'.format(service.service_label()))
self.stdout.write('Renewal: {}'.format(service.renewal_date))
self.stdout.write('Enabled: {}'.format(service.is_enabled()))
self.stdout.write('')
self.stdout.write('Storage information')
self.stdout.write('-------------------')
self.stdout.write(' RMA: {}'.format(user.rma))
self.stdout.write(' Quota: {}'.format(user.quota))
self.stdout.write('Over quota since: {}'.format(user.over_since))
self.stdout.write('Last cached cost: ${}'.format(user.storage_cost_cache))
self.stdout.write(' Account locked: {}'.format(locked_status))
self.stdout.write('')
subs = SubscriptionManager.search_subscriptions(user)
self.stdout.write('Subscriptions information')
self.stdout.write('-------------------------')
if not subs:
self.stdout.write('No subscriptions found for the user.')
self.stdout.write('')
else:
for sub in subs:
self.stdout.write(' ID: {}'.format(sub.subscription_id))
self.stdout.write(' Type: {}'.format(sub.account_type.display))
self.stdout.write(' Period: {}'.format(sub.billing_period.value))
self.stdout.write(' Status: {}'.format(sub.status.value))
self.stdout.write(' Next renewal: {}'.format(sub.next_payment_date))
self.stdout.write(' Product ID: {}'.format(sub.product_id))
self.stdout.write(' Variation ID: {}'.format(sub.variation_id))
self.stdout.write('Variation name: {}'.format(sub.variation_name))
self.stdout.write(' Order ID: {}'.format(sub.order_id))
self.stdout.write(' Price: {}'.format(sub.price))
self.stdout.write('')
self.stdout.write('Invitations information')
self.stdout.write('-----------------------')
invs = user.invitation_set.all()
if not invs.exists():
self.stdout.write('No invitations created by the user.')
self.stdout.write('')
else:
for inv in invs:
self.stdout.write(' Owner: {}'.format(inv.owner.username))
self.stdout.write(' msg: {}'.format(inv.msg))
self.stdout.write(' msg_hash: {}'.format(inv.msg_hash))
self.stdout.write('Created date: {}'.format(inv.created_date))
self.stdout.write(' Consumed: {}'.format(inv.consumed))
guest = inv.guest.username if inv.guest else 'None'
self.stdout.write(' Guest: {}'.format(guest))
self.stdout.write('')
self.stdout.write('Upgrades/Downgrades information')
self.stdout.write('-------------------------------')
changes = user.bundlechange_set.all()
if not changes.exists():
self.stdout.write('No upgrades or downgrades found.')
self.stdout.write('')
else:
bundles = get_available_bundles()
for change in changes:
from_bdl = bundles[change.from_bdl]['short_title']
to_bdl = bundles[change.to_bdl]['short_title']
self.stdout.write(' From: {}'.format(from_bdl))
self.stdout.write(' To: {}'.format(to_bdl))
self.stdout.write(' Is pending?: {}'.format(change.is_pending))
self.stdout.write(' Pay URL: {}'.format(change.pay_url))
self.stdout.write(' New order ID: {}'.format(change.new_order_id))
self.stdout.write('Old Subscription ID: {}'.format(change.old_subscription_id))
self.stdout.write(' Created date: {}'.format(change.created_date))
self.stdout.write('')
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