def edit(user,title): # get all sections sec = global_database.query(ids['section'], limit=global_database.count(ids['section'])) all_sections = [] # append all sections for section in sec: all_sections.append(section) # user editing the paper if request.method == "GET" and session['username']: # get the repo query = global_database.find_one(ids['repo'],username= user, title=title) return render_template('pages/edit.html', query=query, all_sections =all_sections ) # user update the paper else: # get date stars and avatar and validate data date, stars, avatar, pdf = validate(request) # get the old repo prev = repos.find_one({'username': user, 'title':title}) # Api maximum limit has reached if isinstance(stars, dict) or isinstance(avatar, dict): # Flash the message flash(stars) # redirect to homepage return redirect('/') heroku = True if "heroku" in request.form.get('deploy', []) else False # get data from user content = { 'username':session['username'], 'title': request.form['title'], 'new_title': request.form['title'] + "<curr>" + prev['title'], 'url_repo': request.form['repo'], 'url_pdf': pdf, 'date': f'{date}', 'description': request.form['desc'], 'new_description':request.form['desc'] + "<curr>" + prev['description'], 'star':stars, 'avatar':avatar, 'pending':True, 'section': request.form['section'], 'approved':False, 'heroku':heroku } # update the repo repos.replace_one({'username': user, 'title':title}, content, True) # show success flash("Successfully updated the paper. now wait for admin to approve") return redirect(f'/{user}/profile')
def profile(user): # validate if username is signed in if session['username'] and session['username'] == user: # get all the repos # sort by star in descending order query = global_database.query(ids['repo'], limit=global_database.count(ids['repo']),username=session['username']) queries = [] # query all the repos for query in query: # millify star query['star'] = millify(query['star']) # append data into the query queries.append(query) # render all time return render_template('pages/profile.html', queries=queries) else: # wrong url entered flash('failure page does not exist') return redirect('/')
def edit_repo(admin, user, title): # check if user is admin administrator = global_database.find_one(ids['user'], username=admin) # admin editing the paper if request.method == "GET": # check if user is admin if administrator is not None \ and session['username'] == admin: # find repo query = global_database.find_one(ids['repo'], username=user, title=title) # find section sections = global_database.query(ids['section'], limit=global_database.count( ids['section'])) return render_template('pages/edit.html', query=query, all_sections=sections) else: flash("page does not exist") return redirect(url_for('all_time')) # admin saved edited paper else: # get date stars and avatar and validate data date, stars, avatar, pdf = validate(request) # Api maximum limit has reached if isinstance(stars, dict) or isinstance(avatar, dict): # Flash the message flash(stars) # redirect to homepage return redirect('/') # get tags tags = str(request.form.get('tags')) tags = tags.split(';') tags.pop() max_tags = int(global_settings.find().next().get('max_tags', 5)) # tag overload if len(tags) > max_tags: flash("Maximum Tags Reached") return redirect( url_for('edit_repo', admin=admin, user=user, title=title)) heroku = True if "heroku" in request.form.get('deploy', [None]) else False # get data from user content = { 'username': user, 'title': request.form['title'], 'url_repo': request.form['repo'], 'url_pdf': pdf, 'date': f'{date}', 'description': request.form['desc'], 'star': stars, 'avatar': avatar, 'tags': tags, 'section': request.form['section'], 'heroku': heroku, 'approved': True, 'pending': False } # update the repo repos.replace_one({'username': user, 'title': title}, content, True) # show success flash("Successfully updated the paper") return redirect(f'/{user}/profile')
def upload(user): # get all sections sec = global_database.query(ids['section'], limit=global_database.count(ids['section'])) all_sections = [] # append all sections for section in sec: all_sections.append(section) # validate if username is signed in if session.get('username') != None and session['username'] == user: # user is uploading a paper if request.method == "GET": return render_template('pages/upload.html', all_sections=all_sections) # user is submitting the paper else: # get date stars and avatar and validate data #check if user enter the correct information date, stars, avatar, pdf = validate(request) # Api maximum limit has reached if isinstance(stars, dict) or isinstance(avatar, dict): # Flash the message flash(stars) # redirect to homepage return redirect('/') # check if paper is valid if None in {date, stars, avatar, pdf}: flash("Could not upload paper") return redirect(url_for('upload', user=user)) else: # get tags tags = str(request.form.get('tags')) tags = tags.split(';') tags.pop() max_tags = int(global_settings.find().next().get( 'max_tags', 5)) # tag overload if len(tags) > max_tags: flash("Maximum Tags Reached") return redirect(url_for('upload', user=user)) # check if title exist in the database if global_database.find_one(ids['repo'], title=request.form['title']) is not None or \ any(char in request.form.get('title') for char in {'?', '!', '/', '\\', '<', '>'}): flash( "A Paper With The Same Title is Uploaded Or The Title Has Symbols In It" ) return redirect(url_for('upload', user=user)) else: #TODO:// prevent html injection # insert into the database global_database.insert( ids['repo'], username=session['username'], title=request.form['title'], url_repo=request.form['repo'], url_pdf=pdf, date=f'{date}', description=request.form['desc'], star=stars, avatar=avatar, section=request.form['section'], tags=tags, pending=True, approved=False, ) # success flash popped up flash("Paper Successfully Uploaded") # redirect to the homepage return redirect('/') # user entered the wrong url else: # wrong url entered flash('failure page does not exist') # redirect to the homepage return redirect('/')
def all_time(): # set a limit limit = request.args.get('limit', 2, type=int) # user logged in if session.get('username') is not None: # check if admin admin = global_database.find_one(ids['user'], username=session['username'], fsr=True) else: admin = None # if clicked on sections if request.args.get('section') != None: # query according to their section query = global_database.query(ids['repo'], limit=limit, approved=True, section=request.args.get('section')) elif request.args.get('tag') != None: # query according to their tags query = global_database.query(ids['repo'], limit=limit, approved=True, tags=request.args.get('tag')) else: # query all time query = global_database.query(ids['repo'], limit=limit, approved=True) queries = [] # user clicked on load_more if limit > 2 and query.count() != 0 and limit <= query.count(): try: # get the second last query nex = query[limit - 1] # store the content content = { 'username': nex['username'], '_id': str(nex['_id']), 'title': nex['title'], 'url_title': slugify.slugify(nex['title']), 'approved': nex['approved'], 'date': nex['date'], 'description': nex['description'], 'avatar': nex['avatar'], 'url_pdf': nex['url_pdf'], 'url_repo': nex['url_repo'], 'stars': millify(nex['star']), 'queries_count': query.count(), 'limit': limit, 'section': nex['section'], 'tags': nex.get('tags', []), 'heroku': nex.get('heroku', None) } if admin is not None: content.update({"admin": admin['username']}) except IndexError: content = None # upload as json return jsonify(content) # query all the repos for elem in query: # millify star elem['star'] = millify(elem['star']) elem['url_title'] = slugify.slugify(elem['title']) queries.append(elem) # get all sections sec = global_database.query(2, limit=global_database.count(2)) all_sections = [] for section in sec: # append section to the list all_sections.append(section) # render all time return render_template('pages/query.html', queries=queries, all_sections=all_sections, admin=admin)