Ejemplo n.º 1
0
def add_event(data): 
  if KEY.ID not in data or KEY.TYPE not in data:
    return -1
  if data[KEY.TYPE] == 1 and KEY.MAX_PEOPLE not in data:
    return -1

  sql = "insert into event (launcher, type, time) values (%d, %d, now())"
  help_sql = "insert into event (launcher, type, time, help_max) values (%d, %d, now(), %d)"
  event_id = -1
  try:
    if data[KEY.TYPE] == 1:
      event_id = dbhelper.insert(help_sql%(data[KEY.ID], data[KEY.TYPE], data[KEY.MAX_PEOPLE]))
    else:
      event_id = dbhelper.insert(sql%(data[KEY.ID], data[KEY.TYPE]))
    #event_id = dbhelper.insert(sql%(data[KEY.ID], data[KEY.TYPE]))
    print "From add_event_handler: database event id: %d"%event_id
    if event_id > 0:
      data[KEY.EVENT_ID] = event_id
      update_event(data)
      # consume love_coins when adding events
      if data[KEY.TYPE] == 0:
        minus = "update loving_bank set coin = coin-1 where user_id = %d"
      elif data[KEY.TYPE] == 1:
        minus = "update loving_bank set coin = coin-2 where user_id = %d"
      elif data[KEY.TYPE] == 2:
        minus = "update loving_bank set coin = coin-3 where user_id = %d"
      dbhelper.execute(minus%(data[KEY.ID]))
    return event_id
  except:
    return -1
Ejemplo n.º 2
0
def user_event_manage(data):
    if KEY.ID not in data or KEY.EVENT_ID not in data:
        return False
    if KEY.OPERATION not in data:
        return True
    if data[KEY.OPERATION] < 0 or data[KEY.OPERATION] > 2:
        return False
    sql = "select launcher from event where id = %d"
    launcher_id = None
    try:
        sql_result = dbhelper.execute_fetchone(sql % (data[KEY.EVENT_ID]))
        if sql_result is not None:
            launcher_id = sql_result[0]
    except:
        pass
    if launcher_id is None:
        return False
    if data[KEY.OPERATION] == 0:
        sql = "delete from support_relation where event_id = %d and supporter = %d" % (
            data[KEY.EVENT_ID], data[KEY.ID])
    else:
        sql = "replace into support_relation (event_id, supportee, supporter, type, time) values (%d, %d, %d, %d, now())" % (
            data[KEY.EVENT_ID], launcher_id, data[KEY.ID], data[KEY.OPERATION])
    try:
        dbhelper.execute(sql)
    except:
        return False

    #
    # trust and reputation compute here.
    #
    return True
Ejemplo n.º 3
0
def user_event_manage(data):
  if KEY.ID not in data or KEY.EVENT_ID not in data:
    return False
  if KEY.OPERATION not in data:
    return True
  if data[KEY.OPERATION] < 0 or data[KEY.OPERATION] > 2:
    return False
  sql = "select launcher from event where id = %d"
  launcher_id = None
  try:
    sql_result = dbhelper.execute_fetchone(sql%(data[KEY.EVENT_ID]))
    if sql_result is not None:
      launcher_id = sql_result[0]
  except:
    pass
  if launcher_id is None:
    return False
  if data[KEY.OPERATION] == 0:
    sql = "delete from support_relation where event_id = %d and supporter = %d"%(data[KEY.EVENT_ID], data[KEY.ID])
  else:
    sql = "replace into support_relation (event_id, supportee, supporter, type, time) values (%d, %d, %d, %d, now())"%(data[KEY.EVENT_ID], launcher_id, data[KEY.ID], data[KEY.OPERATION])
  try:
    dbhelper.execute(sql)
  except:
    return False

  #
  # trust and reputation compute here.
  #
  return True
Ejemplo n.º 4
0
def update_loving_bank(data):
  if KEY.ID not in data and KEY.OPERATION not in data:
    return False
  if KEY.LOVE_COIN not in data and KEY.SCORE not in data:
    return False

  user = {}
  user[KEY.USER_ID] = data[KEY.ID]
  bank_info = get_user_loving_bank(user)
  if bank_info is None:
    return False

  exchange_rate = 1
  if data[KEY.OPERATION] == 0:
    update_love_coin = bank_info[KEY.LOVE_COIN] + data[KEY.LOVE_COIN]
    update_score = bank_info[KEY.SCORE] + data[KEY.SCORE]
  elif data[KEY.OPERATION] == 1:
    update_love_coin = bank_info[KEY.LOVE_COIN] - data[KEY.LOVE_COIN]
    update_score = bank_info[KEY.SCORE] - data[KEY.SCORE]
  elif data[KEY.OPERATION] == 2:
    update_love_coin = bank_info[KEY.LOVE_COIN] + data[KEY.SCORE] * exchange_rate
    update_score = bank_info[KEY.SCORE] - data[KEY.SCORE]
  else:
    return False

  if update_love_coin < 0 or update_score < 0:
    return False

  sql = "update loving_bank set love_coin = %d, score_rank = %d where userid = %d"
  try:
    dbhelper.execute(sql%(update_love_coin, update_score, data[KEY.ID]))
    return True
  except:
    return False
