def post_fetch(): """ Get all the of the posts """ user = require_login(request.cookies) if not user: return abort(401) posts = database.post_fetch( user=user['user_name'], sort=request.args.get('sort') or 'posted_at') posts_len = len(posts) # get current page curr_page = int(request.args.get('page') ) if request.args.get('page') else 1 if curr_page == 1: posts_limit = posts[:5] else: posts_limit = posts[(5 * curr_page) - 5: ((5 * curr_page) - 5) * 2] # get the info for the top advisors top_advisors = database.get_top_advisor() return render_template('posts/posts.html', user_auth=user, config={ 'posts': posts_limit, 'posts_len': posts_len, 'pagination': ceil(posts_len / 5) + 2, 'curr_page': curr_page, 'ref': request.referrer, 'curr_sort': request.args.get('sort') or 'posted_at', 'render_next': (curr_page * 5) + len(posts_limit) - 5 < posts_len, "top_advisors": top_advisors })
def repository_view(name): user = require_login(request.cookies) if not user: return abort(401) if request.method == "POST": # Creates a feedback if the method is post data = request.form labels = ast.literal_eval(request.form.get('labels')) doc, feedback_arr = {}, [] index = 1 # we loop through the labels and using the variable index # we can point the corresponding rate and description from the form for label in labels: feedback_arr.append({ label: { "rate": int(data['rate%s' % index]), "advice": data['advice%s' % index] } }) index += 1 doc = { 'feedback': feedback_arr, 'post_name': data['name'], 'post_id': int(data['id']), '_user': user['_id'], '_username': user['user_name'], 'posted_at': datetime.now(), 'edited': False, 'like': [], 'dislike': [] } if not database.create_feedback(doc): return render_template('posts/post_single.html', user_auth=user, feedback_error=True, post=database.post_fetch(name=name, user=user['user_name'])) return redirect('/posts/' + name) # get the feedback for this posts feedbacks = list(database.feedback_fetch({'post_name': name})) top_advisors = database.get_top_advisor() return render_template('posts/post_single.html', config={ "top_advisors": top_advisors }, user_auth=user, referrer=request.referrer, post=database.post_fetch( name=name, user=user['user_name']), feedbacks=feedbacks)
def profile(username): """Renders the profile info of the user""" user = require_login(request.cookies) data = database.fetch_user(username) if not data: return {} top_advisors = database.get_top_advisor() return render_template("users/profile.html", config={ "top_advisors": top_advisors }, referrer=request.referrer, user_auth=user, curr_user=data)
def project_repo(repo): user = require_login(request.cookies) if not user: return redirect('/login') # fetch the repository requested repository = git_request(f"/repos/{user['git_username']}/{repo}") # fetch the languages used for this repository # required in order to draw the chart repo_stack = git_request('/repos/%s/%s/languages' % (user['git_username'], repo)) # gets the languages which this repository consist and get the percentage. repo_stack_sum = sum([value for key, value in repo_stack.items()]) data = { 'id': repository['id'], '_user': user['user_name'], 'name': repo, 'stack_labels': list(repo_stack), 'stack_value': [round((value / repo_stack_sum) * 100, 0) for key, value in repo_stack.items()], 'description': repository['description'], 'updated_at': datetime.strptime(repository['updated_at'], '%Y-%m-%dT%H:%M:%SZ'), 'homepage': repository['homepage'], 'html_url': repository['html_url'], 'language': repository['language'], } # checks if the method is post and the if request.method == 'POST': database.create_post(data) return redirect('/posts') top_advisors = database.get_top_advisor() return render_template('projects/project_single.html', config={ "top_advisors": database.get_top_advisor() }, referrer=request.referrer, user_auth=user, project=data)
def project(): user = require_login(request.cookies) # render login form if not login if not user: return redirect('/login') # check if the github username is provided if not user['git_username']: render_template('projects/projects_setup.html', user_auth=user) def essentials(data): return { 'id': data['id'], 'name': data['name'], 'html_url': data['html_url'], 'homepage': data['homepage'], 'description': data['description'], 'language': data['language'], 'avatar_url': data['owner']['avatar_url'], 'updated_at': datetime.strptime(data['updated_at'], '%Y-%m-%dT%H:%M:%SZ') } # make a request to github api that returns the repositories of the username provide # fetch all github projects try: projects = list(map(essentials, git_request( f"/users/{user['git_username']}/repos"))) except: projects = [] # this is the number of the public repos of the current user projects_len = len(projects) curr_page = int(request.args.get('page') ) if request.args.get('page') else 1 if curr_page == 1: project_limit = projects[:5] else: project_limit = projects[(5 * curr_page) - 5: ((5 * curr_page) - 5) * 2] top_advisors = database.get_top_advisor() return render_template('projects/projects.html', user_auth=user, config={ "projects": project_limit, "projects_len": projects_len, "pagination": round(projects_len / 5) + 1, 'curr_page': curr_page, 'ref': request.referrer, 'curr_sort': request.args.get('sort') or 'posted_at', 'render_next': (curr_page * 5) + len(project_limit) - 5 < projects_len, "top_advisors": top_advisors })
def profiel_edit(username): """Renders the profile info of the user""" user = require_login(request.cookies) if request.method == 'POST': if database.edit_user({**request.form,'_id' :user['_id']}): return redirect(f"/{ user['user_name'] }") top_advisors = database.get_top_advisor() data = database.fetch_user(username) return render_template("users/profile_edit.html", user_auth=user, config={ "referrer": request.referrer, 'top_advisors': top_advisors, "profile": data })
def edit_feedback(id): feedback = database.feedback_single(id) user = require_login(request.cookies) if not user: return abort(401) if not feedback: # return false since the feedback is not found # or there was a server error return False if request.method == "POST": data = request.form feedback_arr = [] labels = [] index = 1 for obj in feedback['feedback']: for key, value in obj.items(): labels.append(key) for label in labels: feedback_arr.append({ label: { "rate": int(data['rate%s' % index]), "advice": data['advice%s' % index] } }) index += 1 doc = { 'feedback_arr': feedback_arr, 'edited': True, '_id': feedback['_id'] } database.edit_feedback(doc) return redirect("/posts/%s" % feedback['post_name']) top_advisors = database.get_top_advisor() return render_template('feedback/edit-feedback.html', user_auth=user, config={ "store": feedback, "top_advisors": top_advisors, "referrer": request.referrer })
def home(): # we define a reusable method @ require_login that redirects the # page depending on whether the user is logged In or not user = require_login(request.cookies) # check if the user's cookies exist if not user: if request.method == 'POST': message = Mail(from_email=os.environ.get('CLIENTS_EMAIL'), to_emails=request.form.get('email'), subject="noreply", html_content=f'''<html> <body> <p>Hello {request.form.get('fullname')},</p> <p>Thank you for your message! We will get back to you as soon as possible.</p> <p>Best regards,</p> Code advisor help team </body> </html>''') try: sg = SendGridAPIClient(os.environ.get('SENDGRID_KEY')) response = sg.send(message) return redirect("/message-ok") except Exception as e: print(e.message) return render_template('index/index.html', config={'render_nav': True}) # fetch all of the posts of the students posts = database.post_fetch( sort=request.args.get('sort') or 'posted_at') # returns the posts of all students posts_len = len(posts) curr_page = int( request.args.get('page')) if request.args.get('page') else 1 if curr_page == 1: posts_limit = posts[:5] else: posts_limit = posts[(5 * curr_page) - 5:((5 * curr_page) - 5) * 2] top_advisors = database.get_top_advisor() # below we render the dashboard return render_template( 'view/dashboard.html', user_auth=user, config={ "posts": posts_limit, "posts_len": posts_len, "pagination": ceil(posts_len / 5) + 1, 'curr_page': curr_page, 'ref': request.referrer, 'curr_sort': request.args.get('sort') or 'posted_at', 'render_next': (curr_page * 5) + len(posts_limit) - 5 < posts_len, 'top_advisors': top_advisors, })