def signup_post():
    email = request.form.get('email')
    username = request.form.get('username')
    password = request.form.get('password')

    print("Signup for: ", request.form.get("email"),
          request.form.get('username'), request.form.get('password'))

    user = DBConn().select_user(
        username
    )  # if this returns a user, then the email already exists in database

    # TODO login vs signup pages
    if user:  # if a user is found, we want to redirect back to signup page so user can try again
        flash('User already exists')
        return redirect(url_for('login_page'))

    # create new user with the form data. Hash the password so plaintext version isn't saved.
    hash_pass = generate_password_hash(password)
    new_user = DBUser(username=username, email=email)

    # add the new user to the database
    DBConn().insert_user(new_user, hash_pass)

    return redirect(url_for('login_page'))
Example #2
0
 def test_select_category_articles(self):
     exp_articles = [(1, 'a'), (3, 'c')]
     act_articles = DBConn(
         TestDBConn.DB_FILENAME).select_category_articles('y_b')
     self.assertEqual(exp_articles, act_articles)
     exp_articles = [(1, 'a'), (2, 'b'), (3, 'c')]
     act_articles = DBConn(
         TestDBConn.DB_FILENAME).select_category_articles('tegor')
     self.assertEqual(exp_articles, act_articles)
Example #3
0
 def test_select_tunit_category_exists(self):
     exp_t_unit_list = [
         TUnit('sentence_a', 1, 'url', 1234, 1, 18.1, -66.7, 0, 0, 0),
         TUnit('sentence_b', 2, 'url', 1234, 2, 30.0, 30.25, 1, 1, 1)
     ]
     act_t_unit_list = DBConn(
         TestDBConn.DB_FILENAME).select_tunit_category('y_a')
     self.assertEqual(exp_t_unit_list, act_t_unit_list)
     exp_t_unit_list = [
         TUnit('sentence_a', 1, 'url', 1234, 1, 18.1, -66.7, 0, 0, 0),
         TUnit('sentence_b', 2, 'url', 1234, 2, 30.0, 30.25, 1, 1, 1),
         TUnit('sentence_c', 3, 'url', 1234, 3, -1.0, 30.0, 2, 2, 2)
     ]
     act_t_unit_list = DBConn(
         TestDBConn.DB_FILENAME).select_tunit_category('category')
     self.assertEqual(exp_t_unit_list, act_t_unit_list)
Example #4
0
 def test_insert_user(self):
     exp_user = DBUser(username='******', email='*****@*****.**')
     exp_password = '******'
     user_id = DBConn(TestDBConn.DB_FILENAME).insert_user(
         exp_user, exp_password)
     conn = sqlite3.connect(TestDBConn.DB_FILENAME)
     query = '''
     SELECT *
     FROM user
     WHERE user_id = ?
     '''
     rows = conn.cursor().execute(query, (user_id, )).fetchall()
     conn.close()
     self.assertEqual(len(rows), 1)
     row = rows[0]  # row: (1, 'Jim', 'jim@email', 'pass', 0, 0, 0, 0)
     exp_user.user_id = row[0]
     act_user = DBUser(user_id=row[0],
                       username=row[1],
                       email=row[2],
                       wins=row[4],
                       losses=row[5],
                       num_answered=row[6],
                       num_answered_correct=row[7])
     act_password = row[3]
     self.assertEqual(exp_user, act_user)
     self.assertEqual(exp_password, act_password)
Example #5
0
 def test_select_tunit_location_exists(self):
     exp_t_unit_list = [
         TUnit('sentence_a', 1, 'url', 1234, 1, 18.1, -66.7, 0, 0, 0)
     ]
     act_t_unit_list = DBConn(
         TestDBConn.DB_FILENAME,
         TestDBConn.SEARCH_RADIUS).select_tunit_location('00601')
     self.assertEqual(exp_t_unit_list, act_t_unit_list)
