def onepaper(paperid, title = None): paper = get_paper_w_uploader(paperid) liked = query_db( "select count(*) as c \ from likes \ where paperid=? and userid=?", [paperid,get_user_id()], one=True)['c'] if paper['edited_at'] is not None: paper['edituser'] = query_db( "select username \ from users \ where userid = ?", [paper['edited_by']], one=True)['username'] authors=get_authors(paperid) domains=get_domains(paperid) keywords=get_keywords(paperid) comments=get_comments(paperid) return render_template('paper/onepaper.html', entry=paper, comments=comments, authors=authors, domains=domains, keywords=keywords, liked=liked, liked_by=liked_by(paperid))
def new_paper_was_added(paperid): url = url_for('onepaper', paperid=paperid, _external=True) paper = get_paper_w_uploader(paperid) authors = ", ".join([a['fullname'] for a in get_authors(paperid)]) template = "Hello %s,\n\n\ a new paper was added to Papersˠ. The paper may interest you.\n\ Title: %s\n\ Authors: %s\n\ Uploader: %s\n\ Url: %s\n\n\ Have a good day,\n\ Papers' team\n\n\ P.S. If you wish so you can unsubscribe from notifications using this: %s\ " users = users_to_notify_about_new_paper(paperid) for u in users: unsubscribe = url_for('mute_email_notifs', _external=True) msg = template % (u['username'], paper['title'], authors, paper['username'], url, unsubscribe) # todo save message to notifs table send_mail(u['email'], msg, 'New paper: %s' % (paper['title']))
def with_description(papers): for p in papers: p['domains'] = get_domains(p['paperid']) p['keywords'] = get_keywords(p['paperid']) p['authors'] = get_authors(p['paperid']) return papers
def edit_paper(paperid): if not can_edit_paper(paperid): return "<h1>It's forbidden, my dear</h1>", 403 error = None paper = query_db("select * \ from papers \ where paperid = ?", [paperid], one=True) if request.method == 'GET': request.form.title = paper['title'] request.form.authors = ", ".join([x['fullname'] for x in get_authors(paperid)]) request.form.domains = ", ".join([x['domainname'] for x in get_domains(paperid)]) request.form.keywords= ", ".join([x['keyword'] for x in get_keywords(paperid)]) if not is_internal_pdf (paper['getlink']): request.form.url = paper['getlink'] if request.method == 'POST': histore_paper_info(paper) paper_file = request.files['pdf'] if paper_file and not allowed_file(paper_file.filename): error = 'Please choose a pdf file' elif request.form['title'] == "": error = 'Please add a title' elif request.form['domains'] == "": error = 'Please specify at least one domain' elif request.form['authors'] == "": error = 'Please add some authors' elif request.form['keywords'] == "": error = 'Please add some keywords' else: con = get_db() with con: con.execute('update papers set title = ?, edited_by = ?, \ edited_at = datetime() \ where paperid = ?', [request.form['title'], get_user_id(), paperid]) authors_ids = map(get_insert_author, parse_list(request.form['authors'])) con.execute('delete from papers_authors where paperid = ?', [paperid]) for authorid in authors_ids: con.execute('insert into papers_authors \ (paperid, authorid) \ values(?,?)',[paperid, authorid]) domains_ids = map(get_insert_domain, parse_list(request.form['domains'])) con.execute('delete from papers_domains where paperid = ?', [paperid]) for domainid in domains_ids: con.execute('insert into papers_domains \ (paperid, domainid) \ values(?,?)',[paperid, domainid]) keywords_ids = map(get_insert_keyword, parse_list(request.form['keywords'])) con.execute('delete from papers_keywords where paperid = ?', [paperid]) for keywordid in keywords_ids: con.execute('insert into papers_keywords \ (paperid, keywordid) \ values(?,?)',[paperid, keywordid]) if paper_file: filename_pdf = str(paperid) + "-" + \ secure_filename(paper_file.filename) ppdf = os.path.join(app.config['UPLOAD_FOLDER'],filename_pdf) paper_file.save(ppdf) ## this is just a hack. ## In order to generate first page filename_png = str(paperid) + ".png" ppng = os.path.join(app.config['PREVIEW_FOLDER'],filename_png) os.system('papersite/gen.sh ' + ppdf + ' ' + ppng) # end of hack ## Sometimes authors provide a url to their paper ## in this case we don't store a full paper, we use the url instead if request.form['url'] != "": if paper_file: # a file was just uploaded, we already took the first page. It is a fair use. # We delete the file os.remove(ppdf) else: # The following magick will happens... # we test if a link is to un existing papers, link = paper['getlink'] if (is_internal_pdf(link)): filename_pdf = link.replace('/static/memory/pdfs/', '') ppdf = os.path.join(app.config['UPLOAD_FOLDER'],filename_pdf) os.remove(ppdf) # here we will delete file that was already uploaded some time ago # but now was remplaced by un URL. con.execute("update papers set getlink = ? \ where paperid=?", [request.form['url'], paperid]) elif paper_file: con.execute("update papers set getlink = ? \ where paperid=?", ['/static/memory/pdfs/'+filename_pdf, paperid]) ## TODO: notify some users by email about changes flash('You successfully modified the paper') return redirect(url_for('onepaper', paperid=paperid, title=request.form['title'])) return render_template('paper/edit.html', error=error, paperid=paperid, domains=query_db ("select * from domains"), keywords=query_db ("select * from keywords"), authors=query_db ("select * from authors"))
def edit_paper_meta_information(paperid): ### edit Title, authors, tags and domains lists if not can_meta_edit_paper(paperid): return "<h1>It's forbidden fro you, my sweetie.</h1>", 403 error = None paper = query_db("select * \ from papers \ where paperid = ?", [paperid], one=True) if request.method == 'GET': request.form.title = paper['title'] request.form.authors = ", ".join([x['fullname'] for x in get_authors(paperid)]) request.form.domains = ", ".join([x['domainname'] for x in get_domains(paperid)]) request.form.keywords= ", ".join([x['keyword'] for x in get_keywords(paperid)]) if request.method == 'POST': histore_paper_info(paper) if request.form['title'] == "": error = 'Please add a title' elif request.form['domains'] == "": error = 'Please specify at least one domain' elif request.form['authors'] == "": error = 'Please add some authors' elif request.form['keywords'] == "": error = 'Please add some keywords' else: con = get_db() with con: con.execute('update papers set title = ?, edited_by = ?, \ edited_at = datetime() \ where paperid = ?', [request.form['title'], get_user_id(), paperid]) authors_ids = map(get_insert_author, parse_list(request.form['authors'])) con.execute('delete from papers_authors where paperid = ?', [paperid]) for authorid in authors_ids: con.execute('insert into papers_authors \ (paperid, authorid) \ values(?,?)',[paperid, authorid]) domains_ids = map(get_insert_domain, parse_list(request.form['domains'])) con.execute('delete from papers_domains where paperid = ?', [paperid]) for domainid in domains_ids: con.execute('insert into papers_domains \ (paperid, domainid) \ values(?,?)',[paperid, domainid]) keywords_ids = map(get_insert_keyword, parse_list(request.form['keywords'])) con.execute('delete from papers_keywords where paperid = ?', [paperid]) for keywordid in keywords_ids: con.execute('insert into papers_keywords \ (paperid, keywordid) \ values(?,?)',[paperid, keywordid]) ## TODO: notify some users by email about changes flash('You successfully modified the paper') return redirect(url_for('onepaper', paperid=paperid, title=request.form['title'])) return render_template('paper/meta-edit.html', error=error, paperid=paperid, domains=query_db ("select * from domains"), keywords=query_db ("select * from keywords"), authors=query_db ("select * from authors"))