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.

216 lines
6.7 KiB

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