Example #6
0
 def test_select_tunit_random(self):
     exp_t_unit_list = [
         TUnit('sentence_a', 1, 'url', 1234, 1, 18.1, -66.7, 0, 0, 0),
         TUnit('sentence_b', 2, 'url', 1234, 2, 30.0, 30.25, 1, 1, 1),
         TUnit('sentence_c', 3, 'url', 1234, 3, -1.0, 30.0, 2, 2, 2),
         TUnit('sentence_d', 4, 'url', 1234, 4, -1.0, 30.25, 3, 3, 3)
     ]
     act_t_unit = DBConn(TestDBConn.DB_FILENAME).select_tunit_random()
     self.assertIn(act_t_unit, exp_t_unit_list)
Example #7
0
 def test_delete_category(self):
     category = 'category_a'
     DBConn(TestDBConn.DB_FILENAME).delete_category(category)
     query = '''
     SELECT *
     FROM category
     WHERE name = ?'''
     conn = sqlite3.connect(TestDBConn.DB_FILENAME)
     act_category = conn.cursor().execute(query, (category, )).fetchone()
     self.assertIsNone(act_category)
def login_post():
    username = request.form.get('username')
    password = request.form.get('password')

    # TODO implement this?
    #remember = True if request.form.get('remember') else False
    remember = False

    user = DBConn().select_user(username)

    if user is not None and check_password_hash(
            DBConn().select_password(user.username), password):
        print("Logging user", username, "in")
        session.permanent = True
        session['username'] = user.username
        return redirect(url_for('index'))

    flash('Please check your login details and try again.')
    return redirect(url_for('login_page'))
def _get_article_features(page_html: str,
                          url: str,
                          access_timestamp: int,
                          article_id: int = 1,
                          importance: float = -1) -> Article:
    """Parses the features of Article from the page html.

    :param page_html: the HTML of the page.
    :type page_html: str
    :param url: the URL of the page.
    :type url: str
    :param access_timestamp: the Unix timestamp at which the page was accessed.
    :type access_timestamp: int
    :param article_id: the ID of the article in the database.
    :type article_id: int.
    :returns: the Article object representing the Wikipedia page.
    """
    soup = bs4.BeautifulSoup(page_html, features="html.parser")
    content = ''
    for tag in soup.findAll('p'):
        span_tags = tag.findAll('span')
        # Check if there is LaTeX in the paragraph tag.
        for span in span_tags:
            if span.has_attr('mwe-math-element'):
                break
        else:
            content += ''.join(tag.strings) + '\n'

    content = preprocess_text(content)
    content = features.resolve_coreferences(content)

    # Get categories from original Wikipedia article.
    categories = DBConn().select_article_categories(article_id)

    # Convert list of tuples to list of strings.
    categories = [category[0] for category in categories]

    # categories_div = soup.find('div', {'id': 'mw-normal-catlinks'})
    # if categories_div.ul.children:
    #     for li in categories_div.ul.children:
    #         categories.append(li.text)

    long_span = soup.find('span', {'class': 'longitude'})
    lat_span = soup.find('span', {'class': 'latitude'})

    if long_span and lat_span:
        longitude = convert_dms_to_decimal(long_span.text)
        latitude = convert_dms_to_decimal(lat_span.text)
    else:
        longitude = None
        latitude = None

    article = Article(content, url, article_id, categories, importance,
                      access_timestamp, latitude, longitude)
    return article
Example #10
0
 def test_insert_category(self):
     exp_category = ('category', 1.5)
     DBConn(TestDBConn.DB_FILENAME).insert_category(*exp_category)
     query = '''
     SELECT name, importance
     FROM category
     WHERE name = ?'''
     conn = sqlite3.connect(TestDBConn.DB_FILENAME)
     act_category = conn.cursor().execute(query,
                                          (exp_category[0], )).fetchone()
     self.assertEqual(exp_category, act_category)
Example #11
0
 def test_delete_tunit(self):
     t_unit = TUnit('sentence_a', 1, 'url', 1234, 1, 30, 30, 0, 0, 0)
     DBConn(TestDBConn.DB_FILENAME).delete_tunit(t_unit)
     query = '''
             SELECT t_unit_Id
             FROM t_unit
             WHERE t_unit_Id = ?
             '''
     conn = sqlite3.connect(TestDBConn.DB_FILENAME)
     row = conn.cursor().execute(query, (1, )).fetchone()
     self.assertIsNone(row)
