def settings(self): data.define( required_fields=['brand_name', 'short_code', 'support_number'], optional_fields=['notification_email', 'script_sheets']) if data.payload: data.validate(error_message="Required Data Missing") if self.livemode: self.org_app.spreadsheet_id = data.spreadsheet_id else: self.org_app.demo_spreadsheet_id = data.spreadsheet_id data.put(self.org_app) return "Updated settings" app_settings = self.org_app.to_dict(include=data.defined_fields) if self.livemode: api_secret = self.org.secure_production app_settings['spreadsheet_id'] = self.org_app.spreadsheet_id else: api_secret = self.org.secure_development app_settings['spreadsheet_id'] = self.org_app.demo_spreadsheet_id return { 'auth': { 'api_id': self.org.secure_api_id, 'api_secret': api_secret.api_secrets[0] }, 'app': app_settings, 'is_script_enabled': self.org_app.is_script_enabled }
def add_user(): data.validate( required_fields=['id', 'user_name', 'user_email', 'user_phone'], error_message='Form has errors') org = Organization.get_by_id(data.id) if not org: return error('Organization not found') user = get_user_by_email(data.user_email) if not user: user = User(raw_name=data.user_name, email=data.user_email, phone=data.user_phone, account_verified=True) user.org = [org.key] else: user.org = list(set(user.org + [org.key])) new_password = generate_random_password() user.password = web_auth.signed_password(new_password) user.put() TrackrRoles.get_or_insert('roles', parent=user.key, kind=['Admin', 'Staff']) mail_body = render('/emails/welcome.mako', new_password=new_password, user=user) send_email(data.user_email, mail_body, 'Welcome to Trackr') return 'Account Created'
def entry(self): data.validate(amount=Required(float), advance=Optional(float), required_fields=[ 'order_num', 'order_date', 'sales_phone', 'customer_phone', 'business' ], optional_fields=['category'], error_message='All required details are not present') update = updates_holder() customer = get_or_create_customer(data.business, data.customer_name, data.customer_phone, update) incharge = get_or_create_agent(data.sales_name, data.sales_phone, update) order = create_or_update_sales_order(data.order_num, data.order_date, data.amount, None, customer, incharge, data.category, update) push_updates(self.org, self.org_app, self.livemode, update) update = updates_holder() if data.invoice_num and data.invoice_date: create_invoice(data.invoice_num, data.invoice_date, order, update) push_updates(self.org, self.org_app, self.livemode, update) return "Entries Made"
def login(self): data.validate(required_fields=['email', 'password'], error_message='Invalid User / Password') user = get_verified_user_by_email(data.email.lower()) password = web_auth.signed_password(data.password) if not user: return error('Invalid User / Password') elif password != user.password: return error('Invalid User / Password') org = user.org[0].get() org_id = org.key.id() if org.secure_signup_step == 'Deactivated': return error('This account has been deactivated') web_auth.set_cookie_for_user(user.email, response) response.set_cookie("org_id", org_id, max_age=7200) response.set_cookie("is_demo", 'False') auth_payload = {'uid': user.email, 'org': org_id} token = create_token(FIREBASE_SECRET, auth_payload) return { 'message': 'Logged In', 'user': user.email, 'org': org_id, 'fbaseToken': token, 'isTemporaryPassword': user.is_temporary_password }
def new(): data.validate(pricing=Required(float), min_pricing=Required(float), required_fields=[ 'name', 'id', 'short_code', 'brand_name', 'support_number', 'admin_name', 'admin_email', 'admin_phone' ], error_message='Form has errors') org = Organization.get_by_id(data.id) if not org: org = Organization(id=data.id, name=data.name, secure_signup_step='Approved') set_api_creds(org) org.put() user = get_user_by_email(data.admin_email) if not user: user = User(raw_name=data.admin_name, email=data.admin_email, phone=data.admin_phone, account_verified=True) user.org = [org.key] else: user.org = list(set(user.org + [org.key])) new_password = generate_random_password() user.password = web_auth.signed_password(new_password) user.put() org.admin = user.key org.put() if not Trackr.get_by_id(org.key.id()): Trackr(id=org.key.id(), org=org.key, users=[user.key], short_code=data.short_code, brand_name=data.brand_name, support_number=data.support_number, secure_pricing=data.pricing, secure_min_pricing=data.min_pricing).put() TrackrRoles.get_or_insert('roles', parent=user.key, kind=['Admin', 'Staff']) mail_body = render('/emails/welcome.mako', new_password=new_password, user=user) send_email(data.admin_email, mail_body, 'Welcome to Trackr') return 'Account Created'
def new_invoice(self): data.validate( required_fields=['invoice_num', 'order_num', 'invoice_date'], error_message='All required details are not present') update = updates_holder() create_invoice(data.invoice_num, data.invoice_date, data.order_num, update) push_updates(self.org, self.org_app, self.livemode, update) return "Entries Made"
def change(self): data.validate(required_fields=['email', 'password', 'new_password']) user = get_user_by_email(data.email) password = web_auth.signed_password(data.password) if not user: return error('No user registered with this email') if password != user.password: return error('Invalid Password') user.password = web_auth.validate_password(data.new_password, data.new_password) user.is_temporary_password = False user.put() return 'Password has been changed'
def reset(self): data.validate(required_fields=['email'], error_message='') user = get_user_by_email(data.email) if not user: return error('No user registered with this email') new_password = generate_random_password() user.password = web_auth.signed_password(new_password) user.is_temporary_password = True user.put() mail_body = render('/emails/reset_link.mako', new_password=new_password) send_email(data.email, mail_body, 'Password Reset Notification') return 'Password has been reset'
def cancel_payment(self): data.validate(required_fields=['sales_phone', 'payment_id'], error_message='Valid Payment ID is required') updates = updates_holder() payment = cancel_payment(data.payment_id, data.sales_phone, updates) push_updates(self.org, self.org_app, self.livemode, updates) try: supervisor = None if payment._agent.supervisors: supervisor = payment._agent.supervisors[0].get() mail_body = render('/emails/payment_cancelled.mako', payment=payment) if supervisor and supervisor.email: send_email(supervisor.email, mail_body, 'Payment Cancelled') if self.org_app.notification_email: send_email(self.org_app.notification_email, mail_body, 'Payment Cancelled') except: logging.warn('Error sending email notifications', exc_info=True) return { "message": "Payment Cancelled", 'amount': payment.amount, 'cancel_id': payment.cancellation_id, 'invoice_id': payment.invoice.id() if payment.invoice else payment.order.id(), 'customer_phone': payment.by.id(), 'support_number': self.org_app.support_number }
def create_payment(self): data.validate( amount=Required(float), required_fields=['sales_phone', 'invoice_num'], optional_fields=['additional_data'], error_message='Valid Invoice Number and Amount is required') cache = Cache.get_by_id(data.sales_phone + data.invoice_num + str(data.amount) + (data.additional_data or '')) now = datetime.datetime.utcnow() - datetime.timedelta(hours=24) if cache: if cache.createdAt > now: return error( 'Same amount for this invoice cannot be collected more than once in a day.' ) else: cache.key.delete() updates = updates_holder() if data.additional_data: customer = Customer.get_by_id(data.additional_data) if not customer: customer = get_or_create_customer('New Business', 'New Customer', data.additional_data, updates) incharge = Agent.get_by_id(data.sales_phone) if not incharge: return error( 'Payment cannot be created from this phone number.') order = SalesOrder.get_by_id(data.invoice_num) if order: return error( 'Advance payment can only be entered for new orders.') now = datetime.datetime.utcnow() + datetime.timedelta(hours=5.5) order = create_or_update_sales_order( data.invoice_num, now.strftime('%d/%m/%Y %I:%M %p'), 0, data.amount, customer, incharge, updates, 'Placeholder') payment = order.advance.get() else: payment = create_payment(data.invoice_num, data.amount, data.sales_phone, updates) Cache(id=data.sales_phone + data.invoice_num + str(data.amount) + (data.additional_data or '')).put() push_updates(self.org, self.org_app, self.livemode, updates) try: supervisor = None if payment._agent.supervisors: supervisor = payment._agent.supervisors[0].get() mail_body = render('/emails/payment_made.mako', payment=payment) if supervisor and supervisor.email: send_email(supervisor.email, mail_body, 'New Payment Notification') if self.org_app.notification_email: send_email(self.org_app.notification_email, mail_body, 'New Payment Notification') except: logging.warn('Error sending email notifications', exc_info=True) return { "message": "Payment Created", 'customer_name': payment._customer.contact, 'customer_phone': payment.by.id(), 'agent_name': payment._agent.name, 'payment_id': payment.key.id(), 'org_brand': self.org_app.brand_name, 'support_number': self.org_app.support_number }