Beispiel #1
0
 def __init__(self):
     YuzukiResource.__init__(self)
     dbsession = DatabaseHelper.session()
     query = dbsession.query(Group).filter(Group.important)
     result = query.all()
     self.bunryu_groups = [(group.uid, group.name) for group in result]
     dbsession.close()
Beispiel #2
0
 def __init__(self):
     YuzukiResource.__init__(self)
     dbsession = DatabaseHelper.session()
     query = dbsession.query(Group).filter(Group.important)
     result = query.all()
     self.bunryu_groups = [(group.uid, group.name) for group in result]
     dbsession.close()
Beispiel #3
0
 def processingFailed(self, reason):
     if DEBUG:
         return Request.processingFailed(self, reason)
     else:
         if issubclass(reason.type, YuzukiException):
             exc = reason.value
             self.logger.warning(reason)
             self.setResponseCode(exc.status)
             body = generate_error_message(self, exc.status, exc.message)
         else:
             self.logger.error(reason)
             self.setResponseCode(INTERNAL_SERVER_ERROR)
             body = generate_error_message(self, INTERNAL_SERVER_ERROR, u"서버 에러가 발생하였습니다.")
         if issubclass(reason.type, SQLAlchemyError):
             YuzukiRequest.dbsession.close()
             YuzukiRequest.dbsession = DatabaseHelper.session()
         body = body.encode("UTF-8")
         self.setHeader(b'content-type', b"text/html")
         self.setHeader(b'content-length', intToBytes(len(body)))
         self.write(body)
         self.finish()
         return reason
Beispiel #4
0
 def processingFailed(self, reason):
     if DEBUG:
         return Request.processingFailed(self, reason)
     else:
         if issubclass(reason.type, YuzukiException):
             exc = reason.value
             self.logger.warning(reason)
             self.setResponseCode(exc.status)
             body = generate_error_message(self, exc.status, exc.message)
         else:
             self.logger.error(reason)
             self.setResponseCode(INTERNAL_SERVER_ERROR)
             body = generate_error_message(self, INTERNAL_SERVER_ERROR,
                                           u"서버 에러가 발생하였습니다.")
         if issubclass(reason.type, SQLAlchemyError):
             YuzukiRequest.dbsession.close()
             YuzukiRequest.dbsession = DatabaseHelper.session()
         body = body.encode("UTF-8")
         self.setHeader(b'content-type', b"text/html")
         self.setHeader(b'content-length', intToBytes(len(body)))
         self.write(body)
         self.finish()
         return reason
# -*- coding: utf-8 -*-
from hashlib import sha256

from helper.database import DatabaseHelper
from model.board import Board
from model.group import Group
from model.user import User

DatabaseHelper.create_all()
dbsession = DatabaseHelper.session()

"""
BASIC REQUIREMENT FROM HERE
"""
anybody = Group(u"anybody")
anybody.description = "All user must be approved by being assigned to this group"
dbsession.add(anybody)

nobody = Group("nobody")
nobody.description = "nobody can be registered in this group"
dbsession.add(nobody)

admin = Group("admin")
dbsession.add(admin)

notice = Board("notice", u"공지사항", admin, anybody)
dbsession.add(notice)

