예제 #1
0
    def register_google(cls, response):
        access_token = response.get(u"access_token", None)
        if not access_token:
            return {u"ok": False, u"msg": u"Google 로그인에 실패했습니다."}

        r = GoogleAPI(access_token=access_token).fetch()

        if r.status != 200:
            return {u"ok": False, u"msg": u"개인 정보를 불러오는 데 실패했습니다."}

        data = r.data
        user_email = data.get("email")
        user_data = db.query(User).filter_by(email=user_email).first()
        if user_data:
            return {u"ok": True, u"user": user_data, u"created": False}

        user_id = user_email.split("@")[0]
        user_pw = md5("%s%s%s" % ("go", user_id, "google")).hexdigest()[:20]
        user_pic = data.get("picture")

        created_user = User(id_str=user_email.split("@")[0], email=user_email, password=user_pw, picture=user_pic)
        db.add(created_user)
        db.commit()

        created_feed = NewsFeed(
            news_type=(3,), data={u"user": {u"picture": user_pic, u"id_int": created_user.id_int, u"id_str": user_id}}
        )

        db.add(created_feed)
        db.commit()

        return {u"ok": True, u"user": created_user, u"created": True}
    def create(cls, **kwargs):
        """
        Parameters:
        news_type: type of news
                    * 1: archive
                    * 2: add media to playlist

        user: user object (models.User)
        media: media object (models.Media)
        playlist: playlist object (models.Playlist)
        """
        news_type = kwargs.get(u'news_type', 0)

        user = kwargs.get(u'user', g.user)
        if not user:
            return {u'ok': False, u'msg': u'login required.'}

        media = kwargs.get(u'media', None)
        if not media:
            return {u'ok': False, u'msg': u'media was required.'}

        playlist = kwargs.get(u'playlist', None)
        if not playlist:
            return {u'ok': False, u'msg': u'playlist was required.'}

        param = {
            u'user': {
                u'id_int': user.id_int,
                u'id_str': user.id_str,
                u'picture': user.picture,
            },
            u'media': {
                u'id_int': media.id_int,
                u'title': media.title,
                u'duration': media.duration,
                u'thumbnail': media.thumbnail,
            },
            u'playlist': {
                u'id_int': playlist.id_int,
                u'title': playlist.title,
            },
        }

        created_news_feed = NewsFeed(news_type=(news_type, ),
                                     data=param)
        db.add(created_news_feed)
        db.commit()

        return {u'ok': True}
def drop_db():
    """
    Drop all of tables you defined via models
    """
    print 'Drop DB'
    from yamp.app import db, engine
    from yamp.models import Base, __all__ as model_files
    from importlib import import_module
    for model_file in model_files:
        import_module('yamp.models.' + model_file, __name__)
        print '* Model "%s" loaded.' % model_file
    Base.metadata.drop_all(bind=engine)
    db.commit()
    print '... All tables deleted!'
    raise SystemExit
def init_db():
    """
    Initialize tables you defined via models
    """
    print 'Initialize DB'
    from yamp.app import db, engine
    from yamp.models import Base, __all__ as model_files
    from importlib import import_module
    for model_file in model_files:
        import_module('yamp.models.' + model_file, __name__)
        print '* Model "%s" loaded.' % model_file
    Base.metadata.create_all(bind=engine)
    db.commit()
    print '... All tables created!'
    raise SystemExit
    def create(cls, media_info):
        """
        Parameters:
        media_info: tuple like this (<int>media_type, <str>id_str)
        """
        created_media = None
        parsed_data = cls.route_parser(media_info)
        if parsed_data.get(u'ok'):
            info = parsed_data.get(u'info')
            created_media = Media(id_str=media_info[1],
                                  media_type=media_info,
                                  data=info)
            db.add(created_media)
            db.commit()

        return created_media
    def add_media(cls, playlist, media):
        if isinstance(playlist, int):
            playlist = db.query(Playlist).get(playlist)

        if isinstance(media, int):
            media = db.query(Media).get(media)

        if None in [playlist, media]:
            return False

        playlist.media_list.append(media)
        db.add(playlist)
        db.commit()

        NewsFeedController.create(news_type=1 if playlist.default else 2,
                                  media=media,
                                  playlist=playlist)

        return True
    def create(cls, **kwargs):
        """
        Parameters:
        user: owner of playlist (default: connected_user)
        title: title of playlist (default: None, *required)
        limit: embargo of media in playlist (default: 20)
        """
        user = kwargs.get('user', g.user)
        if not user:
            return {u'ok': False, u'msg': u'Need to sign in.'}

        title = kwargs.get('title', None)
        if not title:
            return {u'ok': False, u'msg': u'title was required'}

        limit = kwargs.get('limit', 20)
        default = kwargs.get('default', False)

        created_playlist = Playlist(title=title, owner=user, limit=limit, default=default)
        db.add(created_playlist)
        db.commit()

        return {u'ok': True, u'playlist': created_playlist}