def remove_task(self, body_values, params): task_id = params[1] db = cugc.utils.db.get_db(self.app) cur = db.execute('DELETE FROM task WHERE id=:task_id', {'task_id': task_id}) db.commit() return 'Task #{task_id} successfully removed'.format(task_id=task_id)
def edit_task(self, body_values, params): updated_by = body_values['user_name'] task_id = params[1] field = params[2] value = params[3] if field == 'title': db_field = 'title' elif field == 'desc': db_field = 'description' elif field == 'assign': db_field = 'assigned_to' elif 'deadline': db_field = 'deadline' value = int( datetime.datetime.strptime(value, '%Y-%m-%d').timestamp()) db = cugc.utils.db.get_db(self.app) db.execute( 'UPDATE task SET {field} = :value, \ updated_at = :updated_at, updated_by = :updated_by \ WHERE id = :task_id'.format(field=db_field), { 'updated_at': int(time.time()), 'updated_by': updated_by, 'value': value, 'task_id': task_id }) db.commit() return "Task #{id} successfully updated!".format(id=task_id)
def delete_vote(self, body_values, params): db = cugc.utils.db.get_db(self.app) delete_id = params[1] cur = db.execute('SELECT * FROM choice WHERE id = :delete_id', {'delete_id': delete_id}) delete_row = cur.fetchone() vote_count = delete_row['vote_count'] voter = delete_row['voter'] voter_arr = shlex.split(voter) new_arr = [] for name in voter_arr: if name[1:] == body_values['user_name']: vote_count = vote_count - 1 else: new_arr.append(name) new_arr = ' '.join(new_arr) db.execute( 'UPDATE choice SET voter = :new_arr, vote_count = :vote_count\ WHERE id = :delete_id', { 'new_arr': new_arr, 'vote_count': vote_count, 'delete_id': delete_id }) db.commit() return 'Vote deleted. use /vote view to check.'
def vote_vote(self, body_values, params): if len(params) < 2: return 'No message given' voted_by = body_values['user_name'] choice_id = shlex.split(params[1]) db = cugc.utils.db.get_db(self.app) for choice in choice_id: cur = db.execute('SELECT * FROM choice WHERE id =:choice_id', {'choice_id': choice}) task_row = cur.fetchone() if task_row is not None: voter = task_row[3] vote_count = task_row[4] if voter is None: voter = '@' + voted_by else: voter = voter + ' @' + voted_by if vote_count is None: vote_count = 1 else: vote_count += 1 db.execute( 'UPDATE choice SET voter = :voter, vote_count = :vote_count\ WHERE id = :choice', { 'voter': voter, 'vote_count': vote_count, 'choice': choice }) db.commit() else: return 'choice ID not valid' return 'Voted successfully! use /vote view to check.'
def add_task(self, body_values, params): created_by = body_values['user_name'] current_time = int(time.time()) title = params[1] description = params[2] assigned_to = params[3] deadline_str = params[4] deadline = int( datetime.datetime.strptime(deadline_str, '%Y-%m-%d').timestamp()) # tags_str = params[5] # tags = tags_str.split(',') tags_str = '' db = cugc.utils.db.get_db(self.app) db.execute( 'INSERT INTO task (created_at, updated_at, title, description, \ assigned_to, created_by,updated_by, deadline, tags)' + 'VALUES (:created_at, :updated_at, :title, :description, \ :assigned_to, :created_by, :updated_by, :deadline, :tags)', { 'created_at': current_time, 'updated_at': current_time, 'title': title, 'description': description, 'assigned_to': assigned_to, 'created_by': created_by, 'updated_by': created_by, 'deadline': deadline, 'tags': tags_str }) cur = db.execute('SELECT last_insert_rowid()') task_id = '' task_row = cur.fetchone() if task_row is not None: task_id = task_row[0] db.commit() text = """ New task created! #task{id} ||| |:-|:-| |**Title**|{title}| |**Created by**|{created_by}| |**Assigned to**|{assigned_to}| |**description**|{description}| |**deadline**|{deadline}| """.format( id=task_id, title=title, created_by='@' + created_by, assigned_to='@' + assigned_to, description=description, deadline=deadline_str, # tags_text=', '.join(['#'+n for n in tags]) ) return text
def touch_banner(self, body_values, params): if len(params) < 2: return 'No banner id given' banner_id = params[1] db = cugc.utils.db.get_db(self.app) cur = db.execute( 'UPDATE banner SET updated_at=:updated_at WHERE id=:banner_id', {'updated_at': int(time.time()), 'banner_id': banner_id} ) db.commit() return 'Banner updated time successfully updated, refresh to see'
def remove_banner(self, body_values, params): if len(params) < 2: return 'No banner id given' banner_id = params[1] db = cugc.utils.db.get_db(self.app) cur = db.execute( 'DELETE FROM banner WHERE id=:banner_id', {'banner_id': banner_id} ) db.commit() return 'Banner successfully deleted, refresh to see'
def create_vote(self, body_values, params): created_by = body_values['user_name'] created_at = int(time.time()) vote_name = params[1] choice = shlex.split(params[2]) db = cugc.utils.db.get_db(self.app) db.execute( 'INSERT INTO voting (created_by, created_at, vote_name)' + 'VALUES\ (:created_by, :created_at, :vote_name)', { 'created_by': '@' + created_by, 'created_at': created_at, 'vote_name': vote_name }) vote_id = '' cur = db.execute('SELECT last_insert_rowid()') if cur.fetchone is not None: vote_id = cur.fetchone()[0] for x in choice: db.execute( 'INSERT INTO choice (vote_id, choice_name)' + 'VALUES \ (:vote_id, :choice_name)', { 'vote_id': vote_id, 'choice_name': x }) db.commit() cur = db.execute('SELECT * FROM choice WHERE vote_id =:vote_id', {'vote_id': vote_id}) entries = cur.fetchall() entry_str_arr = [] for entry in entries: entry_str_arr.append('|{choice_id}|{name}|'.format( choice_id=entry['id'], name=entry['choice_name'])) text = """ Voting created! Created by: {created_by} Voting Name: {vote_name} |Choice ID|Choice name| |:-|:-| """.format(created_by='@' + created_by, vote_name=vote_name) + '\n'.join(entry_str_arr) return text
def add_banner(self, body_values, params): if len(params) < 2: return 'No message given' created_by = body_values['user_name'] current_time = int(time.time()) content = params[1] db = cugc.utils.db.get_db(self.app) db.execute( 'INSERT INTO banner (created_by, created_at, updated_at, content) \ VALUES (:created_by, :created_at, :updated_at, :content)', { 'created_by': created_by, 'created_at': current_time, 'updated_at': current_time, 'content': content } ) db.commit() return 'Banner successfully added, refresh to see'
def init_db(): db = cugc.utils.db.get_db(app) with app.open_resource('schema.sql', mode='r') as f: db.cursor().executescript(f.read()) db.commit()