Ejemplo n.º 5
0
def remove_comment(data):
    if KEY.ID not in data or KEY.EVENT_ID not in data or KEY.COMMENT_ID not in data:
        return False
    sql = "delete from comment where id = %d and event_id = %d and author = %d"
    try:
        dbhelper.execute(sql % (data[KEY.COMMENT_ID], data[KEY.EVENT_ID], data[KEY.ID]))
        return True
    except:
        return False
Ejemplo n.º 6
0
def remove_question(data):
  if KEY._QUESTION_ID not in data :
    return False
  sql = "delete from question where question_id = %d"
  try:
    dbhelper.execute(sql%(data[KEY.QUESTION_ID]))
    return True
  except:
    return False
Ejemplo n.º 7
0
def remove_event(data):
    if KEY.ID not in data or KEY.EVENT_ID not in data:
        return False
    sql = "delete from event where id = %d and launcher = %d"
    try:
        dbhelper.execute(sql % (data[KEY.EVENT_ID], data[KEY.ID]))
        return True
    except:
        return False
Ejemplo n.º 8
0
def remove_event(data):
  if KEY.ID not in data or KEY.EVENT_ID not in data:
    return False
  sql = "delete from event where id = %d and launcher = %d"
  try:
    dbhelper.execute(sql%(data[KEY.EVENT_ID], data[KEY.ID]))
    return True
  except:
    return False
Ejemplo n.º 9
0
def update_token(data):
  if KEY.ID not in data or KEY.TOKEN not in data:
    return False

  sql = "update account set push_token = '%s' where id = %d"
  try:
    dbhelper.execute(sql%(data[KEY.TOKEN], data[KEY.ID]))
    return True
  except:
    return False
Ejemplo n.º 10
0
def remove_comment(data):
    if KEY.ID not in data or KEY.EVENT_ID not in data or KEY.COMMENT_ID not in data:
        return False
    sql = "delete from comment where id = %d and event_id = %d and author = %d"
    try:
        dbhelper.execute(
            sql % (data[KEY.COMMENT_ID], data[KEY.EVENT_ID], data[KEY.ID]))
        return True
    except:
        return False
Ejemplo n.º 11
0
def evaluate_user(data):
  if KEY.ID not in data or KEY.USER_ACCOUNT not in data or KEY.EVENT_ID not in data:
    return False
  if KEY.ATTITUDE not in data or KEY.SKILL not in data or KEY.SATISFY not in data:
    return False
  
  # get a new average reputation value
  value = 0.0
  value = data[KEY.ATTITUDE] + data[KEY.SKILL] + data[KEY.SATISFY]
  value /= 3.0

  # get type of the event
  get_type = "select type from event where id = %d"%data[KEY.EVENT_ID]
  type_result = dbhelper.execute_fetchone(get_type)
  event_type = type_result[0]

  result = 0
  # split user_account string into long array
  user_account = data[KEY.USER_ACCOUNT].split(",")
  # update each user's reputation in list
  for each_account in user_account:
    if (long(each_account) != 0):
      # get id by account
      find_user_id = "select id from account where account = %d"
      user_id = dbhelper.execute_fetchone(find_user_id%(long(each_account)))
      print "From evaluate_handler: %ld" % long(each_account)

      # update a record in table 'evaluation'
      sql = "replace into evaluation (event_id, `from`, `to`, value, time, comment) values (%d, %d, %d, %f, now(), '%s')"
      # get an updated reputation value from table 'evaluation'
      get_final_value = "select AVG(value) from evaluation where `to` = %d"
      # update evaluatee's reputation in table 'user'
      update_repu = "update user set reputation = %d where id = %d"
      # update love coins in table 'loving_bank'
      if event_type == 0:
        update_coin = "update loving_bank set coin = coin+1 where id = %d"
      if event_type == 1:
        update_coin = "update loving_bank set coin = coin+2 where id = %d"
      if event_type == 2:
        update_coin = "update loving_bank set coin = coin+3 where id = %d"

      try:
        a = dbhelper.execute(sql%(data[KEY.EVENT_ID], data[KEY.ID], user_id[0], value, data[KEY.ASSESS]))
        final_value = dbhelper.execute_fetchone(get_final_value%(user_id[0]))
        dbhelper.execute(update_repu%(final_value[0], user_id[0]))
        dbhelper.execute(update_coin%(data[KEY.EVENT_ID]))
        result += 1
      except:
        pass

  return result
