No longer awards points for solving own problem

This commit is contained in:
Collin R
2020-01-15 18:39:00 -08:00
parent 5a897fd819
commit 13fb840df7
2 changed files with 8 additions and 137 deletions

View File

@@ -9,20 +9,6 @@ from . import comment, database
CONFIGPATH = 'pointsbot.ini'
# SUBREDDIT_NAME = 'MinecraftHelp'
# PRAW_SITE_NAME = 'bot'
# TODO Make LEVELS a dict instead
# TODO Could also make a Level class or namedtuple that contains more info, e.g.
# flair css or template id
"""
LEVELS = [
('Helper', 5),
('Trusted Helper', 15),
('Super Helper', 40),
]
"""
### Main Function ###
@@ -32,18 +18,15 @@ def run():
# Get the user flair levels in ascending order by point value
# TODO Make levels a dict instead
print(config.options('Levels'))
levels = []
for opt in config.options('Levels'):
levels.append((opt, config.getint('Levels', opt)))
# TODO Could also make a Level class or namedtuple that contains more info, e.g.
# flair css or template id
levels = [(o, config.getint('Levels', o)) for o in config.options('Levels')]
levels.sort(key=lambda pair: pair[1])
print(levels)
# levels = [(key, int(val)) for key, val in config.items('Levels')]
# levels = sorted(levels, key=lambda keyval: keyval[1])
#levels = sorted(config.items('Levels'), key=lambda pair: pair[1])
# levels = []
# for opt in config.options('Levels'):
# levels.append((opt, config.getint('Levels', opt)))
# levels.sort(key=lambda pair: pair[1])
# Connect to Reddit
reddit = praw.Reddit(site_name=config['Core']['praw_site_name'])
@@ -55,8 +38,6 @@ def run():
print(f'Is mod? {bool(subreddit.moderator(redditor=reddit.user.me()))}')
# TODO pass database path instead of setting global variable
# database.DB_PATH = config['Core']['database_name']
# database.init()
db = database.Database(config['Core']['database_name'])
# The pattern to look for in comments when determining whether to award a point
@@ -135,6 +116,7 @@ def marks_as_solved(comment, solved_pattern):
#comment.refresh() # probably not needed, but the docs are a tad unclear
return (not comment.is_root
and comment.is_submitter
and not comment.parent().is_submitter
and solved_pattern.search(comment.body))

View File

@@ -2,50 +2,8 @@ import functools
import os.path
import sqlite3 as sqlite
### Globals ###
# TODO put name/path in a config file?
# DB_NAME = 'pointsbot.db'
# DB_PATH = DB_NAME
"""
DB_SCHEMA = '''
CREATE TABLE IF NOT EXISTS redditor_points (
id TEXT UNIQUE NOT NULL,
name TEXT UNIQUE NOT NULL,
points INTEGER DEFAULT 0
)
'''
"""
### Decorators ###
# TODO read config here and create a closure instead of using DB_PATH?
# or could do it in the init function somehow?
# It seems like the only alternatives would be an OO aproach or global variable
"""
def transaction(func):
@functools.wraps(func)
def newfunc(*args, **kwargs):
conn = sqlite.connect(DB_PATH)
conn.row_factory = sqlite.Row
cursor = conn.cursor()
ret = func(cursor, *args, **kwargs)
if conn.in_transaction:
conn.commit()
cursor.close()
conn.close()
return ret
return newfunc
"""
# TODO create a method decorator for Database methods
def transaction(func):
@functools.wraps(func)
@@ -57,7 +15,6 @@ def transaction(func):
self.cursor = self.conn.cursor()
created_conn = True
# ret = func(cursor, *args, **kwargs)
ret = func(self, *args, **kwargs)
if self.conn.in_transaction:
@@ -136,72 +93,4 @@ class Database:
return points
"""
### Private Functions ###
# These functions are intended for internal use, since they need to be explicity
# passed a database cursor. The public methods below are wrapped with the
# transaction decorator to remove the need for keeping a connection or cursor
# opened for the entire life of the program.
def _init(cursor):
if not exists():
cursor.execute(DB_SCHEMA)
def _add_redditor(cursor, redditor):
insert_stmt = '''
INSERT OR IGNORE INTO redditor_points (id, name)
VALUES (:id, :name)
'''
cursor.execute(insert_stmt, {'id': redditor.id, 'name': redditor.name})
return cursor.rowcount
def _add_point(cursor, redditor):
points = _get_redditor_points(cursor, redditor, add_if_none=True)
params = {'id': redditor.id, 'name': redditor.name, 'points': points + 1}
update_stmt = '''
UPDATE redditor_points
SET points = :points
WHERE id = :id AND name = :name
'''
cursor.execute(update_stmt, params)
return cursor.rowcount
def _get_redditor_points(cursor, redditor, add_if_none=False):
params = {'id': redditor.id, 'name': redditor.name}
select_stmt = '''
SELECT points
FROM redditor_points
WHERE id = :id AND name = :name
'''
cursor.execute(select_stmt, params)
row = cursor.fetchone() # TODO check if more than one row
if row:
points = row['points']
elif add_if_none:
points = 0
_add_redditor(cursor, redditor)
return points
### Public Functions ###
def exists():
return os.path.exists(DB_PATH)
# Public wrappers for the database access functions
init = transaction(_init)
add_redditor = transaction(_add_redditor)
add_point = transaction(_add_point)
get_redditor_points = transaction(_get_redditor_points)
"""