Beispiel #1
0
def do_lookup(request,user=None):
  ans = {"logged": "yes"}
  if not user:
    user = getUser(request)

  if not user:
    ans["logged"] = "no"  # users who haven't logged in can't see any data
    return ans

  ans["user_name"] = user.name
  ans["user_email"] = user.email
  ans["is_admin"] = user.is_admin()

  # get all the users and their usernames
  all_users = User.query().fetch()  
  user_by_name = {user.name: user} 
  for useri in all_users:
    user_by_name[useri.name] = useri

  # get all the fillouts. This is not strongly consistent. We only need to do that for the current user
  fillout_query = Fillout.query().fetch()
  fillouts = {}
  for fillout in fillout_query:
    if fillout.quiz_id not in fillouts: fillouts[fillout.quiz_id] = {}
    fillouts[fillout.quiz_id][fillout.username] = fillout.to_dict()

  # information about quizzes
  query = Quiz.query().fetch()
  ans["quizzes"] = []
  for quiz in query:
    if not ans["is_admin"] and quiz.status=="editor": continue
    quiz_dict = quiz.to_dict();
    quiz_id = quiz.key.parent().id()
    quiz_dict["id"] = quiz_id

    # get the fillouts for this quiz
    this_fillouts = fillouts.get(quiz_id,{})
    fillout_user_query = Fillout.query(ancestor=fillout_key(user.name,quiz_id)).fetch()
    if len(fillout_user_query)>0:
      fillout = fillout_user_query[0].to_dict()
      this_fillouts[user.name] = fillout
      quiz_dict["fillout"] = fillout

    if user.name in this_fillouts or quiz_dict["status"]=="old":
      quiz_dict["scores"] = score_quiz(quiz,this_fillouts)
    else: # the user hasn't filled this quiz yet, and shouldn't be able to see the answers
      for question in quiz_dict["questions"]:
        del question["answer"]
        del question["source"]

    # question ratings
    ratingQuery = QuestionRatings.query(QuestionRatings.quiz_id==quiz_id,QuestionRatings.username==ans["user_name"]).fetch()
    if len(ratingQuery)>0:
      ratings = ratingQuery[0].ratings
      for idx,rating in enumerate(ratings):
        if(rating!=-1): quiz_dict["questions"][idx]["rating"] = rating

    ans["quizzes"].append(quiz_dict)

  return ans
Beispiel #2
0
  def post(self):
    json = simplejson.loads(self.request.body)
    all_users = User.query().fetch()

    hashs = json["resetHash"]
    email = json["email"]
    username = json["username"]
    password = json["password"]

    user = None
    if len(hashs)>0:
      if not utils.checkResetHash(email,hashs):
        utils.write_back(self,{"invalid hash": 1})
        return

      users = User.query().fetch()
      for ui in users:
        if ui.email == email:
          user = ui
          break
        
      if user == None:
        utils.write_back(self,{"invalid hash": 1})
        return

    else:
      for user in all_users:
        if user.email == email:
          utils.write_back(self,{"email exists": 1})
          return

        if user.name == username:
          utils.write_back(self,{"username exists": 1})
          return

      user = User(name=username,email=email,subscribed=True)

    user.password = user.password_hash(password)
    user.put()

    self.response.set_cookie('username', urllib.quote(user.name))
    self.response.set_cookie('hash', urllib.quote(cookieHash(user.name)))

    ans = lookup_util.do_lookup(self,user)
    utils.write_back(self,ans)
Beispiel #3
0
  def get(self,qs):
    email = urllib.unquote(self.request.get("email"))
    hashs = self.request.get("hash")

    if not utils.checkResetHash(email,hashs):
      return

    users = User.query().fetch()
    for user in users:
      if user.email != email: continue
      template = JINJA_ENVIRONMENT.get_template("html/template.html")
      template_values = {}
      self.response.write(template.render(template_values))
      return
Beispiel #4
0
  def post(self):
    json = simplejson.loads(self.request.body)
    username = json["username"]

    user = None
    users = User.query().fetch()
    for ui in users:
      if ui.email == username or ui.name == username:
        user = ui
        break

    if user:
      template = JINJA_ENVIRONMENT.get_template("html/password_reset.html")
      message = mail.EmailMessage()
      message.sender = "*****@*****.**"
      message.subject = "The Market Making Game - reset your password"

      template_values = {"link": utils.resetLink(user.email,user.name)}
      message.html = template.render(template_values)
      message.to = user.email
      message.send()
Beispiel #5
0
  def post(self):
    json = simplejson.loads(self.request.body)
    username = json["username"]

    all_users = User.query().fetch()

    for user in all_users:
      if user.name != json["username"] and user.email != json["username"]:
        continue
        
      if not user.checkPassword(json["password"]):
        utils.write_back(self,{"incorrect": 1})
        return

      self.response.set_cookie('username', urllib.quote(user.name))
      self.response.set_cookie('hash', urllib.quote(cookieHash(user.name)))
      
      ans = lookup_util.do_lookup(self,user)
      utils.write_back(self,ans)
      return

    utils.write_back(self,{"incorrect": 1})