Example #12
0
 def test_delete_user(self):
     user = DBUser(0, 'Jill', '*****@*****.**', 5, 5, 10, 5)
     DBConn(TestDBConn.DB_FILENAME).delete_user(user)
     query = '''
     SELECT username
     FROM user
     WHERE username = ?
     '''
     conn = sqlite3.connect(TestDBConn.DB_FILENAME)
     act_user = conn.cursor().execute(query, (user.username, )).fetchone()
     self.assertIsNone(act_user)
Example #13
0
 def test_update_password(self):
     exp_username = '******'
     exp_password = '******'
     DBConn(TestDBConn.DB_FILENAME).update_password(exp_username,
                                                    exp_password)
     query = '''
     SELECT username, password
     FROM user
     WHERE username = ?
     '''
     conn = sqlite3.connect(TestDBConn.DB_FILENAME)
     act_username, act_password = conn.cursor().execute(
         query, (exp_username, )).fetchone()
     self.assertEqual(exp_username, act_username)
     self.assertEqual(exp_password, act_password)
Example #14
0
 def test_select_random_article(self):
     observations = {}
     for j in range(10000):
         article_id, article_text = DBConn(
             TestDBConn.DB_FILENAME).select_random_article()
         self.assertEqual(article_id, ord(article_text) - 96)
         if article_id in observations.keys():
             observations[article_id] += 1
         else:
             observations[article_id] = 1
     act_chisq, p = chisquare(list(observations.values()))
     # NOTE(Alex): This value is retrieved from a chi square critical value chart with 9 degrees of freedom
     # (10 articles - 1) and using a P value of 0.01
     exp_max_chisq = 21.67
     self.assertLess(act_chisq, exp_max_chisq)
Example #15
0
 def test_update_tunit_does_not_exist(self):
     exp_t_unit = TUnit('sentence', 1, 'url', 1234, None, 10, 10, 0, 0, 0)
     t_unit_Id = DBConn(TestDBConn.DB_FILENAME).update_tunit(exp_t_unit)
     self.assertEqual(t_unit_Id, exp_t_unit.t_unit_id)
     conn = sqlite3.connect(TestDBConn.DB_FILENAME)
     query = """
             SELECT sentence, article_id, url, access_timestamp, t_unit_Id, lat, long, num_likes, num_mehs,
                 num_dislikes
             FROM t_unit
             WHERE t_unit_Id = ?;
             """
     row = conn.cursor().execute(query, (t_unit_Id, )).fetchone()
     conn.close()
     act_t_unit = TUnit(*row)
     self.assertEqual(exp_t_unit, act_t_unit)
def get_page_by_random() -> Article:
    """Gets the contents and metadata of a random Wikipedia article.
    
    :returns: the Article object representing the Wikipedia article.
    """
    article_id, title = None, None
    page_html = None
    url = None
    while page_html is None:
        article_id, title, importance = DBConn(
        ).select_weighted_random_article()
        url = BASE_URL + title.replace(' ', '_')
        page_html = _get_page_from_title(title)
    access_timestamp = int(time.time())

    article = _get_article_features(page_html, url, access_timestamp,
                                    article_id, importance)
    return article
def get_page_by_category(category: str) -> Article:
    """Gets the contents and metadata of a Wikipedia article with a given category.

    :param category: the category with which to search.
    :type category: str
    :returns: the Article obj ect representing the Wikipedia article.

    """
    articles_with_category = DBConn().select_category_articles(category)
    print("articles by category:", articles_with_category)
    if not articles_with_category:
        return None

    article_id, title = random.choice(articles_with_category)
    url = BASE_URL + title.replace(' ', '_')
    page_html = _get_page_from_title(title)
    access_timestamp = int(time.time())

    article = _get_article_features(page_html, url, access_timestamp,
                                    article_id)
    return article
