当前位置: 首页>>代码示例>>Python>>正文


Python Session.add方法代码示例

本文整理汇总了Python中TrackerRestApi.Session.add方法的典型用法代码示例。如果您正苦于以下问题:Python Session.add方法的具体用法?Python Session.add怎么用?Python Session.add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TrackerRestApi.Session的用法示例。


在下文中一共展示了Session.add方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: getCode

# 需要导入模块: from TrackerRestApi import Session [as 别名]
# 或者: from TrackerRestApi.Session import add [as 别名]
def getCode(number):

    session = Session()

    sms = SmsEpochta()
    code = ''.join(random.choice(string.digits) for _ in range(app.config.get('SMS_CODE_LEN')))

    u = session.query(TrUser).filter(TrUser.login == number).first()

    if u is None:
        u = TrUser(login=number, auth_code=code, active='N')
        session.add(u)
    else:
        setattr(u, 'auth_code', code)
        setattr(u, 'active', 'N')
        session.merge(u)

    try:
        session.commit()
        session.refresh(u)
    except:
        session.rollback()
        raise ServerError("Can't generate new code.")
    finally:
        session.close()

    sms.send(number, code)

    return u.id
开发者ID:toxxin,项目名称:TrackerRestApi,代码行数:31,代码来源:auth.py

示例2: addComment

# 需要导入模块: from TrackerRestApi import Session [as 别名]
# 或者: from TrackerRestApi.Session import add [as 别名]
def addComment(user_id, group_id, message):

    session = Session()

    uid = int(current_user.get_id()) if app.config.get('LOGIN_DISABLED') is False else user_id

    ug = session.query(association_table_user_group).filter(association_table_user_group.user_id == uid).\
                                                    filter(association_table_user_group.group_id == group_id).first()
    if ug is None:
        session.close()
        raise ServerError("Group doesn't exist.")

    gm = TrGroupComment(message, ug.id)

    try:
        session.add(gm)
        session.commit()
        session.refresh(gm)
    except:
        session.rollback()
        raise ServerError("Can't add message.")
    finally:
        session.close()

    return True
开发者ID:toxxin,项目名称:TrackerRestApi,代码行数:27,代码来源:group.py

示例3: addFavFeed

# 需要导入模块: from TrackerRestApi import Session [as 别名]
# 或者: from TrackerRestApi.Session import add [as 别名]
def addFavFeed(user_id, feed_id, id):

    session = Session()

    uid = int(current_user.get_id()) if app.config.get('LOGIN_DISABLED') is False else user_id

    u_f = session.query(association_table_user_feed).filter_by(user_id=uid).filter_by(feed_id=feed_id).first()
    if u_f is None:
        session.close()
        raise ServerError("User or feed doesn't exist.")

    uf_fav = session.query(TrUserFeedFav).filter_by(uf_id=u_f.id).filter_by(feed_news_id=id).first()

    if uf_fav is None:
        new_fav = TrUserFeedFav(uf_id=u_f.id, feed_news_id=id)

        try:
            session.add(new_fav)
            session.commit()
        except:
            session.rollback()
            raise ServerError("Can't add item to favorites.")
        finally:
            session.close()
    else:
        session.close()

    return True
开发者ID:toxxin,项目名称:TrackerRestApi,代码行数:30,代码来源:feed.py

示例4: login

# 需要导入模块: from TrackerRestApi import Session [as 别名]
# 或者: from TrackerRestApi.Session import add [as 别名]
def login(id, code, platform, hw_id, reg_id):

    if platform not in ["A", "a", "I", "i"]:
        raise ServerError("Incorrect platform.")

    s = Session()

    u = s.query(TrUser).filter(TrUser.id == id).first()

    if u is None:
        s.close()
        raise ServerError("User doesn't exist.")

    #TODO:: expired date of code
    exp = int((datetime.datetime.now() - u.last_modified).total_seconds())
    print exp
    if exp > app.config['SMS_CODE_LIFETIME']:
        s.close()
        raise ServerError("Invalid code.")

    if u.auth_code == code:
        u.authenticated = True
        s.merge(u)
        s.commit()
        login_user(u)

    """Save token for push notification"""
    pt = s.query(TrPushToken).filter(TrPushToken.platform == platform.upper()).\
                                filter(TrPushToken.hardware_id == hw_id).first()
    if pt is None:
        pt = TrPushToken(hw_id, platform.upper(), reg_id, id)
        s.add(pt)
        s.commit()
    else:
        setattr(pt, 'hardware_id', hw_id)
        setattr(pt, 'token', reg_id)
        setattr(pt, 'user_id', id)
        s.merge(pt)
        s.commit()

    s.close()

    return True
开发者ID:toxxin,项目名称:TrackerRestApi,代码行数:45,代码来源:auth.py

示例5: addPlace

# 需要导入模块: from TrackerRestApi import Session [as 别名]
# 或者: from TrackerRestApi.Session import add [as 别名]
def addPlace(user_id, title, longitude, latitude, type, desc):

    session = Session()
    
    uid = int(current_user.get_id()) if app.config.get('LOGIN_DISABLED') is False else user_id

    p = TrPlace(user_id=uid, title=title, longitude=float(longitude), latitude=float(latitude), type=type, desc=desc)

    try:
        session.add(p)
        session.commit()
        session.refresh(p)
    except:
        session.rollback()
        raise ServerError("Can't add place.")
    finally:
        session.close()

    return p.id
开发者ID:toxxin,项目名称:TrackerRestApi,代码行数:21,代码来源:place.py