free = Board("free", u"자유게시판", anybody, anybody)
dbsession.add(free)
Beispiel #6
0
class YuzukiRequest(Request):
    dbsession = DatabaseHelper.session()

    def initialize(self, resource):
        """
        if you need to do something just before or just after a request is
        initialized, write it here
        """
        self._initial_session = copy.deepcopy(self.yzk_session)
        auto_login = "******" in self.received_cookies and\
                     "auto_pw" in self.received_cookies
        if auto_login:
            username = self.getCookie("auto_id")
            password = self.getCookie("auto_pw")
            if self.user is None:
                query = self.dbsession.query(User)\
                                      .filter(User.username == username)
                result = query.all()
                if result:
                    user = result[0]
                    if user.check_password(password):
                        self.log_user_in(user)
                    else:
                        self.remove_auto_login_cookie()
                else:
                    self.remove_auto_login_cookie()
            else:
                username = self.getCookie("auto_id")
                password = self.getCookie("auto_pw")
                if self.user.username == username and\
                   self.user.check_password(password):
                    # refresh auto login cookie expire time
                    self.set_auto_login(username, password)

    def remove_auto_login_cookie(self):
        expires_date = datetime.now() - timedelta(days=2)
        expires = expires_date.strftime("%a, %d-%b-%Y %H:%M:%S GMT")
        self.addCookie("auto_id", "", expires=expires, path="/")
        self.addCookie("auto_pw", "", expires=expires, path="/")

    def set_auto_login(self, username, password):
        username = username.encode("utf-8")
        password = password.encode("utf-8")
        expires_date = datetime.now() + timedelta(days=2)
        expires = expires_date.strftime("%a, %d-%b-%Y %H:%M:%S GMT")
        self.addCookie("auto_id", username, expires=expires, path="/")
        self.addCookie("auto_pw", password, expires=expires, path="/")

    def finalize(self):
        """
        if you need to do something just before or just after a request is
        finished, write it here
        """
        if self._initial_session != self.yzk_session:
            self.session.redis_sync()

    @property
    def yzk_session(self):
        twisted_session = self.getSession()
        if not hasattr(twisted_session, "yuzuki_session_data"):
            twisted_session.yuzuki_session_data = dict()
        return twisted_session.yuzuki_session_data

    def log_user_in(self, user):
        self.yzk_session["login_user"] = user.uid

    def log_user_out(self):
        if "login_user" in self.yzk_session:
            del self.yzk_session["login_user"]

    def _is_user_logged_in(self):
        return "login_user" in self.yzk_session

    @property
    def user(self):
        if hasattr(self, "_user"):
            return self._user
        user_id = self.yzk_session["login_user"] \
            if self._is_user_logged_in()else None
        if not user_id:
            return None
        else:
            query = self.dbsession.query(User)\
                                  .filter(User.uid == user_id)\
                                  .options(subqueryload(User.groups))
            result = query.all()
            user = result[0]
            self._user = user
            return user

    def get_argument(self, key, default=NoArgument):
        args = self.args.get(key, None)
        if not args:
            if default == NoArgument:
                raise BadRequest()
            else:
                return default
        else:
            if len(args) == 1:
                return unicode(args[0], "utf8")
            else:
                raise BadRequest()

    def get_argument_int(self, key, default=NoArgument):
        try:
            value = self.get_argument(key, default)
            return int(value)
        except ValueError:
            if default != NoArgument:
                return default
            else:
                raise BadRequest()

    def get_path_and_query(self):
        result = self.path
        if self.args:
            result += "?"
            for i, key in enumerate(self.args):
                for j, value in enumerate(self.args[key]):
                    result += key
                    result += "="
                    result += quote(value, "")
                    if not (i == len(self.args) - 1
                            and j == len(self.args[key]) - 1):
                        result += "&"
        return result

    def processingFailed(self, reason):
        if DEBUG:
            return Request.processingFailed(self, reason)
        else:
            if issubclass(reason.type, YuzukiException):
                exc = reason.value
                self.logger.warning(reason)
                self.setResponseCode(exc.status)
                body = generate_error_message(self, exc.status, exc.message)
            else:
                self.logger.error(reason)
                self.setResponseCode(INTERNAL_SERVER_ERROR)
                body = generate_error_message(self, INTERNAL_SERVER_ERROR,
                                              u"서버 에러가 발생하였습니다.")
            if issubclass(reason.type, SQLAlchemyError):
                YuzukiRequest.dbsession.close()
                YuzukiRequest.dbsession = DatabaseHelper.session()
            body = body.encode("UTF-8")
            self.setHeader(b'content-type', b"text/html")
            self.setHeader(b'content-length', intToBytes(len(body)))
            self.write(body)
            self.finish()
            return reason

    def redirect(self, url):
        if isinstance(url, unicode):
            url = url.encode("UTF-8")
        Request.redirect(self, url)

    def getClientIP(self):
        real_ip = self.getHeader("X-Real-IP")
        if real_ip:
            return real_ip
        else:
            Request.getClientIP(self)

    def setNoCache(self):
        self.setHeader("Cache-Control", "no-cache, no-store, must-revalidate")
        self.setHeader("Pragma", "no-cache")
        self.setHeader("Expires", "0")

    def getProto(self):
        if self.requestHeaders.hasHeader("X-Forwarded-Proto"):
            return self.getHeader("X-Forwarded-Proto").lower()
        else:
            return "https" if self.isSecure() else "http"