Ejemplo n.º 12
0
def update_location(data):
  if KEY.LONGITUDE not in data or KEY.LATITUDE not in data or KEY.ID not in data:
    return False

  # if there is a event id, also update a helper's location
  if KEY.EVENT_ID in data:
    help_sql = "update support_relation set longitude = %f, latitude = %f where event_id = %d and supporter = %d"
  # update a user's location, in table 'user'
  sql = "update user set longitude = %f, latitude = %f where id = %d"
  try:
    dbhelper.execute(sql%(data[KEY.LONGITUDE], data[KEY.LATITUDE], data[KEY.ID]))
    if KEY.EVENT_ID in data:
      dbhelper.execute(help_sql%(data[KEY.LONGITUDE], data[KEY.LATITUDE], data[KEY.EVENT_ID], data[KEY.ID]))
    return True
  except:
    return False
Ejemplo n.º 13
0
def update_db_avatar(data):
  if KEY.ID not in data or 'filepath' not in data:
    return False
  sql = "update user set avatar = '%s' where id = %d"
  try:
    result = dbhelper.execute(sql%(data['filepath'], data[KEY.ID]))
    print "From database - update db avatar: update operation result: %d"%result
    return True
  except:
    return False
Ejemplo n.º 14
0
def update_account(data):
  if KEY.ID in data and KEY.CHAT_TOKEN in data:
    sql = "update account set chat_token = '%s' where id = %d"
    try:
      if dbhelper.execute(sql%(data[KEY.CHAT_TOKEN], data[KEY.ID])) > 0:
        return True
    except:
      return False
  else:
    return False
Ejemplo n.º 15
0
def update_account(data):
    if KEY.ID in data and KEY.CHAT_TOKEN in data:
        sql = "update account set chat_token = '%s' where id = %d"
        try:
            if dbhelper.execute(sql %
                                (data[KEY.CHAT_TOKEN], data[KEY.ID])) > 0:
                return True
        except:
            return False
    else:
        return False
Ejemplo n.º 16
0
def user_event_manage(data):
  if KEY.ID not in data or KEY.EVENT_ID not in data:
    return False
  if KEY.OPERATION not in data:
    return True
  if data[KEY.OPERATION] < 0 or data[KEY.OPERATION] > 2:
    return False
  sql = "select launcher from event where id = %d"
  launcher_id = None
  try:
    sql_result = dbhelper.execute_fetchone(sql%(data[KEY.EVENT_ID]))
    if sql_result is not None:
      launcher_id = sql_result[0]
  except:
    pass

  if launcher_id is None:
    return False
  if data[KEY.OPERATION] == 0:
    sql = "delete from support_relation where event_id = %d and supporter = %d"%(data[KEY.EVENT_ID], data[KEY.ID])
    update_event_sql = "update event set support_number = support_number-1 where id = %d"%(data[KEY.EVENT_ID])
  else:
    sql = "insert into support_relation (event_id, supportee, supporter, type, time) values (%d, %d, %d, %d, now())"%(data[KEY.EVENT_ID], launcher_id, data[KEY.ID], data[KEY.OPERATION])
    update_event_sql = "update event set support_number = support_number+1 where id = %d"%(data[KEY.EVENT_ID])
  count_supporter = "select count(*) from support_relation where event_id = %d and supportee = %d"%(data[KEY.EVENT_ID], launcher_id)
  help_max = "select help_max from event where id = %d and launcher = %d"%(data[KEY.EVENT_ID], launcher_id)
  state_sql = "select state from event where id = %d and launcher = %d"%(data[KEY.EVENT_ID], launcher_id)
  update_state = "update event set state = %d where id = %d"

  try:
    # update support_relation
    p = dbhelper.execute(sql)
    print "From user_event_manage_handler: attend result: %d"%p
    if p == 0:
      return False
    # update event information
    dbhelper.execute(update_event_sql)

    # Check if people are enough for helping
    supporters_result = dbhelper.execute_fetchone(count_supporter)
    help_max_result = dbhelper.execute_fetchone(help_max)
    state_result = dbhelper.execute_fetchone(state_sql)
    if supporters_result[0] == help_max_result[0] and state_result[0] == 0:
      # update event state to "supporter enough"
      dbhelper.execute(update_state%(2, data[KEY.EVENT_ID]))
      print "From db update help event:", supporters_result[0], help_max_result[0]
    elif supporters_result[0] < help_max_result[0] and state_result[0] == 2:
      # update event state to "ing"
      dbhelper.execute(update_state%(0, data[KEY.EVENT_ID]))
  except:
    return False

  #
  # trust and reputation compute here.
  #
  return True
