Пример #1
0
def create_widget_for_account_by_email(widget_name, email):
  """
  Creates a new widget for the account, will return widget object if success, else it will return None
  """
  new_widget = None
  property_name = None
  if widget_name == "TrophyCase":
    new_widget = TrophyCase(key_name=email)
    property_name = "trophyWidget"
  elif widget_name == "Rank":
    new_widget = Rank(key_name=email)
    property_name = "rankWidget"
  elif widget_name == "Points":
    new_widget = Points(key_name=email)
    property_name = "pointsWidget"
  elif widget_name == "Notifier":
    new_widget = Notifier(key_name=email)
    property_name = "notifierWidget"
  elif widget_name == "Milestones":
    new_widget = Milestones(key_name=email)
    property_name = "milestoneWidget"
  elif widget_name == "Leaderboard":
    new_widget = Leaderboard(key_name=email)
    property_name = "leaderWidget"
    
  if new_widget!= None:
    memcache_db.save_entity(new_widget, email)
    update_fields = { property_name : new_widget }
    memcache_db.update_fields(email, "Accounts", update_fields)
  else:
    logging.info("Unable to create widget because widget type unknown: " + widget_name)
    
  return new_widget
Пример #2
0
def add_milestones(acc_ref):
  logging.error("Had to add a Milestones widget to an account " + str(acc_ref.key().name()))
  new_milestones = Milestones(key_name=acc_ref.key().name())
  memcache_db.save_entity(new_milestones, acc_ref.key().name())
  acc_ref.milestonesWidget = new_milestones
  accounts_dao.save(acc_ref)
  return new_milestones
Пример #3
0
  def post(self):
    isLocal = os.environ['SERVER_SOFTWARE'].startswith('Dev')
    if not isLocal:
      return
    api_key = self.request.get('apikey')
    account_id = self.request.get('accountid')
    badge_id = self.request.get('badgeid')
    badge_theme = self.request.get('theme')
    user_id = self.request.get('user')
    if not badge_theme or not badge_id or not account_id or not api_key:
      ret = bad_args()
      self.response.out.write(ret)
      return

    user_key = users_dao.get_user_key(account_id, user_id)
    user_ref = users_dao.get_user(account_id, user_id)
    if user_ref:
      badge_instances = badges_dao.get_user_badges(user_ref)
      for b in badge_instances:
        badges_dao.delete_badge_instance(b.key().name())
      users_dao.delete_user(user_key)

    trophy_case_widget = TrophyCase(key_name=account_id)
    points_widget = Points(key_name=account_id)
    rank_widget = Rank(key_name=account_id)
    notifier_widget = Notifier(key_name=account_id)
    leader_widget = Leaderboard(key_name=account_id)
    milestones_widget = Milestones(key_name=account_id)
    acc = Accounts(key_name=account_id,
                   email=account_id,
                   password="******",
                   isEnabled=constants.ACCOUNT_STATUS.ENABLED, 
                   accountType="admin",
                   paymentType="free",
                   cookieKey="xxxxxxxxx", 
                   apiKey=api_key, 
                   trophyWidget=trophy_case_widget,
                   pointsWidget=points_widget,
                   rankWidget=rank_widget,
                   leaderWidget=leader_widget,
                   milestoneWidget=milestones_widget)

     # delete ten badges
    for ii in range(0,10):
      badgeKey = badges_dao.create_badge_key(account_id, badge_theme, str(ii), "private")
      badges_dao.delete_badge_image(badgeKey)
      badges_dao.delete_badge(badgeKey)

    widgets_dao.delete_widget(account_id, "TrophyCase")
    widgets_dao.delete_widget(account_id, "Points")
    widgets_dao.delete_widget(account_id, "Rank")
    widgets_dao.delete_widget(account_id, "Leaderboard")
    widgets_dao.delete_widget(account_id, "Notifier")
    widgets_dao.delete_widget(account_id, "Milestones")
    accounts_dao.delete_account(account_id)
    self.response.out.write(success_ret())
    return
Пример #4
0
  def milestones_values(self, user_ref, acc_ref, height, width):
    user_badges = badges_dao.get_user_badges(user_ref)
    acc_badges = badges_dao.get_rendereable_badgeset(acc_ref)
    mcase_ref = None
    if not acc_ref:
      mcase_ref = Milestones()
    else:
      try:
        mcase_ref = acc_ref.milestoneWidget
      except:
        mcase_ref = widgets_dao.add_milestones(acc_ref)

    if user_ref and user_ref.userid == constants.ANONYMOUS_USER:
      user_badges = []

    badge_count = 0
    display_badges = []
    for badge in user_badges:
      b = {}
      try:
        # In case the badge was removed, we'll skip it
        b["badgeRef"] = badge.badgeRef
      except Exception, e:
        continue
      if badge.awarded == "yes":
        b["awarded"] = True
      else:
        b["awarded"] = True
      b["id"] = badge_count 
      b["awarded"] = badge.awarded
      b["pointsRequired"] = badge.pointsRequired

      # backward compatibility
      if badge.pointsRequired == 9999999999:
        b["pointsRequired"] = 0
   
      if badge.pointsEarned > badge.pointsRequired:
        b["pointsEarned"] = badge.pointsRequired
      else:  
        b["pointsEarned"] = badge.pointsEarned
      b["resource"] = badge.resource
      b["reason"] = badge.reason
      b["downloadLink"] = badge.downloadLink
      b["id"] = badge_count
      badge_count += 1
      display_badges.append(b)
