def password_changed_notification(email, name, date=generate_date(), redirect=True): msg = Message(f'Password Changed', sender=EMAIL, recipients=[email]) msg.body = f"Hello {name}, this is an automatic email from {get_name('m')} to notify you of recent" \ f" events that occurred in regards to your account.\n\n" \ f'Your account password was changed at {date}.\n\n' \ f"If this wasn't you, contact us by replying to this email or via our website." return send_mail(msg, allow_redirects=redirect)
def validate_post_addition(post_json, requesting_user): required = ['title', 'subtitle', 'body'] try: missing = [ key for key in required if key not in list(post_json.keys()) ] except (AttributeError, TypeError): return make_response( jsonify( response= f"You are missing the following keys: {', '.join(required)}"), 400) if not missing: try: new_post = BlogPost(title=post_json['title'], subtitle=post_json['subtitle'], body=post_json['body'], author=requesting_user, img_url=post_json.get('img_url', ''), date=generate_date()) except KeyError: return make_response(jsonify(response="Malformed request."), 400) else: db.session.add(new_post) db.session.commit() log_api_post_addition(requesting_user=requesting_user, post=new_post) return make_response(jsonify(message="Post published."), 201) else: return make_response( jsonify( response= f"You are missing the following keys: {', '.join(missing)}"), 400)
def make_user_administrator(token, user, reason): load_token(token, salt='make-auth') if user: try: user.author = False except AttributeError: return abort(400) set_notification('administrator', user.email, user.name, current_user.name, reason) user.admin = True new_notification = Notification( user=user, by_user=current_user.email, user_name=current_user.name, body=f"You were set as an administrator by {current_user.name}.", date=generate_date(), category='new') db.session.add(new_notification) db.session.commit() flash( "The user has been set as an administrator, a notification has been sent to the user." ) return redirect(url_for('user_operations.user_page', user_id=user.id)) else: return abort(400)
def unblock_api_key(requested_key): try: if requested_key.blocked is True: requested_key.blocked = False new_notification = Notification( user=requested_key.developer, by_user=current_user.email, body=f"Your API Key has been unblocked by {current_user.name}.", date=generate_date(), category='unblock', user_name=current_user.name) db.session.add(new_notification) db.session.commit() flash("This API Key has been unblocked successfully.") return redirect( url_for('user_operations.user_page', current_mode='api', user_id=requested_key.developer_id)) else: flash("This API Key is not blocked.") return redirect( url_for('user_operations.user_page', current_mode='api', user_id=requested_key.developer_id)) except AttributeError: return abort(400)
def emdr_to_csv(input_dir, output_dir): total_orders, total_history = parse_raw_emdr_data(input_dir) if len(total_orders)>0: orders_cols = ["price", "volRemaining", "range", "orderID", "volEntered", "minVolume", "bid", "issueDate", "duration", "stationID", "solarSystemID"] orders = pd.DataFrame(total_orders, columns=orders_cols) orders.to_csv(os.path.join(output_dir, 'orders_' + utils.generate_date() + '.csv'), index=False) else: orders=[] if len(total_history)>0: history_cols = ["date", "orders", "quantity", "low", "high", "average","typeID","regionID"] history = pd.DataFrame(total_history, columns=history_cols) history.to_csv(os.path.join(output_dir, 'history_' + utils.generate_date() + '.csv'), index=False) else: history=[] return orders, history
def post_addition(form): new_post = BlogPost(title=form.title.data, subtitle=form.subtitle.data, author=current_user, img_url=form.img_url.data, body=form.body.data, date=generate_date()) db.session.add(new_post) db.session.commit() return redirect(url_for('home.home_page'))
def log_deletion(requested_post): log_information = get_post_log_information(requested_post) new_log = Log(user=current_user, user_name=current_user.name, category='post', description=f"{current_user.name} deleted a post.<br><br>" f"Post Details:<br><br>{log_information}", user_email=current_user.email, date=generate_date()) db.session.add(new_log) db.session.commit()
def log_newsletter_sendout(title, contents): new_log = Log( user=current_user, description=f"{current_user.name} sent out a newsletter.<br><br>" f"Title: {title}<br><br>Contents: {contents}", user_name=current_user.name, date=generate_date(), category='newsletter', user_email=current_user.email) db.session.add(new_log) db.session.commit()
def add_reply(requested_comment, reply, current_page=1): if requested_comment: comment_id = requested_comment.id if current_user.is_authenticated: new_reply = Reply(author=current_user, parent_comment=requested_comment, reply=html2text(reply), date=generate_date()) new_notification = Notification(user=requested_comment.author, by_user=current_user.email, user_name=current_user.name, body=f"{current_user.name} replied to your comment on" f" {requested_comment.parent_post.title}", parent_reply=new_reply, date=generate_date(), category='reply') db.session.add(new_reply, new_notification) db.session.commit() return redirect(url_for('comment.show_comment', comment_id=comment_id, c_page=current_page)) else: flash("User is not logged in.") return redirect(url_for('comment.show_comment', comment_id=comment_id)) else: return abort(404)
def remove_user_as_author(user, reason): try: user.author = False except AttributeError: return abort(400) remove_notification('author', user.email, user.name, current_user.name, html2text(reason)) new_notification = Notification(user=user, by_user=current_user.email, user_name=current_user.name, body=f"You were removed as an author by {current_user.name}.", date=generate_date(), category='removal') db.session.add(new_notification) db.session.commit() flash("This user has been removed as an author, a notification has been sent to the user.") return redirect(url_for('user_operations.user_page', user_id=user.id))
def log_api_post_addition(post, requesting_user): requested_key = load_api_key(requesting_user) requested_key.add_post += 1 new_log = Log( user=requesting_user, user_name=requesting_user.name, category='api_request', description= f"{requesting_user.name} published a post via an API request.<br><br>" f'Post ID: {post.id}', date=generate_date(), user_email=requesting_user.email) db.session.add(new_log) db.session.commit()
def log_api_newsletter_sendout(requesting_user, title, contents): requested_key = load_api_key(requesting_user) requested_key.newsletter_sendout += 1 new_log = Log( user=requesting_user, description= f"{requesting_user.name} sent out a newsletter via an API request.<br><br>" f"Title: {title}<br><br>Contents: {contents}", user_name=requesting_user.name, date=generate_date(), category='api_request', user_email=requesting_user.email) db.session.add(new_log) db.session.commit()
def log_api_post_deletion(requested_post, requesting_user): requested_key = load_api_key(requesting_user) requested_key.delete_post += 1 log_information = get_post_log_information(requested_post) new_log = Log( user=requesting_user, user_name=requesting_user.name, category='api_request', description= f"{requesting_user.name} deleted a post via an API request.<br><br>" f"Post Details:<br><br>{log_information}", user_email=requesting_user.email, date=generate_date()) db.session.add(new_log) db.session.commit()
def add_comment(requested_post, comment): if requested_post: post_id = requested_post.id if current_user.is_authenticated: new_comment = Comment(author=current_user, parent_post=requested_post, comment=comment, date=generate_date()) new_notification = Notification( user=requested_post.author, by_user=current_user.email, user_name=current_user.name, parent_comment=new_comment, category='comment', body=f"{current_user.name} commented on your post.", date=generate_date()) db.session.add(new_comment, new_notification) db.session.commit() return redirect(url_for('post.show_post', post_id=post_id)) else: flash("User is not logged in.") return redirect(url_for('post.show_post', post_id=post_id)) else: return abort(404)
def handle_forgot_password(token, user, new_password): load_token(token=token, salt='forget-password', redirect_to='login') if user: new_password = generate_password_hash(password=new_password, method='pbkdf2:sha256', salt_length=8) try: user.password = new_password except AttributeError: return abort(400) db.session.commit() password_changed_notification(user.email, user.name, generate_date()) flash("Password changed successfully.") return redirect(url_for('login_system.login', category='success')) else: flash("Could not find a user with the specified email address.") return redirect(url_for('home.home_page', category='danger'))
def unsubscription_verification_handling(requested_subscription, token, reason, explanation): load_token(token=token, salt='unsubscription-verify') if requested_subscription: if requested_subscription.active: requested_subscription.active = False requested_subscription.unsubscription_reason = reason requested_subscription.unsubscription_explanation = explanation requested_subscription.unsubscription_date = generate_date() db.session.commit() flash("You've unsubscribed from the newsletter successfully.") return redirect(url_for('home.home_page', category='success')) else: flash("You've already unsubscribed from our newsletter.") return redirect(url_for('home.home_page', category='success')) else: return abort(400)
def handle_email_verification(token, email): load_token(token=token, salt='email-verify', redirect_to='register') user = User.query.filter_by(email=email).first() if user: if not user.confirmed_email: user.confirmed_email = True user.join_date = generate_date() db.session.commit() login_user(user) flash("You've confirmed your email successfully.") category = 'success' else: flash("You've already confirmed your email.") category = 'danger' return redirect(url_for('home.home_page', category=category)) else: flash("This user does not exist.") return redirect(url_for('login_system.register'))
def add_subscriber(first_name, last_name, email): if first_name and last_name and email: requested_subscription = NewsletterSubscription.query.filter_by( email=email).first() if requested_subscription: if requested_subscription.active: flash("You are already subscribed to the newsletter.") return redirect(url_for('home.home_page', category='danger')) else: db.session.delete(requested_subscription) db.session.commit() new_subscriber = NewsletterSubscription(first_name=first_name, last_name=last_name, email=email, date=generate_date()) db.session.add(new_subscriber) db.session.commit() return generate_subscription_verification(email=email) else: return abort(400)
def log_changes(configuration, new_configuration, keys_lst, values_lst): data = get_data() requested_data = data.get(configuration, {}) track_changes = True if requested_data else False changes_lst = [] for index, value in enumerate(new_configuration): if '_' in keys_lst[index]: current_key = ' '.join(keys_lst[index].split('_')).title() else: current_key = keys_lst[index].title() current_change = values_lst[index] if str( values_lst[index]).strip() != '' else None if not track_changes: changes_lst.append(f"{current_key}: {current_change}") else: try: previous_version = data[configuration].get( value, f"{current_key}: {current_change}") except KeyError: changes_lst.append(f"{current_key}: {current_change}") continue if values_lst[index] != previous_version: changes_lst.append( f"{current_key}: {previous_version} -> {current_change}") changes = '<br><br>'.join(changes_lst) if any(changes_lst): new_log = Log( user=current_user, user_name=current_user.name, category='configuration', description= f"{current_user.name} configured the {' '.join(configuration.split('_')).title()}." f"<br><br>" f"Changes:<br><br>{changes}", date=generate_date(), user_email=current_user.email) db.session.add(new_log) db.session.commit()
def generate_deletion(): def create_link(decision): link_token = serializer.dumps(current_user.email, salt='deletion_request') return url_for('verification.handle_request', token=link_token, email=current_user.email, decision=decision, _external=True) if user_has_deletion_request(current_user.id): requested_report = DeletionReport.query.filter_by(user_id=current_user.id).first() if not requested_report.approval_link: try: requested_report.approval_link = create_link(decision='approved') requested_report.rejection_link = create_link(decision='rejected') requested_report.date = generate_date() except AttributeError: return abort(500) else: db.session.commit() flash("Request sent, please wait 1-3 days for administrators to review your request.") return redirect(url_for('home.home_page', category='success')) else: flash("Your account is already in a pending deletion state.") return redirect(url_for('home.home_page', category='danger')) else: return abort(400)
def log_api_post_edition(requested_post, changes_json, requesting_user): requested_key = load_api_key(requesting_user) requested_key.edit_post += 1 changes = [] for key in changes_json: if getattr(requested_post, key) != changes_json[key] and any( str(changes_json[key]).strip()): new_change = f"{key.title() if key != 'img_url' else 'Image URL'}:" \ f" {requested_post[key]} -> {changes_json[key]}" changes.append(new_change) changes_description = '<br><br>'.join(changes) if any(changes): new_log = Log( user=requesting_user, user_name=requesting_user.name, category='api_request', description= f"{requesting_user.name} edited a post via an API request.<br>" f'Post ID: {requested_post.id}<br><br>' f'Changes:<br><br>{changes_description}', user_email=requesting_user.emai, date=generate_date()) db.session.add(new_log) db.session.commit()