Ejemplo n.º 17
0
def modify_password(data):
    if KEY.ACCOUNT not in data or KEY.PASSWORD not in data:
        return False
    sql = "update account set password = '******' where account = '%s'"
    try:
        n = dbhelper.execute(sql % (data[KEY.PASSWORD], data[KEY.ACCOUNT]))
        if n > 0:
            return True
        else:
            return False
    except:
        return False
Ejemplo n.º 18
0
def remove_static_relation(data):
    if KEY.ID not in data or KEY.USER_ID not in data:
        return False
    sql = "delete from static_relation where user_a = %d and user_b = %d"
    try:
        n = dbhelper.execute(sql % (data[KEY.ID], data[KEY.USER_ID]))
        if n > 0:
            return True
        else:
            return False
    except:
        return False
Ejemplo n.º 19
0
def evaluate_user(data):
  if KEY.ID not in data or KEY.USER_ID not in data or KEY.EVENT_ID not in data:
    return False
  if KEY.VALUE not in data:
    return False

  value_list = ast.literal_eval(data[KEY.VALUE])
  value = 0.0
  for each_value in value_list:
    value += each_value
  list_len = len(value_list)
  if list_len == 0:
    list_len = 1
  value /= list_len

  sql = "replace into evaluation (event_id, from, to, value, time) values (%d, %d, %d, %f, now())"
  try:
    dbhelper.execute(sql%(data[KEY.EVENT_ID], data[KEY.ID], data[KEY.USER_ID], value))
    return True
  except:
    return False
Ejemplo n.º 20
0
def modify_password(data):
  if KEY.ACCOUNT not in data or KEY.PASSWORD not in data:
    return False
  sql = "update account set password = '******' where account = '%s'" 
  try:
    n = dbhelper.execute(sql%(data[KEY.PASSWORD], data[KEY.ACCOUNT]))
    if n > 0:
      return True
    else:
      return False
  except:
      return False
Ejemplo n.º 21
0
def add_static_relation(data):
  if KEY.ID not in data or KEY.USER_ID not in data or KEY.TYPE not in data:
    return False
  sql = "replace into static_relation (user_a, user_b, type, time) values (%d, %d, %d, now())"
  try:
    n = dbhelper.execute(sql%(data[KEY.ID], data[KEY.USER_ID], data[KEY.TYPE]))
    if n > 0:
      return True
    else:
      return False
  except:
    return False
Ejemplo n.º 22
0
def delete_friends(data):
  if KEY.HOST_NAME not in data or KEY.GUEST_NAME not in data:
    return False
  sql = "delete from friendlist where host_name = %d and guest_name = %d"
  try:
    n = dbhelper.execute(sql%(data[KEY.HOST_NAME], data[KEY.GUEST_NAME]))
    if n > 0:
      return True
    else:
      return False
  except:
    return False
Ejemplo n.º 23
0
def add_friends(data): 
  if KEY.HOST_NAME not in data or KEY.GUEST_NAME not in data:
    return False
  sql = "insert into friendlist (host_name, guest_name) values (%s, %s)"
  try:
    n = dbhelper.execute(sql%(data[KEY.HOST_NAME], data[KEY.GUEST_NAME]))
    if n > 0:
      return True
    else:
      return False
  except:
    return False
Ejemplo n.º 24
0
def remove_static_relation(data):
  if KEY.ID not in data or KEY.USER_ID not in data:
    return False
  sql = "delete from static_relation where user_a = %d and user_b = %d"
  try:
    n = dbhelper.execute(sql%(data[KEY.ID], data[KEY.USER_ID]))
    if n > 0:
      return True
    else:
      return False
  except:
    return False
Ejemplo n.º 25
0
def update_event(data):
    result = True
    sql = ""
    if KEY.CONTENT in data:
        data[KEY.CONTENT] = MySQLdb.escape_string(
            data[KEY.CONTENT].encode("utf8"))
        sql = "update event set content = '%s' where id = %d"
        try:
            dbhelper.execute(sql % (data[KEY.CONTENT], data[KEY.EVENT_ID]))
            result &= True
        except:
            result &= False

    if KEY.LONGITUDE in data and KEY.LATITUDE in data:
        sql = "update event set longitude = %f, latitude = %f where id = %d"
        try:
            dbhelper.execute(
                sql %
                (data[KEY.LONGITUDE], data[KEY.LATITUDE], data[KEY.EVENT_ID]))
            result &= True
        except:
            result &= False

    if KEY.STATE in data:
        if data[KEY.STATE] == 0:
            data[KEY.STATE] = 1
        sql = "update event set state = %d where id = %d"
        try:
            dbhelper.execute(sql % (data[KEY.STATE], data[KEY.EVENT_ID]))
            result &= True
        except:
            result &= False

    return result
