Esempio n. 1
0
async def create_new_comment(repo_id: int, post_id: int, type: str,
                             content: str, commenter_name: str):
    if await check_post_existing(repo_id, post_id):
        current_id = await fetchrow(
            '''
        SELECT max(comment_id) FROM comments
        WHERE repo_id=$1 and post_id=$2
        ''', repo_id, post_id)
        this_id = 1
        if current_id['max'] != None:
            this_id = current_id['max'] + 1
        await execute(
            '''
                INSERT INTO comments(post_id,repo_id,comment_id,is_reply,reply_to_id,address_time,type,content,commenter)
                VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9)
                ''', post_id, repo_id, this_id, False, None, datetime_now(),
            type, content, commenter_name)

        notification = commenter_name+' commented your post (id: ' + \
            str(repo_id)+'_'+str(post_id)+')'
        poster = await get_postername(repo_id, post_id)
        poster_id = await get_uid(poster)
        await create_new_notification(poster_id, notification)

        return this_id
    else:
        raise PostNotFoundException()
Esempio n. 2
0
async def create_pull_request(dstowner: str, dstrepo: str, dstbranch: str,
                              srcowner: str, srcrepo: str, srcbranch: str,
                              title: str, authorname: str,
                              visibility: bool) -> Tuple[int, bool, str]:
    status = "Open"
    authoremail = await user.get_user_mail_address(authorname)
    dst_repo_id = await repo.get_repo_id(dstowner, dstrepo)
    src_repo_id = await repo.get_repo_id(srcowner, srcrepo)
    pull_id = await post.create_new_attached_post(dst_repo_id, srcowner, title,
                                                  status, visibility, False)

    branches = (await git.git_branches(dstowner, dstrepo))[1]
    if not dstbranch in branches:
        raise BranchNotFoundException()

    flag_auto_merged, conflicts = await create_pull_request_git(
        dstowner, dstrepo, dstbranch, pull_id, srcowner, srcrepo, srcbranch,
        authorname, authoremail, title)
    await execute(
        '''
        INSERT INTO pull_requests(dest_repo_id,dest_branch,pull_id,src_repo_id,src_branch,
                                  created_time,status,auto_merge_status)
        VALUES($1,$2,$3,$4,$5,$6,$7,$8)
        ''', dst_repo_id, dstbranch, pull_id, src_repo_id, srcbranch,
        datetime_now(), status, flag_auto_merged)

    postid = await post.create_new_attached_post(dst_repo_id, srcowner, title,
                                                 status, visibility, False)
    return postid, flag_auto_merged, conflicts
Esempio n. 3
0
 async def prepare(self):
     data = self.get_secure_cookie('session')
     if data is None or len(data) == 0:
         self.current_user = None
         return
     try:
         dataobj = session_decode(data)
         user = dataobj.get('user', None)
         session = dataobj.get('session', None)
         logged_time = dataobj.get('logged_time',
                                   "2000-01-01T00:00:00+00:00")
         logged_time = parse_log_time(logged_time)
         now = datetime_now()
         if user is None:
             self.current_user = None
         elif now > logged_time and now - logged_time > timedelta(
                 minutes=15):
             self.current_user = AuthState(user, False)
             self.set_cookie('username', user)
         elif await check_session_status(user, session):
             self.current_user = AuthState(user, True)
             self.set_cookie('username', user)
         else:
             self.current_user = AuthState(user, False)
             self.set_cookie('username', user)
     except Exception as e:
         logging.error(str(e))
         self.current_user = None
Esempio n. 4
0
async def create_new_notification(user_id: int, content: str):
    current_id = await fetchrow(
        '''
        SELECT max(notification_id) FROM notifications
        WHERE user_id=$1
        ''', user_id)
    this_id = 1
    if current_id['max'] != None:
        this_id = current_id['max'] + 1
    await execute(
        '''
            INSERT INTO notifications(user_id,notification_id,created_time,content)
            VALUES($1,$2,$3,$4)
        ''', user_id, this_id, datetime_now(), content)
    return this_id
