Beispiel #1
0
def init_db():
    # init status
    global status_list
    global memberships
    global category_list
    global user_list

    s_init = OrderStatus.create(name="init")
    s_confirmed = OrderStatus.create(name="confirmed")
    s_sent = OrderStatus.create(name="sent")
    s_complete = OrderStatus.create(name="complete")
    s_canceled = OrderStatus.create(name="canceled")
    status_list += [s_init, s_confirmed, s_sent, s_complete, s_canceled]

    member0 = Member.create(name="normal", point=1000, discount=100)
    member1 = Member.create(name="vip1", point=2000, discount=90)
    member2 = Member.create(name="vip2", point=3000, discount=80)
    member3 = Member.create(name="vip3", point=4000, discount=70)
    member4 = Member.create(name="vip4", point=5000, discount=60)
    member5 = Member.create(name="vip5", point=-1, discount=50)
    memberships += [member1, member2, member3, member4]

    # init user
    User.create(username="******", password="******", admin=True)
    # user = User.create(username="******", password="******", admin=True)
    # user.set_password("zhang")
    # user.save()

    Announcement.create(title="Hello", content="World")
    Announcement.create(title="Hello", content="World")
    Announcement.create(title="Hello", content="World")

    for i in range(100):
        random_user()

    # init Commodity
    cat = Category.create(name="All")
    category_list.append(cat)
    for i in range(10):
        random_cat()

    for i in range(100):
        random_co()

    for u in user_list:
        random_order(u)

    for i in range(20):
        random_feed()
Beispiel #2
0
def _update_contact(bot, update=False):
    session = db.session
    myself = _get_self(bot)
    wx_friends = bot.friends(update)
    local_ids = set([u.id for u in myself.friends.all()])
    wx_ids = set([u.puid for u in wx_friends])
    need_add = wx_ids.difference(local_ids)
    if need_add:
        for u in wx_friends:
            if u.puid in need_add:
                user = User.create(
                    id=u.puid,
                    **{field: getattr(u, field)
                       for field in USER_FIELD})
                myself.add_friend(user)
            _, path, need_update = gen_avatar_path(u.puid)
            if need_update:
                try:
                    u.get_avatar(path)
                except ResponseError:
                    print('No member: {}'.format(u.puid))
    need_del = local_ids.difference(wx_ids)
    if need_del:
        for u in myself.friends:
            if u.id in need_del:
                myself.del_friend(u)
    session.commit()
Beispiel #3
0
def _update_group(bot, update=False):
    session = db.session
    for g in bot.groups(update):
        group = session.query(Group).get(g.puid)
        if not group:
            group = Group.create(id=g.puid, nick_name=g.nick_name)
        local_ids = set([u.id for u in group.members])
        wx_ids = set([u.puid for u in g.members])
        need_add = wx_ids.difference(local_ids)
        if need_add:
            for u in g.members:
                if u.puid in need_add:
                    user = User.create(
                        id=u.puid,
                        **{field: getattr(u, field)
                           for field in USER_FIELD})
                    group.add_member(user)
                _, path, need_update = gen_avatar_path(u.puid)
                if need_update:
                    try:
                        u.get_avatar(path)
                    except (ResponseError, KeyError):
                        print('No member: {}'.format(u.puid))
        need_del = local_ids.difference(wx_ids)
        if need_del:
            for u in group.members:
                if u.id in need_del:
                    group.del_member(u)
        _, path, need_update = gen_avatar_path(g.puid)
        if need_update:
            g.get_avatar(path)
    session.commit()
Beispiel #4
0
def _get_self(bot):
    myself = db.session.query(User).get(bot.self.puid)
    if myself is None:
        myself = User.create(
            id=bot.self.puid,
            **{field: getattr(bot.self, field)
               for field in USER_FIELD})
    return myself
Beispiel #5
0
def random_user():
    global memberships
    global user_list
    username = "******".join(random_name(2))
    password = "******"
    user = User.create(username=username, password=password, membership=random.choice(memberships))
    # user.set_password("zhang")
    # user.save()
    user_list.append(user)
    return user