Ejemplo n.º 26
0
def update_answer(data):
  if KEY.ANSWER_ID not in data:
    return False

  result = True
  sql = ""
  if KEY.CONTENT in data:
    data[KEY.CONTENT] = MySQLdb.escape_string(data[KEY.CONTENT].encode("utf8"))
    sql = "update answer set content = '%s' where id = %d"
    try:
      dbhelper.execute(sql%(data[KEY.CONTENT], data[KEY.ANSWER_ID]))
      result &= True
    except:
      result &= False
  if KEY.IS_ADOPTED in data:
    sql = "update answer set is_adopted = %d where id = %d"
    try:
      dbhelper.execute(sql%(data[KEY.IS_ADOPTED], data[KEY.ANSWER_ID]))
      result &= True
    except:
      result &= False
  if KEY.LIKING_NUM in data:
    sql = "update answer set liking_num = %d where id = %d"
    try:
      dbhelper.execute(sql%(data[KEY.LIKING_NUM], data[KEY.ANSWER_ID]))
      result &= True
    except:
      result &= False

  return result
Ejemplo n.º 27
0
def update_event(data):
    result = True
    sql = ""
    if KEY.CONTENT in data:
        data[KEY.CONTENT] = MySQLdb.escape_string(data[KEY.CONTENT].encode("utf8"))
        sql = "update event set content = '%s' where id = %d"
        try:
            dbhelper.execute(sql % (data[KEY.CONTENT], data[KEY.EVENT_ID]))
            result &= True
        except:
            result &= False

    if KEY.LONGITUDE in data and KEY.LATITUDE in data:
        sql = "update event set longitude = %f, latitude = %f where id = %d"
        try:
            dbhelper.execute(sql % (data[KEY.LONGITUDE], data[KEY.LATITUDE], data[KEY.EVENT_ID]))
            result &= True
        except:
            result &= False

    if KEY.STATE in data:
        if data[KEY.STATE] == 0:
            data[KEY.STATE] = 1
        sql = "update event set state = %d where id = %d"
        try:
            dbhelper.execute(sql % (data[KEY.STATE], data[KEY.EVENT_ID]))
            result &= True
        except:
            result &= False

    return result
Ejemplo n.º 28
0
def love_coin_transfer(data):
  if KEY.SENDER not in data and KEY.RECEIVER not in data:
    return False
  if KEY.LOVE_COIN not in data:
    return False

  sender = {}
  receiver = {}
  sender[KEY.USER_ID] = data[KEY.SENDER]
  receiver[KEY.USER_ID] = data[KEY.RECEIVER]
  sender = get_user_loving_bank(sender)
  receiver = get_user_loving_bank(receiver)
  print sender, receiver
  if sender is None or receiver is None:
    return False
  update_sender_coin = sender[KEY.LOVE_COIN] - data[KEY.LOVE_COIN]
  update_receiver_coin = receiver[KEY.LOVE_COIN] + data[KEY.LOVE_COIN]

  if update_sender_coin < 0 or update_receiver_coin < 0:
    return False

  sql = "update loving_bank set love_coin = %d where userid = %d"
  try:
    dbhelper.execute(sql%(update_sender_coin, data[KEY.SENDER]))
  except :
    return False

  try:
    dbhelper.execute(sql%(update_receiver_coin, data[KEY.RECEIVER]))
    return True
  except:
    dbhelper.execute(sql%(sender[KEY.LOVE_COIN], data[KEY.SENDER]))
    return False
Ejemplo n.º 29
0
def add_static_relation(data):
    if KEY.ID not in data or KEY.USER_ID not in data or KEY.TYPE not in data:
        return False
    sql = "replace into static_relation (user_a, user_b, type, time) values (%d, %d, %d, now())"
    try:
        n = dbhelper.execute(sql %
                             (data[KEY.ID], data[KEY.USER_ID], data[KEY.TYPE]))
        if n > 0:
            return True
        else:
            return False
    except:
        return False
Ejemplo n.º 30
0
def evaluate_user(data):
    if KEY.ID not in data or KEY.USER_ID not in data or KEY.EVENT_ID not in data:
        return False
    if KEY.VALUE not in data:
        return False

    value_list = ast.literal_eval(data[KEY.VALUE])
    value = 0.0
    for each_value in value_list:
        value += each_value
    list_len = len(value_list)
    if list_len == 0:
        list_len = 1
    value /= list_len

    sql = "replace into evaluation (event_id, from, to, value, time) values (%d, %d, %d, %f, now())"
    try:
        dbhelper.execute(
            sql % (data[KEY.EVENT_ID], data[KEY.ID], data[KEY.USER_ID], value))
        return True
    except:
        return False
