示例#1
0
def update_distance(display, cols):
    if not validate(cols):
        return

    condition = cond({'display': display})
    rid = store.value('runner', 'id', condition=condition)

    distance = cols[0]
    record = None
    #撤回功能
    if (distance == 0):
        delete_record(rid)
    else:
        record = update_record(rid, distance)

    total = None if len(cols) < 2 else cols[1]
    if total:
        update_total(rid, total)
    else:
        if not none_or_empty(record):  #如果已经打卡,则进行纠正
            old = record[0]
            new = record[1]
            distance = new - old
        update_total(rid, distance, add=True)

    plan = None if len(cols) < 3 else cols[2]
    if plan:
        update_plan(rid, plan)
示例#2
0
def update(table, field, value, condition):
    if store.exists(table, condition):
        old = store.value(table, field, condition=condition)
        if old != value:
            store.update(table, [field], [value], condition=condition)
            logger.debug('update %s: %s=%s' % (table, field, value))
        return (old, value)
    return None
示例#3
0
def delete_record(rid):
    table = 'record'
    day = date.date()
    condition = cond({'rid': rid, 'date': day})

    old = store.value(table, 'distance', condition=condition)
    if not none_or_empty(old):
        update_total(rid, old * (-1), add=True)
        store.delete(table, condition=condition)
示例#4
0
def update_display(nick, display, remark=None):
    table = 'runner'
    if display == None or display == '':
        display = nick
    name = display

    condition = cond({'nick': nick})
    if not update_or_insert(table, ['nick', 'name', 'remark', 'display'],
                            [nick, name, remark, display], condition):
        id = store.value(table, 'id', condition=condition)
        update_plan(id, 60)
示例#5
0
def update_total(rid, total, add=False):
    table = 'plan'
    year = date.year()
    month = date.month()
    condition = cond({'rid': rid, 'year': year, 'month': month})

    if add:
        old = store.value(table, 'total', condition=condition)
        if old:
            total = total + old

    return update_or_insert(table, ['rid', 'year', 'month', 'total'],
                            [rid, year, month, total], condition)
示例#6
0
def motto():
    if not none_or_empty(today_motto):
        return '{} - \n{}'.format('齐队', today_motto)

    count = store.count('motto')

    if count == None or count == 0:
        return mottos[random.randint(0, len(mottos) - 1)]

    max_id = store.value('motto', 'max(id)')
    random_motto = store.query('motto', ['content', 'rid'],
                               cond({'id': random.randint(1, max_id)}))

    if random_motto == None:
        return mottos[random.randint(0, len(mottos) - 1)]

    random_motto = random_motto[0]
    content = random_motto[0]
    rid = random_motto[1]
    name = store.value('runner', 'name', cond({'id': rid}))

    return '{} - \n{}'.format(name, content)
示例#7
0
def rank(order='time', reverse=False):
    table_runner = 'runner'
    table_record = 'record'
    table_plan = 'plan'

    day = date.date()
    year = date.year()
    month = date.month()

    condition = cond({'date': day})

    records = store.query(table_record, ['rid', 'distance', 'time'],
                          condition=condition)
    marks = []
    for record in records:
        id = record[0]
        distance = record[1]
        time = record[2]
        name = store.value(table_runner, 'name', condition=cond({'id': id}))
        total = 0
        plan = 0
        plans = store.query(table_plan, ['plan', 'total'],
                            condition=cond({
                                'rid': id,
                                'year': year,
                                'month': month
                            }))
        if plans and len(plans) > 0:
            plans = plans[0]

        if plans:
            plan = plans[0]
            total = plans[1]

        mark = {
            'name': name,
            'distance': distance,
            'total': total,
            'plan': plan,
            'time': date.toTime(time)
        }
        marks.append(mark)

    marks.sort(reverse=reverse, key=lambda mark: mark[order])
    return marks