Пример #1
0
  def post(self):
    start = time.time()
    api_key = self.request.get('apikey')
    account_id = self.request.get('accountid')
    user_id = self.request.get('userid')
    badge_ref_id = self.request.get('badgeid')
    reason = self.request.get('reason')

    clean = XssCleaner()
    reason = clean.strip(reason)
    logdiction = {'event':'awardbadge', 
                  'api':'award_badge',
                  'badgeid':badge_ref_id,
                  'is_api':'yes',
                  'ip':self.request.remote_addr,
                  'user':user_id,
                  'account':account_id,
                  'success':'true'}

    # Get the account 
    acc = accounts_dao.authorize_api(account_id, api_key)
    if not acc:
      logdiction['success'] = 'false'
      logdiction['details'] = auth_error()
      logs.create(logdiction)
      self.response.out.write(auth_error())
      return 

    if not user_id or not badge_ref_id:
      logdiction['success'] = 'false'
      logdiction['details'] = bad_args()
      logs.create(logdiction)
      self.response.out.write(bad_args())
      error("User id or badge id was not given")
      return  

    # Make sure we have a legit badge before getting/creating a user
    badge_key = badges_dao.get_key_from_badge_id(account_id, badge_ref_id)
    if not badge_key:
      logdiction['success'] = 'false'
      logdiction['details'] = badge_error()
      logs.create(logdiction)
      self.response.out.write(badge_error())
      return  
  
    # Get the user
    user_ref = users_dao.get_or_create_user(account_id, user_id, acc)
    if not user_ref:
      logdiction['success'] = 'false'
      logdiction['details'] = db_error()
      logs.create(logdiction)
      self.response.out.write(db_error())
      return 
 
    badge_instance_key = badges_dao.get_badge_instance_key(badge_key, user_id) 
    # If the user already has it, skip the award
    badge_ref = badges_dao.get_badge_instance(badge_instance_key)
    if badge_ref:
      if badge_ref.awarded == "yes":
        logs.create(logdiction)
        self.response.out.write(success_ret())
        timing(start)
        return  

    # Get the Badge Type (used as a reference for the instances) 
    badge_ref = badges_dao.get_badge(badge_key)
    if not badge_ref:
      self.response.out.write(badge_error())
      return  

    if not reason:
      reason = badge_ref.description

    link = badge_ref.downloadLink
    new_badge_instance = badges_dao.create_badge_instance(
                                    badge_instance_key,
                                    badge_ref,
                                    user_ref,
                                    "yes", #isawarded
                                    0, #points
                                    0, #points_needed
                                    "private",
                                    link,
                                    reason)
    name = badges_dao.get_badge_name_from_instance_key(badge_instance_key)
    notifier.user_badge_award(user_ref, "Badge Awarded", link, reason, acc, badge_ref_id)
    logs.create(logdiction)
    self.response.out.write(success_ret())
    timing(start)
    return  
Пример #2
0
  def post(self):
    start = time.time()

    api_key = self.request.get('apikey')
    account_id = self.request.get('accountid')
    user_id = self.request.get('userid')
    badge_ref_id = self.request.get('badgeid')
    how_to_get_badge = self.request.get('how')
    points = self.request.get('pointsawarded')
    points_needed = self.request.get('pointsrequired')
    reason = self.request.get('reason') 
    logdiction = {'event':'awardbadgepoints', 
                  'api':'award_badge_points',
                  'user':user_id,
                  'is_api':'yes',
                  'ip':self.request.remote_addr,
                  'account':account_id,
                  'badgeid':badge_ref_id,
                  'points':points,
                  'success':'true'}
    try:
      points = int(points)
      points_needed = int(points_needed)
    except:
      logdiction['success'] = 'false'
      logdiction['details'] = "The number of points was not a number"
      logs.create(logdiction)
      self.response.out.write(bad_args())
      error("Account %s -- Bad value for points awarded \
                    %s or points needed %s"\
                    %(account_id, points, points_needed ))
      return

    if not reason:
      reason = ""

    # Get the account 
    acc = accounts_dao.authorize_api(account_id, api_key)
    if not acc:
      logdiction['success'] = 'false'
      logdiction['details'] = auth_error()
      logs.create(logdiction)
      self.response.out.write(auth_error())
      return 

    # Get the Badge Type (used as a reference for the instances) 
    # Do this before getting/creating user
    badge_key = badges_dao.get_key_from_badge_id(account_id, badge_ref_id)
    if not badge_key:
      logdiction['success'] = 'false'
      logdiction['details'] = badge_error()
      logs.create(logdiction)
      self.response.out.write(badge_error())
      error("Badge not found with key %s"%badge_ref_id)
      return  

    # Get the user, create if it does not exist
    user_ref = users_dao.get_or_create_user(account_id, user_id, acc)
    if not user_ref:
      logdiction['success'] = 'false'
      logdiction['details'] = db_error()
      logs.create(logdiction)
      self.response.out.write(db_error())
      return  
  
    badge_ref = badges_dao.get_badge(badge_key) 
    if not badge_ref:
      logdiction['success'] = 'false'
      logdiction['details'] = badge_error()
      logs.create(logdiction)
      ret = badge_error()
      self.response.out.write(ret)
      return  

    badge_instance_key = badges_dao.get_badge_instance_key(badge_key, user_id)
    badge_instance_ref = badges_dao.get_badge_instance(badge_instance_key)

    if not reason:
      reason = badge_ref.description
    link = badge_ref.downloadLink

    if not badge_instance_ref:
      # Create a new badge with 0 points
      isawarded = "no"
      if points >= points_needed:
        isawarded = "yes" 
      perm = badges_dao.get_badge_key_permission(badge_ref_id)
      new_badge_instance = badges_dao.create_badge_instance(
                                       badge_instance_key,
                                       badge_ref,
                                       user_ref,
                                       isawarded,
                                       points,
                                       points_needed,
                                       perm,
                                       link,
                                       reason)
      if isawarded == "yes":
        notifier.user_badge_award(user_ref, "Badge Awarded", link, reason, acc, badge_ref_id)
        logdiction['event'] = 'badgeawarded'
    else: 
      isawarded = "no"
      points_thus_far = badge_instance_ref.pointsEarned
      if points:
        points_thus_far += points
      incr_args = {"pointsEarned":points}
      reg_args = {}
      # Update the following if its changed
      if badge_instance_ref.pointsRequired != points_needed:
        reg_args["pointsRequired"] = points_needed

      if badge_instance_ref.pointsEarned < points_needed and \
              points_thus_far >= points_needed:
        notifier.user_badge_award(user_ref, "Badge Awarded", link, reason, acc, badge_ref_id)
        logdiction['event'] = 'badgeawarded'

      if points_thus_far >= points_needed:
        reg_args["awarded"] = "yes"
      try:
        ret = badges_dao.update_badge_instance(badge_instance_key, 
                                reg_args, incr_args)
        if not ret:
          raise
      except:
        error("Unable to update badge instance with key %s"%\
              badge_instance_key)
        self.response.out.write(db_error())
        return
    logs.create(logdiction)
    ret = {"status":"success",
           "badge_awarded":isawarded}
    self.response.out.write(json.dumps(ret))
    timing(start)
    return