Ejemplo n.º 31
0
def update_sign(data):
  if KEY.BANK_ID not in data:
    return False
  result = True
  
  sql = ""
  if KEY.SIGN_HISTORY in data:
    sql = "update sign set sign_history = '%d' where sign_name = %s"
    try:
      dbhelper.execute(sql%(data[KEY.SIGN_HISTORY], data[KEY.SIGN_NAME]))
      result &= True
    except:
      result &= False

  if KEY.SIGN_TIME in data:
    sql = "update sign set sign_time = 'now()' where sign_name = %s"
    try:
      dbhelper.execute(sql%(data[KEY.SIGN_TIME], data[KEY.SIGN_NAME]))
      result &= True
    except:
      result &= False
  return True
Ejemplo n.º 32
0
def update_bank(data):
  if KEY.BANK_ID not in data:
    return False
  result = True
  
  sql = ""
  if KEY.GRADE in data:
    sql = "update bank set grade = '%d' where bank_name = %d"
    try:
      dbhelper.execute(sql%(data[KEY.GRADE], data[KEY.BANK_NAME]))
      result &= True
    except:
      result &= False

  if KEY.COIN in data:
    sql = "update bank set coin = '%d' where bank_name = %d"
    try:
      dbhelper.execute(sql%(data[KEY.COIN], data[KEY.BANK_NAME]))
      result &= True
    except:
      result &= False
  return True
Ejemplo n.º 33
0
def remove_static_relation(data):
  if KEY.ID not in data or KEY.USER_ACCOUNT not in data or KEY.TYPE not in data:
    return False
  find_user_id = "select id from account where account = %d"
  user_id = dbhelper.execute_fetchone(find_user_id%data[KEY.USER_ACCOUNT])
  sql = "delete from static_relation where user_a = %d and user_b = %d and type = %d"
  try:
    n = dbhelper.execute(sql%(data[KEY.ID], user_id[0], data[KEY.TYPE]))
    if n > 0:
      return True
    else:
      return False
  except:
    return False
Ejemplo n.º 34
0
def add_static_relation(data):
  if KEY.ID not in data or KEY.USER_ACCOUNT not in data or KEY.TYPE not in data:
    return False
  find_user_id = "select id from account where account = %d"
  user_id = dbhelper.execute_fetchone(find_user_id%data[KEY.USER_ACCOUNT])
  #sql = "replace into static_relation (user_a, user_b, type, time) values (%d, %d, %d, now())"
  sql = "insert into static_relation (user_a, user_b, type, time) values (%d, %d, %d, now())"
  try:
    n = dbhelper.execute(sql%(data[KEY.ID], user_id[0], data[KEY.TYPE]))
    if n > 0:
      return True
    else:
      return False
  except:
    return False
Ejemplo n.º 35
0
def sign_in(data):
  if KEY.ID not in data:
    return False
  if is_sign_in(data[KEY.ID]):
    return False
  sql = "insert into sign_in (user_id, time) values (%d, now())"
  try:
    sign_in_id = dbhelper.insert(sql%(data[KEY.ID]))
    if sign_in_id > 0:
      # every day sign in and coin increase 2
      incre = "update loving_bank set coin = coin+2 where user_id = %d"
      a = dbhelper.execute(incre%(data[KEY.ID]))
      print "                                    ", a
      print "                                    ", a
      print "                                    ", a
      print "                                    ", a
      print "                                    ", a
      return True
    else:
      return False
  except:
    return False