Beispiel #7
0
 def __init__(self, name, anybody_allowed):
     dbsession = DatabaseHelper.session()
     query = dbsession.query(Board).filter(Board.name == name)
     self.board = query.one()
     self.anybody_allowed = anybody_allowed
Beispiel #8
0
 def __init__(self, name, anybody_allowed):
     dbsession = DatabaseHelper.session()
     query = dbsession.query(Board).filter(Board.name == name)
     self.board = query.one()
     self.anybody_allowed = anybody_allowed
Beispiel #9
0
# -*- coding: utf-8 -*-
from hashlib import sha256

from helper.database import DatabaseHelper
from model.board import Board
from model.group import Group
from model.user import User

DatabaseHelper.create_all()
dbsession = DatabaseHelper.session()
"""
BASIC REQUIREMENT FROM HERE
"""
anybody = Group(u"anybody")
anybody.description = "All user must be approved by being assigned to this group"
dbsession.add(anybody)

nobody = Group("nobody")
nobody.description = "nobody can be registered in this group"
dbsession.add(nobody)

admin = Group("admin")
dbsession.add(admin)

notice = Board("notice", u"공지사항", admin, anybody)
dbsession.add(notice)

free = Board("free", u"자유게시판", anybody, anybody)
dbsession.add(free)

dbsession.commit()
Beispiel #10
0
# -*- coding: utf-8 -*-
from helper.side_menu import (
    ParentMenuElement,
    BoardMenuElement,
    LinkMenuElement,
    ArbitraryMenuElement,
)
from helper.database import DatabaseHelper
from model.board import Board

dbsession = DatabaseHelper.session()
board_query = dbsession.query(Board).filter(Board.enabled).order_by(Board.repr_order.asc())
somoim_board_elements = [BoardMenuElement(b.name, False) for b in board_query.filter(Board.classification == "somoim")]
normal_board_elements = [BoardMenuElement(b.name, False) for b in board_query.filter(Board.classification == "normal")]
somoim = ParentMenuElement("somoim", u"소모임", somoim_board_elements, False)
normal = ParentMenuElement("normal", u"일반 게시판", normal_board_elements, False)
improve_button = ArbitraryMenuElement(u"""
<li>
    <a href="https://github.com/PoolC/Yuzuki/issues" target="_blank">홈페이지 개선</a>
    <iframe class="fork-button pull-right"
            src="https://ghbtns.com/github-btn.html?user=PoolC&repo=Yuzuki&type=fork&count=true"
            frameborder="0" scrolling="0" width="80px" height="20px">
    </iframe>
</li>
""", True)
SIDE_MENU = [
    LinkMenuElement("about", "/page/about", u"소개", True),
    BoardMenuElement("notice", True),
    LinkMenuElement("new", "new", u"새 글", False),
    BoardMenuElement("free", False),
    somoim,
Beispiel #11
0
# -*- coding: utf-8 -*-
from helper.database import DatabaseHelper

dbsession = DatabaseHelper.session()

from model.group import Group
from model.user import User

anybody = dbsession.query(Group).filter(Group.name == "anybody")[0]


def get_user_by_username(username):
    query = dbsession.query(User).filter(User.username == username)
    result = query.all()
    return result[0] if result else None


def get_user_by_uid(uid):
    query = dbsession.query(User).filter(User.uid == uid)
    result = query.all()
    return result[0] if result else None


def get_users_not_anybody():
    anybody_user_ids = [user.uid for user in anybody.users]
    query = dbsession.query(User).filter(User.uid.notin_(anybody_user_ids))
    return query.all()


import code