Пример #5
0
def get_entity(key_name, ent_type):
    if ent_type not in constants.PROTECTED_DB_TYPES:
        raise Exception()
    e = memcache.get(key=key_name, namespace=ent_type)
    if e:
        try:
            e = deserialize(e)
        except:
            logging.error(
                "Memcache_db: Unable to deserialize entity of type %s" %
                ent_type)
            e = None

    if not e:
        memcache.delete(key=key_name, namespace=ent_type)
        if ent_type == "Accounts":
            e = Accounts.get_by_key_name(key_name)
        elif ent_type == "Badges":
            e = Badges.get_by_key_name(key_name)
        elif ent_type == "BadgeInstance":
            e = BadgeInstance.get_by_key_name(key_name)
        elif ent_type == "BadgeImage":
            e = BadgeImage.get_by_key_name(key_name)
        elif ent_type == "Users":
            e = Users.get_by_key_name(key_name)
        elif ent_type == "TrophyCase":
            e = TrophyCase.get_by_key_name(key_name)
        elif ent_type == "Points":
            e = Points.get_by_key_name(key_name)
        elif ent_type == "Notifier":
            e = Notifier.get_by_key_name(key_name)
        elif ent_type == "Rank":
            e = Rank.get_by_key_name(key_name)
        elif ent_type == "PassPhrase":
            e = PassPhrase.get_by_key_name(key_name)
        elif ent_type == "Milestones":
            e = Milestones.get_by_key_name(key_name)
        elif ent_type == "Leaderboard":
            e = Leaderboard.get_by_key_name(key_name)
        else:
            raise Exception()
        if e:
            memcache.add(key=key_name,
                         value=str(serialize(e)),
                         namespace=ent_type)
    return e
Пример #6
0
def get_entity(key_name, ent_type):
  if ent_type not in constants.PROTECTED_DB_TYPES:
    raise Exception()
  e = memcache.get(key=key_name, namespace=ent_type)
  if e:
    try:
      e = deserialize(e)
    except:
      logging.error("Memcache_db: Unable to deserialize entity of type %s"%ent_type)
      e = None 

  if not e:
    memcache.delete(key=key_name, namespace=ent_type)
    if ent_type == "Accounts":
      e = Accounts.get_by_key_name(key_name)
    elif ent_type == "Badges":
      e = Badges.get_by_key_name(key_name)
    elif ent_type == "BadgeInstance":
      e = BadgeInstance.get_by_key_name(key_name)
    elif ent_type == "BadgeImage":
      e = BadgeImage.get_by_key_name(key_name)
    elif ent_type == "Users":
      e = Users.get_by_key_name(key_name)
    elif ent_type == "TrophyCase":
      e = TrophyCase.get_by_key_name(key_name)
    elif ent_type == "Points":
      e = Points.get_by_key_name(key_name)
    elif ent_type == "Notifier":
      e = Notifier.get_by_key_name(key_name)
    elif ent_type == "Rank":
      e = Rank.get_by_key_name(key_name)
    elif ent_type == "PassPhrase":
      e = PassPhrase.get_by_key_name(key_name)
    elif ent_type == "Milestones":
      e = Milestones.get_by_key_name(key_name)
    elif ent_type == "Leaderboard":
      e = Leaderboard.get_by_key_name(key_name)
    else:
      raise Exception()
    if e:
      memcache.add(key=key_name,value=str(serialize(e)),namespace=ent_type)
  return e