示例6: addGroup

# 需要导入模块: from TrackerRestApi import Session [as 别名]
# 或者: from TrackerRestApi.Session import add [as 别名]
def addGroup(user_id, title, desc, invitation, meeting, help):

    session = Session()

    uid = int(current_user.get_id()) if app.config.get('LOGIN_DISABLED') is False else user_id

    g = TrGroup(title=title, desc=desc, invitation=invitation, meeting=meeting, help=help, user_id=uid)

    try:
        session.add(g)
        session.commit()
        session.refresh(g)
    except:
        session.rollback()
        raise ServerError("Can't add group.")
    finally:
        session.close()

    return g.id
开发者ID:toxxin,项目名称:TrackerRestApi,代码行数:21,代码来源:group.py

示例7: addHelp

# 需要导入模块: from TrackerRestApi import Session [as 别名]
# 或者: from TrackerRestApi.Session import add [as 别名]
def addHelp(user_id,vehicle_id,message,lat_log, phone):

    session = Session()

    v = session.query(TrVehicle).filter(TrVehicle.id == vehicle_id).filter(TrVehicle.user_id == user_id).first()

    if v is None:
        session.close()
        raise ServerError("Vehicle doesn't exist.")

    if v.help is None:
        """Add new message"""
        message = TrHelp(vehicle_id=v.id, message=message, lat_log=lat_log)

        try:
            session.add(message)
            session.commit()
        except:
            session.rollback()
            raise ServerError("Can't add help message.")
        finally:
            session.close()

    else:
        """Update existing message"""
        help = session.query(TrHelp).filter(TrHelp.vehicle_id == v.id).first()

        setattr(help, 'message', message)
        setattr(help, 'lat_log', lat_log)
        setattr(help, 'creation_date', func.now())

        try:
            session.merge(help)
            session.commit()
        except:
            session.rollback()
            raise ServerError("Can't update")
        finally:
            session.close()

    return True
开发者ID:toxxin,项目名称:TrackerRestApi,代码行数:43,代码来源:help.py

示例8: addGroupMeeting

# 需要导入模块: from TrackerRestApi import Session [as 别名]
# 或者: from TrackerRestApi.Session import add [as 别名]
def addGroupMeeting(user_id, group_id, title, lat_log, time):

    session = Session()

    uid = int(current_user.get_id()) if app.config.get('LOGIN_DISABLED') is False else user_id

    g = session.query(TrGroup).filter(TrGroup.user_id == uid).filter(TrGroup.id == group_id).first()
    if g is None:
        session.close()
        raise ServerError("Group doesn't exist.")

    m = TrGroupMeeting(group_id=group_id, title=title, latitude=0, longitude=0, time=datetime.datetime.fromtimestamp(time))

    try:
        session.add(m)
        session.commit()
        session.refresh(m)
    except:
        session.rollback()
        raise ServerError("Can't add meeting.")
    finally:
        session.close()

    return m.id
开发者ID:toxxin,项目名称:TrackerRestApi,代码行数:26,代码来源:group.py

示例9: addDGeo

# 需要导入模块: from TrackerRestApi import Session [as 别名]
# 或者: from TrackerRestApi.Session import add [as 别名]
def addDGeo(user_id=null, device_id=null, **kwargs):

    # 1. check that user_id has device_id
    # 2. check that all params from list
    # 3. check mandatory params

    session = Session()

    if user_id == null or device_id == null:
        session.close()
        raise InvalidParamsError("user_id or device_id is not specified.")

    """check if user exist and has such device"""
    dev = session.query(TrDevice).filter(TrDevice.device_ID == device_id) \
                                    .filter(TrDevice.device_userID == user_id).first()
    if dev is None:
        session.close()
        raise ServerError("Incorrect user or device.")

    """check all params"""
    if not isAllIncluded(kwargs.keys(), geo_mandatory_params + geo_option_params):
        session.close()
        raise InvalidParamsError("Incorrect parameters.")

    """check mandatory params"""
    if not isAllIncluded(geo_mandatory_params, kwargs.keys()):
        session.close()
        raise InvalidParamsError("Incorrect mandatory parameters.")

    # check special params
    if kwargs['shape'] == GEO_SHAPE_RANDOM:
        pass
        # # if checkParams(kwargs.keys(), geo_test_params_must_for_rand) == True:
        # S1 = set(geo_test_params_must_for_rand)
        # S2 = set(kwargs.keys())
        #
        # if len(S1.intersection(S2)) != len(S1):
        #     session.close()
        #     raise ServerError("Incorrect parameters")
        # else:
        #     # add geozone!

    elif kwargs['shape'] == GEO_SHAPE_CIRCUS or kwargs['shape'] == GEO_SHAPE_SQUARE:

        geo = TrDGeozone(device_id=device_id,
                         name=kwargs['name'],
                         shape=kwargs['shape'],
                         center=kwargs['center'],
                         radius=kwargs['radius'],
                         color=kwargs['color'] if 'color' in kwargs else "#ff00ff",
                         state=kwargs['state'] if 'state' in kwargs else True)

        try:
            session.add(geo)
            session.commit()
        except:
            session.rollback()
            raise ServerError("Can't add geo.")
        finally:
            session.close()

        return True

    else:
        session.close()
        raise ServerError("Incorrect value for shape")
开发者ID:toxxin,项目名称:TrackerRestApi,代码行数:68,代码来源:geo.py


注:本文中的TrackerRestApi.Session.add方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。