def get_page_by_location_zip(zip_code: str):
    """Gets the contents and metadata of a a Wikipedia article close to the given zip code.

    :param zip_code: the zipcode of the desired location, as a string
    :returns: the Article object representing the Wikipedia article
    """
    article_id, title = None, None
    page_html = None
    url = None
    while page_html is None:
        article_list = DBConn().select_articles_location(zip_code)
        print("location article list: ", article_list)
        if article_list is None or len(article_list) == 0:
            print("didn't find anything at zip", zip_code)
            return None
        article_id, title = random.choice(article_list)
        url = BASE_URL + title.replace(' ', '_')
        page_html = _get_page_from_title(title)
    access_timestamp = int(time.time())
    article = _get_article_features(page_html, url, access_timestamp,
                                    article_id)
    return article
Example #19
0
 def test_update_user_does_not_exist(self):
     user = DBUser(username='******', email='*****@*****.**')
     exp_user_id = -1
     act_user_id = DBConn(TestDBConn.DB_FILENAME).update_user(user)
     self.assertEqual(exp_user_id, act_user_id)
Example #20
0
 def test_select_user_does_not_exist(self):
     act_user = DBConn(TestDBConn.DB_FILENAME).select_user('bum')
     self.assertIsNone(act_user)
Example #21
0
 def test_select_user_exists(self):
     exp_user = DBUser(1, 'Jill', '*****@*****.**', 5, 5, 10, 5)
     act_user = DBConn(TestDBConn.DB_FILENAME).select_user('Jill')
     self.assertEqual(exp_user, act_user)
Example #22
0
from flask import Flask, render_template, request
from flask_socketio import SocketIO

top_level_dir = os.path.abspath('../')
sys.path.append(top_level_dir)

from trivia_generator.web_scraper.WebScraper import get_page_by_random
from trivia_generator.NLPPreProcessor import create_TUnits
from database_connection.dbconn import DBConn

app = Flask(__name__)
socketio = SocketIO(app)

tunit_dictionary = dict()

dbconn = DBConn()


@app.route('/')
@app.route('/index')
def index():
    return render_template("index.html")


@socketio.on('update_rank')
def update_rank(rank):
    try:
        tunit = tunit_dictionary[request.sid]
        if rank == 'like':
            tunit.num_likes += 1
        elif rank == 'dislike':
Example #23
0
 def test_select_tunit_category_does_not_exist(self):
     exp_t_unit_list = []
     act_t_unit_list = DBConn(
         TestDBConn.DB_FILENAME).select_tunit_category('does not exist')
     self.assertEqual(exp_t_unit_list, act_t_unit_list)
Example #24
0
 def test_select_tunit_location_does_not_exist(self):
     exp_t_unit_list = []
     act_t_unit_list = DBConn(
         TestDBConn.DB_FILENAME,
         TestDBConn.SEARCH_RADIUS).select_tunit_location('75163')
     self.assertEqual(exp_t_unit_list, act_t_unit_list)
Example #25
0
 def test_select_password(self):
     exp_password = '******'
     act_password = DBConn(TestDBConn.DB_FILENAME).select_password('Jill')
     self.assertEqual(exp_password, act_password)
Example #26
0
 def test_select_articles_location(self):
     exp_articles = [(1, 'a')]
     act_articles = DBConn(
         TestDBConn.DB_FILENAME,
         TestDBConn.SEARCH_RADIUS).select_articles_location('00601')
     self.assertEqual(exp_articles, act_articles)
Example #27
0
 def test_select_articles_location_does_not_exist(self):
     exp_articles = []
     act_articles = DBConn(
         TestDBConn.DB_FILENAME).select_articles_location('25974')
     self.assertEqual(exp_articles, act_articles)
Example #28
0
 def test_select_article_categories(self):
     article_id = 1
     exp_categories = ['category_a', 'category_b', 'category_c']
     act_categories = DBConn(
         TestDBConn.DB_FILENAME).select_article_categories(article_id)
     self.assertEqual(exp_categories, act_categories)