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.

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