You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

242 lines
7.2 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. from utils import clean_articles
  2. import ldap as l
  3. from ldap3 import Server, Connection, ALL, MODIFY_REPLACE
  4. from flask import Flask, g, request, session, redirect, url_for, render_template, send_from_directory
  5. from flask_simpleldap import LDAP
  6. from flask_bootstrap import Bootstrap
  7. import os
  8. import sqlite3
  9. import requests
  10. from utils import clean_articles, get_article
  11. from flask_cache_buster import CacheBuster
  12. app = Flask(__name__)
  13. Bootstrap(app)
  14. app.secret_key = 'asdf'
  15. app.debug = True
  16. # Base
  17. app.config['LDAP_REALM_NAME'] = 'OpenLDAP Authentication'
  18. app.config['LDAP_HOST'] = os.environ.get('LDAP_HOST')
  19. app.config['LDAP_BASE_DN'] = os.environ.get('LDAP_BASE_DN')
  20. app.config['LDAP_USERNAME'] = os.environ.get('LDAP_USERNAME')
  21. app.config['LDAP_PASSWORD'] = os.environ.get('LDAP_PASSWORD')
  22. # OpenLDAP
  23. app.config['LDAP_OBJECTS_DN'] = 'dn'
  24. app.config['LDAP_OPENLDAP'] = True
  25. app.config['LDAP_USER_OBJECT_FILTER'] = '(&(objectclass=posixAccount)(uid=%s))'
  26. short_domain = os.environ.get('SHORT_DOMAIN')
  27. ldap = LDAP(app)
  28. config = {
  29. 'extensions': ['.js', '.css', '.csv'],
  30. 'hash_size': 10
  31. }
  32. cache_buster = CacheBuster(config=config)
  33. cache_buster.register_cache_buster(app)
  34. server = Server(app.config['LDAP_HOST'])
  35. conn = Connection(server, app.config['LDAP_USERNAME'], app.config['LDAP_PASSWORD'], auto_bind=True)
  36. @app.before_request
  37. def before_request():
  38. g.user = None
  39. if 'user_id' in session:
  40. # This is where you'd query your database to get the user info.
  41. g.user = {}
  42. @app.route("/manifest.json")
  43. def manifest():
  44. return send_from_directory('./', 'manifest.json')
  45. @app.route('/')
  46. @ldap.login_required
  47. def index():
  48. user_dict = ldap.get_object_details(session['user_id'])
  49. if 'user_id' in session:
  50. user = {'dn': 'cn={},cn=usergroup,ou=users,dc=technicalincompetence,dc=club'.format(user_dict['cn'][0].decode('ascii')),
  51. 'firstName': user_dict['givenName'][0].decode('ascii'),
  52. 'lastName': user_dict['sn'][0].decode('ascii'),
  53. 'email': user_dict['mail'][0].decode('ascii'),
  54. 'userName': user_dict['uid'][0].decode('ascii'),
  55. }
  56. conn = sqlite3.connect('pocket/readitlater.db')
  57. c = conn.cursor()
  58. c.execute("SELECT article_id, url, title, byline FROM saved_articles INNER JOIN articles on saved_articles.article_id = articles.id WHERE user=? AND read=0 OR read IS NULL", (session['user_id'], ))
  59. rows = c.fetchall()
  60. conn.commit()
  61. conn.close()
  62. return render_template('list.j2', articles = rows)
  63. @app.route('/archived')
  64. @ldap.login_required
  65. def archived():
  66. conn = sqlite3.connect('pocket/readitlater.db')
  67. c = conn.cursor()
  68. c.execute("SELECT article_id, url, title, byline FROM saved_articles INNER JOIN articles on saved_articles.article_id = articles.id WHERE user=? AND read=1", (session['user_id'], ))
  69. rows = c.fetchall()
  70. print(rows)
  71. conn.commit()
  72. conn.close()
  73. return render_template('list.j2', articles = rows)
  74. @app.route('/bookmarklet')
  75. @ldap.login_required
  76. def bookmarklet():
  77. return render_template('bookmarklet.j2')
  78. @app.route('/login', methods=['GET', 'POST'])
  79. def login():
  80. if g.user:
  81. return redirect(url_for('index'))
  82. if request.method == 'POST':
  83. user = request.form['user']
  84. passwd = request.form['passwd']
  85. test = ldap.bind_user(user, passwd)
  86. if test is None or passwd == '':
  87. return render_template('login.j2', error='Invalid credentials')
  88. else:
  89. session['user_id'] = request.form['user']
  90. session['passwd'] = request.form['passwd']
  91. if 'next_redirect' in session:
  92. next = session['next_redirect']
  93. session['next_redirect'] = ''
  94. return redirect(next)
  95. return redirect('/')
  96. return render_template('login.j2')
  97. @ldap.login_required
  98. @app.route('/article/<int:article_id>')
  99. def read_article(article_id):
  100. conn = sqlite3.connect('pocket/readitlater.db')
  101. c = conn.cursor()
  102. # c.execute("SELECT * FROM articles where id=?", (article_id,))
  103. c.execute("SELECT articles.id, url, content, title, byline, read FROM articles INNER JOIN saved_articles on saved_articles.article_id = articles.id WHERE articles.id=?", (article_id, ))
  104. rows = c.fetchall()
  105. conn.commit()
  106. conn.close()
  107. if (len(rows) > 0):
  108. return render_template('article.j2', article=rows[0])
  109. return render_template('article.j2', article=())
  110. @ldap.login_required
  111. @app.route('/add', methods=['GET', 'POST'])
  112. def add_url():
  113. if not 'user_id' in session:
  114. session['next_redirect'] = request.url
  115. return redirect(url_for('login'))
  116. if request.method == 'POST':
  117. url = request.form['url']
  118. close = None
  119. else:
  120. url = request.args.get('url')
  121. close = request.args.get('close')
  122. conn = sqlite3.connect('pocket/readitlater.db')
  123. c = conn.cursor()
  124. if url is not None and len(url) > 0:
  125. article, short_domain = get_article(url)
  126. c.execute("SELECT * FROM articles WHERE url=?", (url,))
  127. rows = c.fetchall()
  128. if (len(rows) == 0):
  129. c.execute("INSERT INTO articles (url, content, title, byline) VALUES (?, ?, ?, ?)", (url, article['content'], article['title'], short_domain))
  130. c.execute("SELECT * FROM articles WHERE url=?", (url,))
  131. rows = c.fetchall()
  132. article_id = rows[0][0]
  133. c.execute("SELECT * FROM saved_articles WHERE user=? AND article_id=?", (session['user_id'], article_id))
  134. rows = c.fetchall()
  135. if (len(rows) == 0):
  136. c.execute("INSERT INTO saved_articles (user, article_id) VALUES (?, ?)", (session['user_id'], article_id))
  137. conn.commit()
  138. conn.close()
  139. if close is not None and close == '1':
  140. return render_template('close.j2')
  141. return 'Saved'
  142. conn.commit()
  143. conn.close()
  144. return 'Error'
  145. @ldap.login_required
  146. @app.route('/delete/<int:article_id>')
  147. def delete_article(article_id):
  148. conn = sqlite3.connect('pocket/readitlater.db')
  149. c = conn.cursor()
  150. c.execute("DELETE FROM saved_articles WHERE user=? AND article_id=?", (session['user_id'], article_id))
  151. c.execute("SELECT * FROM saved_articles WHERE article_id=?", (article_id, ))
  152. rows = c.fetchall()
  153. if (len(rows) == 0):
  154. c.execute("DELETE FROM articles WHERE id=?", (article_id,))
  155. conn.commit()
  156. conn.close()
  157. return redirect(url_for('index'))
  158. @ldap.login_required
  159. @app.route('/archive/<int:article_id>')
  160. def archive_article(article_id):
  161. conn = sqlite3.connect('pocket/readitlater.db')
  162. c = conn.cursor()
  163. c.execute("UPDATE saved_articles SET read=1 WHERE user=? AND article_id=?", (session['user_id'], article_id))
  164. conn.commit()
  165. conn.close()
  166. return redirect(url_for('index'))
  167. @ldap.login_required
  168. @app.route('/unarchive/<int:article_id>')
  169. def unarchive_article(article_id):
  170. conn = sqlite3.connect('pocket/readitlater.db')
  171. c = conn.cursor()
  172. c.execute("UPDATE saved_articles SET read=0 WHERE user=? AND article_id=?", (session['user_id'], article_id))
  173. conn.commit()
  174. conn.close()
  175. return redirect(url_for('archived'))
  176. @app.route('/logout')
  177. def logout():
  178. session.pop('user_id', None)
  179. return redirect(url_for('index'))
  180. if __name__ == '__main__':
  181. app.run()