Ejemplo n.º 36
0
def update_user(data):
    if KEY.ID not in data:
        return False
    result = True

    sql = ""
    if KEY.NAME in data:
        data[KEY.NAME] = MySQLdb.escape_string(data[KEY.NAME].encode("utf8"))
        sql = "update user set name = '%s' where id = %d"
        try:
            dbhelper.execute(sql % (data[KEY.NAME], data[KEY.ID]))
            result &= True
        except:
            result &= False

    if KEY.NICKNAME in data:
        data[KEY.NICKNAME] = MySQLdb.escape_string(
            data[KEY.NICKNAME].encode("utf8"))
        sql = "update user set nickname = '%s' where id = %d"
        try:
            dbhelper.execute(sql % (data[KEY.NICKNAME], data[KEY.ID]))
            result &= True
        except:
            result &= False

    if KEY.GENDER in data:
        sql = "update user set gender = %d where id = %d"
        try:
            dbhelper.execute(sql % (data[KEY.GENDER], data[KEY.ID]))
            result &= True
        except:
            result &= False

    if KEY.AGE in data:
        sql = "update user set age = %d where id = %d"
        try:
            dbhelper.execute(sql % (data[KEY.AGE], data[KEY.ID]))
            result &= True
        except:
            result &= False

    if KEY.PHONE in data:
        sql = "update user set phone = '%s' where id = %d"
        try:
            dbhelper.execute(sql % (data[KEY.PHONE], data[KEY.ID]))
            result &= True
        except:
            result &= False

    if KEY.LOCATION in data:
        data[KEY.LOCATION] = MySQLdb.escape_string(
            data[KEY.LOCATION].encode("utf8"))
        sql = "update user set location = '%s' where id = %d"
        try:
            dbhelper.execute(sql % (data[KEY.LOCATION], data[KEY.ID]))
            result &= True
        except:
            result &= False

    if KEY.LONGITUDE in data and KEY.LATITUDE in data:
        sql = "update user set longitude = %f, latitude = %f where id = %d"
        try:
            dbhelper.execute(
                sql % (data[KEY.LONGITUDE], data[KEY.LATITUDE], data[KEY.ID]))
            result &= True
        except:
            result &= False
    elif not (KEY.LONGITUDE not in data and KEY.LATITUDE not in data):
        result &= False

    if KEY.OCCUPATION in data:
        sql = "update user set occupation = %d where id = %d"
        try:
            dbhelper.execute(sql % (data[KEY.OCCUPATION], data[KEY.ID]))
            result &= True
        except:
            result &= False

    if KEY.IDENTITY_ID in data:
        sql = "update user set identity_id = '%s' where id = %d"
        try:
            dbhelper.execute(sql % (data[KEY.IDENTITY_ID], data[KEY.ID]))
            result &= True
        except:
            result &= False

    return result
Ejemplo n.º 37
0
def update_health_record(data):
  result = True
  if KEY.ID not in data:
    return False
  sql = ""

  if KEY.HEIGHT in data:
    sql = "update health set height = %d where user_id = %d"
    try:
      dbhelper.execute(sql%(data[KEY.HEIGHT], data[KEY.ID]))
      result &= True
    except:
      result &= False

  if KEY.WEIGHT in data:
    sql = "update health set weight = %d where user_id = %d"
    try:
      dbhelper.execute(sql%(data[KEY.WEIGHT], data[KEY.ID]))
      result &= True
    except:
      result &= False

  if KEY.BLOOD_TYPE in data:
    data[KEY.BLOOD_TYPE] = MySQLdb.escape_string(data[KEY.BLOOD_TYPE].encode("utf8"))
    sql = "update health set blood_type = '%s' where user_id = %d"
    try:
      dbhelper.execute(sql%(data[KEY.BLOOD_TYPE], data[KEY.ID]))
      result &= True
    except:
      result &= False

  if KEY.MEDICINE_TAKEN in data:
    data[KEY.MEDICINE_TAKEN] = MySQLdb.escape_string(data[KEY.MEDICINE_TAKEN].encode("utf8"))
    sql = "update health set medicine_taken = '%s' where user_id = %d"
    try:
      dbhelper.execute(sql%(data[KEY.MEDICINE_TAKEN], data[KEY.ID]))
      result &= True
    except:
      result &= False

  if KEY.MEDICAL_HISTORY in data:
    data[KEY.MEDICAL_HISTORY] = MySQLdb.escape_string(data[KEY.MEDICAL_HISTORY].encode("utf8"))
    sql = "update health set medical_history = '%s' where user_id = %d"
    try:
      dbhelper.execute(sql%(data[KEY.MEDICAL_HISTORY], data[KEY.ID]))
      result &= True
    except:
      result &= False

  if KEY.ANAPHYLAXIS in data:
    data[KEY.ANAPHYLAXIS] = MySQLdb.escape_string(data[KEY.ANAPHYLAXIS].encode("utf8"))
    sql = "update health set anaphylaxis = '%s' where user_id = %d"
    try:
      dbhelper.execute(sql%(data[KEY.ANAPHYLAXIS], data[KEY.ID]))
      result &= True
    except:
      result &= False

  return result
