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.

226 lines
6.7 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
  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. rows = c.fetchall()
  104. conn.commit()
  105. conn.close()
  106. if (len(rows) > 0):
  107. return render_template('article.j2', article=rows[0])
  108. return render_template('article.j2', article=())
  109. @ldap.login_required
  110. @app.route('/add', methods=['GET', 'POST'])
  111. def add_url():
  112. if not 'user_id' in session:
  113. session['next_redirect'] = request.url
  114. return redirect(url_for('login'))
  115. if request.method == 'POST':
  116. url = request.form['url']
  117. close = None
  118. else:
  119. url = request.args.get('url')
  120. close = request.args.get('close')
  121. conn = sqlite3.connect('pocket/readitlater.db')
  122. c = conn.cursor()
  123. if url is not None and len(url) > 0:
  124. article, short_domain = get_article(url)
  125. c.execute("SELECT * FROM articles WHERE url=?", (url,))
  126. rows = c.fetchall()
  127. if (len(rows) == 0):
  128. c.execute("INSERT INTO articles (url, content, title, byline) VALUES (?, ?, ?, ?)", (url, article['content'], article['title'], short_domain))
  129. c.execute("SELECT * FROM articles WHERE url=?", (url,))
  130. rows = c.fetchall()
  131. article_id = rows[0][0]
  132. c.execute("SELECT * FROM saved_articles WHERE user=? AND article_id=?", (session['user_id'], article_id))
  133. rows = c.fetchall()
  134. if (len(rows) == 0):
  135. c.execute("INSERT INTO saved_articles (user, article_id) VALUES (?, ?)", (session['user_id'], article_id))
  136. conn.commit()
  137. conn.close()
  138. if close is not None and close == '1':
  139. return render_template('close.j2')
  140. return 'Saved'
  141. conn.commit()
  142. conn.close()
  143. return 'Error'
  144. @ldap.login_required
  145. @app.route('/delete/<int:article_id>')
  146. def delete_article(article_id):
  147. conn = sqlite3.connect('pocket/readitlater.db')
  148. c = conn.cursor()
  149. c.execute("DELETE FROM saved_articles WHERE user=? AND article_id=?", (session['user_id'], article_id))
  150. c.execute("SELECT * FROM saved_articles WHERE article_id=?", (article_id, ))
  151. rows = c.fetchall()
  152. if (len(rows) == 0):
  153. c.execute("DELETE FROM articles WHERE id=?", (article_id,))
  154. conn.commit()
  155. conn.close()
  156. return redirect(url_for('index'))
  157. @ldap.login_required
  158. @app.route('/archive/<int:article_id>')
  159. def archive_article(article_id):
  160. conn = sqlite3.connect('pocket/readitlater.db')
  161. c = conn.cursor()
  162. c.execute("UPDATE saved_articles SET read=1 WHERE user=? AND article_id=?", (session['user_id'], article_id))
  163. conn.commit()
  164. conn.close()
  165. return redirect(url_for('index'))
  166. @app.route('/logout')
  167. def logout():
  168. session.pop('user_id', None)
  169. return redirect(url_for('index'))
  170. if __name__ == '__main__':
  171. app.run()