Beispiel #6
0
def _update_mp(bot, update=False):
    session = db.session
    myself = session.query(User).get(bot.self.puid)
    wx_mps = bot.mps()
    local_ids = set([m.id for m in myself.mps])
    wx_ids = set([u.puid for u in wx_mps])
    need_add = wx_ids.difference(local_ids)
    if need_add:
        for m in wx_mps:
            if m.puid in need_add:
                User.create(id=m.puid,
                            **{field: getattr(m, field)
                               for field in MP_FIELD})
                # wxpy还不支持公众号的头像下载
    need_del = local_ids.difference(wx_ids)
    if need_del:
        for mp in myself.mps:
            if mp.id in need_del:
                db.session.delete(mp)
    session.commit()
Beispiel #7
0
def _update_group(bot, update=False):
    session = db.session
    wx_groups = bot.groups(update)
    myself = _get_self(bot)
    wx_ids = set([g.puid for g in wx_groups])
    groups = session.query(Group).filter(Group.owner_id == bot.self.puid).all()
    local_ids = set([g.id for g in groups])

    need_del = local_ids.difference(wx_ids)
    for group in groups:
        if group.id in need_del:
            myself.groups.remove(group)
            db.session.delete(group)

    for g in wx_groups:
        group = session.query(Group).get(g.puid)
        if not group:
            group = Group.create(id=g.puid,
                                 nick_name=g.nick_name,
                                 owner_id=bot.self.puid)
        local_ids = set([u.id for u in group.members])
        wx_ids = set([u.puid for u in g.members if u.puid])
        need_add = wx_ids.difference(local_ids)
        if need_add:
            for u in g.members:
                if u.puid in need_add:
                    user = User.create(
                        id=u.puid,
                        **{field: getattr(u, field)
                           for field in USER_FIELD})
                    group.add_member(user)
                _, path, need_update = gen_avatar_path(u.puid)
                if need_update:
                    try:
                        u.get_avatar(path)
                    except (ResponseError, KeyError):
                        print('No member: {}'.format(u.puid))
        need_del = local_ids.difference(wx_ids)
        if need_del:
            for u in group.members:
                if u.id in need_del:
                    group.del_member(u)
        _, path, need_update = gen_avatar_path(g.puid)
        if need_update:
            g.get_avatar(path)
    session.commit()
Beispiel #8
0
def register():
    if request.method == 'GET':
        return render_template('core/register.html', register=True)
    else:
        username = request.form.get('username', None)
        password = request.form.get('password', None)
        passwordr = request.form.get('passwordr', None)
        if not username or not password or not passwordr:
            return render_template('core/register.html',
                    register=True, error='Can not be empty')
        if password != passwordr:
            return render_template('core/register.html',
                    register=True, error='Password Not The Same')
        if User.select().filter(username=username).exists():
            return render_template('core/register.html',
                    register=True, error='User has exist')
        user = User.create(username=username, password=password)
        #user.set_password(password)
        #user.save()
        session['logged_in'] = True
        session['user_pk'] = user.get_pk()
        session.permanent = True
        g.user = user
        return redirect(url_for('core.index'))
