예제 #1
0
    def view_task(self, body_values, params):
        user_name = params[1]
        db = cugc.utils.db.get_db(self.app)

        if user_name == 'all':
            cur = db.execute('SELECT * FROM task')
        else:
            cur = db.execute(
                'SELECT * FROM task WHERE assigned_to=:assigned_to',
                {'assigned_to': user_name})
        entries = cur.fetchall()
        entry_str_arr = []
        for entry in entries:
            tags = entry['tags'].split(',')
            entry_str_arr.append(
                '|{task_id}|{title}|{description}|{assigned_to}|{deadline}|\
                {updated_by}|{created_by}|{updated_at}|{created_at}|'.format(
                    task_id=entry['id'],
                    title=entry['title'],
                    description=entry['description'],
                    assigned_to='@' + entry['assigned_to'],
                    deadline=cugc.utils.util.format_timestamp(
                        entry['deadline']),
                    updated_by='@' + entry['updated_by'],
                    created_by='@' + entry['created_by'],
                    updated_at=cugc.utils.util.format_timestamp(
                        entry['updated_at']),
                    created_at=cugc.utils.util.format_timestamp(
                        entry['created_at']),
                    # tags=', '.join(['#'+n for n in tags])
                ))
        text = """|Id|Title|Description|Assigned to|Deadline|Updated by|Created by|Updated at|Created at|
|:-|:-|:-|:-|:-|:-|:-|:-|
""" + '\n'.join(entry_str_arr)  # noqa
        return text
예제 #2
0
    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)
예제 #3
0
    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.'
예제 #4
0
    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.'
예제 #5
0
    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
예제 #6
0
 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)
예제 #7
0
    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'
예제 #8
0
    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'
예제 #9
0
    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'
예제 #10
0
 def get_banner(self, body_values, params):
     db = cugc.utils.db.get_db(self.app)
     cur = db.execute(
         'SELECT * FROM banner ORDER BY updated_at DESC LIMIT 4'
     )
     entries = cur.fetchall()
     messages = []
     for entry in entries:
         messages.append({
             'message': entry['content'],
             'created_by': entry['created_by'],
             'updated_at': entry['updated_at']
         })
     return json.dumps(messages)
예제 #11
0
    def view_banner(self, body_values, params):
        db = cugc.utils.db.get_db(self.app)
        cur = db.execute('SELECT * FROM banner ORDER BY updated_at DESC')
        entries = cur.fetchall()
        if len(entries) == 0:
            return 'No banner'
        entry_str_arr = []
        for entry in entries:
            created_at = cugc.utils.util.format_timestamp(entry['created_at'])
            updated_at = cugc.utils.util.format_timestamp(entry['created_at'])
            entry_str_arr.append(
                '|' + str(entry['id']) + '|' + entry['content'] + '|' +
                entry['created_by'] + '|' + created_at + '|' + updated_at + '|'
            )
        return """
|Id|Content|Created by|Created at|Updated at|
|:-|:-|:-|:-|:-|
        """ + '\n'.join(entry_str_arr)
예제 #12
0
    def list_vote(self):
        db = cugc.utils.db.get_db(self.app)
        cur = db.execute('SELECT * FROM voting')
        entries = cur.fetchall()
        entry_str_arr = []
        for entry in entries:
            entry_str_arr.append(
                '|{id}|{created_by}|{created_at}|{vote_name}|'.format(
                    id=entry['id'],
                    created_by=entry['created_by'],
                    created_at=cugc.utils.util.format_timestamp(
                        entry['created_at']),
                    vote_name=entry['vote_name']))
        text = '''
|Vote ID|Created by|Time of creation|Vote Name|
|:-|:-|:-|:-|
        ''' + '\n'.join(entry_str_arr)
        return text
예제 #13
0
    def view_vote(self, body_values, params):
        if len(params) < 2:
            return 'No message given'
        vote_id = params[1]
        db = cugc.utils.db.get_db(self.app)
        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(
                '|{id}|{choice_name}|{voter}|{vote_count}|'.format(
                    id=entry['id'],
                    choice_name=entry['choice_name'],
                    voter=entry['voter'],
                    vote_count=entry['vote_count']))
        text = '''
|Choice ID|Choice Name|Voters|vote_count|
|:-|:-|:-|:-|
        ''' + '\n'.join(entry_str_arr)
        return text
예제 #14
0
    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