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.

217 lines
6.7 KiB

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