Пример #7
0
def create_account(email,
                   password,
                   enable=False,
                   account_type="bronze",
                   payment_type="free"):
    """
  Creates an account with all the other needed dependencies properly initialized.
  """
    """
  Required:
  email = db.EmailProperty(required=True)
  password = db.StringProperty(required=True);
  isEnabled = db.StringProperty(required=True, choices=ACCOUNT_STATUS.RANGE_OF_VALUES)
  accountType = db.StringProperty(required=True, choices=set(ACCOUNT_TYPES)) 
  paymentType = db.StringProperty(required=True,choices=set(PAYMENT_TYPES))
  cookieKey = db.StringProperty(required=True)
  apiKey = db.StringProperty(required=True)
  trophyWidget = db.ReferenceProperty(required=True, reference_class=TrophyCase)
  pointsWidget = db.ReferenceProperty(required=True, reference_class=Points)
  rankWidget = db.ReferenceProperty(required=True, reference_class=Rank)
  """

    new_trophy_case = TrophyCase(key_name=email)
    memcache_db.save_entity(new_trophy_case, email)

    new_rank = Rank(key_name=email)
    memcache_db.save_entity(new_rank, email)

    new_points = Points(key_name=email)
    memcache_db.save_entity(new_points, email)

    new_notifier = Notifier(key_name=email)
    memcache_db.save_entity(new_notifier, email)

    new_leader = Leaderboard(key_name=email)
    memcache_db.save_entity(new_leader, email)

    new_milestones = Milestones(key_name=email)
    memcache_db.save_entity(new_milestones, email)
    """ Generate an API key """
    api_key = str(uuid.uuid4())
    """ Hash the password """
    hashed_password = hashlib.sha1(password).hexdigest()

    enable_account = ACCOUNT_STATUS.PENDING_CREATE
    if enable:
        enable_account = ACCOUNT_STATUS.ENABLED

    newacc = Accounts(key_name=email,
                      email=email,
                      password=hashed_password,
                      isEnabled=enable_account,
                      accountType=account_type,
                      paymentType=payment_type,
                      apiKey=api_key,
                      cookieKey="xxxxxxxxxxxxxx",
                      trophyWidget=new_trophy_case,
                      pointsWidget=new_points,
                      rankWidget=new_rank,
                      notifierWidget=new_notifier,
                      leaderWidget=new_leader,
                      milestoneWidget=new_milestones)

    try:
        memcache_db.save_entity(newacc, email)
    except:
        logging.error("Failed to create account")

    users_dao.create_new_user(email, constants.ANONYMOUS_USER)

    return newacc
Пример #8
0
  def milestones_values(self, user_ref, acc_ref, height, width):
    user_badges = badges_dao.get_user_badges(user_ref)
    acc_badges = badges_dao.get_rendereable_badgeset(acc_ref)
    mcase_ref = None
    if not acc_ref:
      mcase_ref = Milestones()
    else:
      try:
        mcase_ref = acc_ref.milestoneWidget
      except:
        mcase_ref = widgets_dao.add_milestones(acc_ref)

    if user_ref and user_ref.userid == constants.ANONYMOUS_USER:
      user_badges = []

    badge_count = 0
    display_badges = []
    for badge in user_badges:
      b = {}
      if badge.awarded == "yes":
        b["awarded"] = True
      else:
        b["awarded"] = True
      b["id"] = badge_count 
      b["awarded"] = badge.awarded
      b["pointsRequired"] = badge.pointsRequired

      # backward compatibility
      if badge.pointsRequired == 9999999999:
        b["pointsRequired"] = 0
   
      if badge.pointsEarned > badge.pointsRequired:
        b["pointsEarned"] = badge.pointsRequired
      else:  
        b["pointsEarned"] = badge.pointsEarned
      b["resource"] = badge.resource
      b["reason"] = badge.reason
      b["downloadLink"] = badge.downloadLink
      b["id"] = badge_count
      b["badgeRef"] = badge.badgeRef
      badge_count += 1
      display_badges.append(b)
    # Put all badges that have not been awarded
    to_add = []
    for aa in acc_badges:
      is_there = False
      for dd in display_badges:
        if aa["key"] == dd["badgeRef"].key().name():
          is_there = True
      if not is_there:
        b = {}
        b["id"] = badge_count 
        b["awarded"] = False
        b["pointsEarned"] = 0
        b["pointsRequired"] = 0
        # This name should not have changed
        b["resource"] = ""
        b["reason"] = aa["description"]
        b["downloadLink"] = aa["downloadLink"]
        badge_count += 1
        to_add.append(b)
    display_badges.extend(to_add)
    ret = {"status":"success"}
     
    for ii in mcase_ref.properties():
      ret[ii] = getattr(mcase_ref, ii)
    ret["badges"] = display_badges

    # Internal div's need to be slighy smaller than the iframe
    if width and height:
      try:
        width = int(width)
        height = int(height)
        # How did I get this equation? Trial and error.
        height = height - 2 *int(ret['borderThickness']) - 8
        width = width - 2 *int(ret['borderThickness']) - 8
        ret['height'] = height
        ret['width'] = width
      except:
        pass
    ret['barSize'] = ret['imageSize']
    return ret 
Пример #9
0
def get_milestones_properties_to_render(email):
  return get_values("Milestones", email, Milestones.properties())
Пример #10
0
def get_milestones_properties_to_render(email):
  return get_values("Milestones", email, Milestones.properties())