Esempio n. 5
0
async def user_login(username: str, password: str):
    flag = await verify_user(username, password)
    if not flag:
        return None
    temp = await fetchrow(
        '''
    SELECT username FROM log_info
    WHERE username =$1''', username)
    if temp is not None:
        await execute(
            '''
        DELETE FROM log_info WHERE username=$1
        ''', username)
    session = secrets.token_urlsafe()
    await execute(
        '''
            INSERT INTO log_info(username,session,login_time) VALUES($1,$2,$3)
        ''', username, session, datetime_now())
    return session
Esempio n. 6
0
async def create_new_attached_post(repo_id: int,
                                   postername: str,
                                   title: str,
                                   status: str,
                                   visibility: bool,
                                   is_issue: bool = True):
    current_id = await fetchrow(
        '''
        SELECT max(post_id) FROM posts
        WHERE repo_id=$1
        ''', repo_id)
    this_id = 1
    if current_id['max'] is not None:
        this_id = current_id['max'] + 1

    await execute(
        '''
        INSERT INTO posts(post_id,repo_id,is_issue,postername,title,status,visibility,post_time)
        VALUES($1,$2,$3,$4,$5,$6,$7,$8)
        ''', this_id, repo_id, is_issue, postername, title, status, visibility,
        datetime_now())
    return this_id
Esempio n. 7
0
async def create_new_repo(owner: str,
                          reponame: str,
                          introduction: str = None,
                          star_num: int = 0,
                          fork_num: int = 0,
                          visibility: bool = False,
                          forked_from: str = None,
                          default_branch: str = None):
    # 创建一个新repo,必须提供owner名和repo名
    if not check_reponame_validity(reponame):
        raise NameError('Invalid reponame')
    flag = await check_repo_existing(owner, reponame)
    if not flag:
        await execute(
            '''
                INSERT INTO repos(username,reponame,introduction,create_time,star_num,fork_num,visibility,forked_from,default_branch)
                VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9)
            ''', owner, reponame, introduction, datetime_now(), star_num,
            fork_num, visibility, forked_from, default_branch)
        await git.init_git_repo(owner, reponame)
        return True
    else:
        raise NameError('Repo already exists')
Esempio n. 8
0
async def create_new_reply(repo_id: int, post_id: int, reply_to_id: int,
                           type: str, content: str, replier_name: str):
    if await check_post_existing(repo_id, post_id):
        current_id = await fetchrow(
            '''
        SELECT max(comment_id) FROM comments
        WHERE repo_id=$1 and post_id=$2
        ''', repo_id, post_id)
        this_id = 1
        if current_id['max'] != None:
            this_id = current_id['max'] + 1
        await execute(
            '''
            INSERT INTO comments(post_id,repo_id,comment_id, is_reply,reply_to_id,address_time,type,content,commenter)
            VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9)
            ''', post_id, repo_id, this_id, True, reply_to_id, datetime_now(),
            type, content, replier_name)

        notification = replier_name + ' replied to you'
        await create_new_notification(reply_to_id, notification)

        return this_id
    else:
        raise PostNotFoundException()
Esempio n. 9
0
from unittest.mock import Mock, NonCallableMock, patch
import unittest
from datetime import timedelta
import asyncio

from gapsule import models
from gapsule.handlers.Base import BaseHandler, AuthState
from gapsule.utils.cookie_session import format_log_time, session_encode, datetime_now
from gapsule.utils import async_test
import gapsule.handlers.Base

test_time = format_log_time(datetime_now() - timedelta(seconds=1))


class BaseHandlerTestCase(unittest.TestCase):
    def create_query(self, chk_session_res, session_cookie):
        check_session_status = Mock(return_value=chk_session_res)
        gapsule.handlers.Base.check_session_status = asyncio.coroutine(
            check_session_status)
        if session_cookie is not None:
            get_secure_cookie = Mock(
                return_value=session_encode(session_cookie))
        else:
            get_secure_cookie = Mock(return_value=None)
        base_handler = NonCallableMock(spec=BaseHandler,
                                       get_secure_cookie=get_secure_cookie,
                                       prepare=BaseHandler.prepare)
        return base_handler, get_secure_cookie, check_session_status

    @async_test
    async def test_get_current_user1(self):