Beispiel #9
0
def retrieve_data(update=False):
    bot = get_bot()
    session = db.session
    # update group
    for g in bot.groups(update):
        group = session.query(Group).get(g.puid)
        if not group:
            group = Group.create(id=g.puid, nick_name=g.nick_name)
        local_ids = set([u.id for u in group.members])
        wx_ids = set([u.puid for u in g.members])
        need_add = wx_ids.difference(local_ids)
        if need_add:
            for u in g.members:
                if u.puid in need_add:
                    user = User.create(id=u.puid, **{field: getattr(u, field)
                                                     for field in USER_FIELD})
                    group.add_member(user)
                _, path, need_update = gen_avatar_path(u.puid)
                if need_update:
                    try:
                        u.get_avatar(path)
                    except (ResponseError, KeyError):
                        print('No member: {}'.format(u.puid))
        need_del = local_ids.difference(wx_ids)
        if need_del:
            for u in group.members:
                if u.id in need_del:
                    group.del_member(u)
        _, path, need_update = gen_avatar_path(g.puid)
        if need_update:
            g.get_avatar(path)
    # update mp
    myself = session.query(User).get(bot.self.puid)
    wx_mps = bot.mps()
    local_ids = set([m.id for m in myself.mps])
    wx_ids = set([u.puid for u in wx_mps])
    need_add = wx_ids.difference(local_ids)
    if need_add:
        for m in wx_mps:
            if m.puid in need_add:
                User.create(id=m.puid, **{field: getattr(m, field)
                                          for field in MP_FIELD})
                # wxpy还不支持公众号的头像下载
    need_del = local_ids.difference(wx_ids)
    if need_del:
        for mp in myself.mps:
            if mp.id in need_del:
                db.session.delete(mp)
    # update contact
    myself = session.query(User).get(bot.self.puid)
    wx_friends = bot.friends()
    local_ids = set([u.id for u in myself.friends.all()])
    wx_ids = set([u.puid for u in wx_friends])
    need_add = wx_ids.difference(local_ids)
    if need_add:
        for u in wx_friends:
            if u.puid in need_add:
                user = User.create(id=u.puid, **{field: getattr(u, field)
                                                 for field in USER_FIELD})
                myself.add_friend(user)
            _, path, need_update = gen_avatar_path(u.puid)
            if need_update:
                try:
                    u.get_avatar(path)
                except ResponseError:
                    print('No member: {}'.format(u.puid))
    need_del = local_ids.difference(wx_ids)
    if need_del:
        for u in myself.friends:
            if u.id in need_del:
                myself.del_friend(u)
    session.commit()
Beispiel #10
0
def retrieve_data(update=False):
    bot = get_bot()
    session = db.session
    # update group
    for g in bot.groups(update):
        group = session.query(Group).get(g.puid)
        if not group:
            group = Group.create(id=g.puid, nick_name=g.nick_name)
        local_ids = set([u.id for u in group.members])
        wx_ids = set([u.puid for u in g.members])
        need_add = wx_ids.difference(local_ids)
        if need_add:
            for u in g.members:
                if u.puid in need_add:
                    user = User.create(
                        id=u.puid,
                        **{field: getattr(u, field)
                           for field in USER_FIELD})
                    group.add_member(user)
                _, path, need_update = gen_avatar_path(u.puid)
                if need_update:
                    try:
                        u.get_avatar(path)
                    except (ResponseError, KeyError):
                        print('No member: {}'.format(u.puid))
        need_del = local_ids.difference(wx_ids)
        if need_del:
            for u in group.members:
                if u.id in need_del:
                    group.del_member(u)
        _, path, need_update = gen_avatar_path(g.puid)
        if need_update:
            g.get_avatar(path)
    # update mp
    myself = session.query(User).get(bot.self.puid)
    wx_mps = bot.mps()
    local_ids = set([m.id for m in myself.mps])
    wx_ids = set([u.puid for u in wx_mps])
    need_add = wx_ids.difference(local_ids)
    if need_add:
        for m in wx_mps:
            if m.puid in need_add:
                User.create(id=m.puid,
                            **{field: getattr(m, field)
                               for field in MP_FIELD})
                # wxpy还不支持公众号的头像下载
    need_del = local_ids.difference(wx_ids)
    if need_del:
        for mp in myself.mps:
            if mp.id in need_del:
                db.session.delete(mp)
    # update contact
    myself = session.query(User).get(bot.self.puid)
    wx_friends = bot.friends()
    local_ids = set([u.id for u in myself.friends.all()])
    wx_ids = set([u.puid for u in wx_friends])
    need_add = wx_ids.difference(local_ids)
    if need_add:
        for u in wx_friends:
            if u.puid in need_add:
                user = User.create(
                    id=u.puid,
                    **{field: getattr(u, field)
                       for field in USER_FIELD})
                myself.add_friend(user)
            _, path, need_update = gen_avatar_path(u.puid)
            if need_update:
                try:
                    u.get_avatar(path)
                except ResponseError:
                    print('No member: {}'.format(u.puid))
    need_del = local_ids.difference(wx_ids)
    if need_del:
        for u in myself.friends:
            if u.id in need_del:
                myself.del_friend(u)
    session.commit()