Ejemplo n.º 38
0
def update_user(data):
  if KEY.ID not in data:
    return False
  result = True
  
  sql = ""
  if KEY.NAME in data:
    data[KEY.NAME] = MySQLdb.escape_string(data[KEY.NAME].encode("utf8"))
    sql = "update user set name = '%s' where id = %d"
    try:
      dbhelper.execute(sql%(data[KEY.NAME], data[KEY.ID]))
      result &= True
    except:
      result &= False

  if KEY.NICKNAME in data:
    data[KEY.NICKNAME] = MySQLdb.escape_string(data[KEY.NICKNAME].encode("utf8"))
    sql = "update user set nickname = '%s' where id = %d"
    try:
      dbhelper.execute(sql%(data[KEY.NICKNAME], data[KEY.ID]))
      result &= True
    except:
      result &= False

  if KEY.GENDER in data:
    sql = "update user set gender = %d where id = %d"
    try:
      dbhelper.execute(sql%(data[KEY.GENDER], data[KEY.ID]))
      result &= True
    except:
      result &= False

  if KEY.AGE in data:
    sql = "update user set age = %d where id = %d"
    try:
      dbhelper.execute(sql%(data[KEY.AGE], data[KEY.ID]))
      result &= True
    except:
      result &= False
   
  if KEY.PHONE in data:
    sql = "update user set phone = '%s' where id = %d"
    try:
      dbhelper.execute(sql%(data[KEY.PHONE], data[KEY.ID]))
      result &= True
    except:
      result &= False

  if KEY.LOCATION in data:
    data[KEY.LOCATION] = MySQLdb.escape_string(data[KEY.LOCATION].encode("utf8"))
    sql = "update user set location = '%s' where id = %d"
    try:
      dbhelper.execute(sql%(data[KEY.LOCATION], data[KEY.ID]))
      result &= True
    except:
      result &= False

  if KEY.LONGITUDE in data and KEY.LATITUDE in data:
    sql = "update user set longitude = %f, latitude = %f where id = %d"
    try:
      dbhelper.execute(sql%(data[KEY.LONGITUDE], data[KEY.LATITUDE], data[KEY.ID]))
      result &= True
    except:
      result &= False
  elif not (KEY.LONGITUDE not in data and KEY.LATITUDE not in data):
    result &= False

  if KEY.OCCUPATION in data:
    sql = "update user set occupation = %d where id = %d"
    try:
      dbhelper.execute(sql%(data[KEY.OCCUPATION], data[KEY.ID]))
      result &= True
    except:
      result &= False

  if KEY.IDENTITY_ID in data:
    sql = "update user set identity_id = '%s' where id = %d"
    try:
      dbhelper.execute(sql%(data[KEY.IDENTITY_ID], data[KEY.ID]))
      result &= True
    except:
      result &= False

  return result
Ejemplo n.º 39
0
def update_event(data):
  result = True
  if KEY.EVENT_ID not in data:
    return False
  sql = ""
  if KEY.TITLE in data:
    data[KEY.TITLE] = MySQLdb.escape_string(data[KEY.TITLE].encode("utf8"))
    sql = "update event set title = '%s' where id = %d"
    try:
      dbhelper.execute(sql%(data[KEY.TITLE], data[KEY.EVENT_ID]))
      result &= True
    except:
      result &= False
  if KEY.CONTENT in data:
    data[KEY.CONTENT] = MySQLdb.escape_string(data[KEY.CONTENT].encode("utf8"))
    sql = "update event set content = '%s' where id = %d"
    try:
      dbhelper.execute(sql%(data[KEY.CONTENT], data[KEY.EVENT_ID]))
      result &= True
    except:
      result &= False
  
  if KEY.LONGITUDE in data and KEY.LATITUDE in data:
    sql = "update event set longitude = %f, latitude = %f where id = %d"
    try:
      dbhelper.execute(sql%(data[KEY.LONGITUDE], data[KEY.LATITUDE], data[KEY.EVENT_ID]))
      result &= True
    except:
      result &= False

  if KEY.STATE in data:
    if data[KEY.STATE] == 0:
      data[KEY.STATE] = 1
    sql = "update event set state = %d where id = %d"
    try:
      dbhelper.execute(sql%(data[KEY.STATE], data[KEY.EVENT_ID]))
      result &= True
    except:
      result &= False

  if KEY.DEMAND_NUMBER in data:
    sql = "update event set demand_number = %d where id = %d"
    try:
      dbhelper.execute(sql%(data[KEY.DEMAND_NUMBER], data[KEY.EVENT_ID]))
      result &= True
    except:
      result &= False

  if KEY.LOVE_COIN in data:
    sql = "update event set love_coin = %d where id = %d"
    try:
      dbhelper.execute(sql%(data[KEY.LOVE_COIN], data[KEY.EVENT_ID]))
      result &= True
    except:
      result &= False

  if KEY.COMMENT in data:
    data[KEY.COMMENT] = MySQLdb.escape_string(data[KEY.COMMENT].encode("utf8"))
    sql = "update event set comment = '%s' where id = %d"
    try:
      dbhelper.execute(sql%(data[KEY.COMMENT], data[KEY.EVENT_ID]))
      result &= True
    except:
      result &= False

  if KEY.LOCATION in data:
    data[KEY.LOCATION] = MySQLdb.escape_string(data[KEY.LOCATION].encode("utf8"))
    sql = "update event set location = '%s' where id = %d"
    try:
      dbhelper.execute(sql%(data[KEY.LOCATION], data[KEY.EVENT_ID]))
      result &= True
    except:
      result &= False
  return result