def get_report(): try: file = open(os.path.join(app.config['UPLOAD_FOLDER'], app.config['FILE_NAME']+'.txt')) report_file_path = app.config['UPLOAD_FOLDER']+'/report.pdf' email = request.args['messages'] hash_values = [line.strip() for line in file] total = len(hash_values) counter = 0 report = [] for lines in list(helper_service.divide_chunks(hash_values, 4)): api_server_hits = 0 for line in lines: try: response = query_service.query_database(line) if 'update_flag' in response.keys() and 'value' in response.keys(): report.append(query_service.query_server(response['value'],response['update_flag'])) api_server_hits+=1 else: report.append(response) except Exception as e: print(e) continue counter += len(lines) if api_server_hits>0: time.sleep(60) print("Percentage of records processed: {}%".format((counter/total)*100)) pdf_service.create_report_pdf(report,report_file_path) email_service.send_email(app.config['GENERIC_EMAIL'],email,app.config['PASSWD'],'report.pdf',report_file_path) return "Please find the report attached in the email sent to: "+ email except Exception as e: print(e)
def create_item(**params): try: response = requests.post(config.endpoints.items, json=params) except Exception: raise exceptions.StorageServerError if response.status_code == 400: raise exceptions.ItemAlreadyExists elif response.status_code == 201: email_service.send_email("New Item created", "A new Item has been created") return response.json()['item_id'] raise exceptions.StorageServerError()
def add_user(birth_date, nick, about, email, password): db = db_session.create_session() user = User(nickname=nick, about=about, birth_date=birth_date, email=email) user.set_password(password) confirm_url = url_for('confirm_email', token=generate_confirmation_token(user.email), _external=True) html = render_template('confirm_account.html', confirm=confirm_url, nick=user.nickname) send_email(user.email, "Подтверждение почты на pih-poh.online", html) db.add(user) db.commit() db.close()
def checkout(self): to_charge = self.apply_discount() try: response = requests.put(config.endpoints.cart + self._cart_id, json={'status': 'Checked out', 'payment_method': self._payment_method, 'total_amount': to_charge, 'username': self._cart_db['username']}) except Exception: raise exceptions.StorageServerError() # Esto también podría ejecutarse en un thread separado ya que ya se le cobró al cliente... if response.status_code == 200: email_service.send_email("You sold!", "A new " + self._payment_method + " transaction was made.") return response.json() else: # Haría rollback del cobro con la pasarela raise exceptions.StorageServerError()
def personal(): if request.method == 'GET': return render_template('investor/personal.html') if request.form.get('doRegister') is None: user = User.query.filter_by(username=current_user.id).first() old_password = request.form['old_password'] new_password1 = request.form['new_password1'] new_password2 = request.form['new_password2'] status, message = change_password(user, old_password, new_password1, new_password2) if status: return redirect(url_for('home')) else: flash(message, 'danger') return render_template('investor/personal.html') else: if admin_permission.can(): new_user_name = request.form['username'] user = User.query.filter_by(username=new_user_name).first() if user is None: new_user_email = request.form.get('email') is_admin = 1 if request.form.get("is_admin") == 1 else 0 message = send_email(new_user_name, new_user_email, is_admin, mail) flash(message, 'success') return redirect(url_for('personal')) else: flash("Given username already exists!", 'danger') return render_template('investor/personal.html', error_add_user="******") abort(403)
def update_item(item_id, token, **params): if user_service.valid_admin(token): previous_item = get_item(item_id) try: response = requests.put(config.endpoints.item + item_id, json=params) except Exception: raise exceptions.StorageServerError if response.status_code == 404: raise exceptions.ItemNotFoundException() elif response.status_code == 400: raise exceptions.BadRequest(response.text) elif response.status_code == 200: item_updated = response.json() if item_updated['price'] != previous_item['price']: email_service.send_email("New Price!", "The new price for " + item_updated['name'] + " is " + str(item_updated['price'])) return item_updated else: raise exceptions.StorageServerError() else: raise exceptions.NotAnAdmin()