Example #1
0
 def get(self):
    from push import SendPushMessage
    sender = SendPushMessage()
    """answers = Answer.query().order(-Answer.question).fetch(10)
    for answer in answers:
       question = answer.question.get()
       if question.askedby:
          self.response.write('A: {0}, Q: {1}'.format(answer.key.id(), question.key.id()))
          sender.post('bc49fd3d483e8975933828872aabd848941d4cdc6835550c5cd6cb162c239494', answer.key.id())
          break"""
    #sender.post_for_admin(['2330ed34f16c583c443c2e41484b09c6b46de04781ea6c460a9ab488533c15d2'], 'test admin')
    sender.post('bc49fd3d483e8975933828872aabd848941d4cdc6835550c5cd6cb162c239494', 'reload_questions', 'You have new questions')
    self.response.write('OK')
Example #2
0
   def post(self):
      form = self.request.get('form')
      if not form:
         form = self.request.body
      form = json.loads(form)
      if 'ids' in form.keys():
         answers = form['ids']
      else:
         self.error('Wrong data')
         return
      if 'action' in form.keys():
         action = int(form['action']) # 1 to approve, -1 to reject
      else:
         self.error('Wrong data')
         return

      silent = 'silent' in form.keys()
      answers = answers.split(',')
      update_answers = []
      update_questions = []
      update_users =[]
      for a in answers:
         answer = Answer.get_by_id(int(a))
         if not answer or answer.moderated == action:
            continue
         prev_moderated = answer.moderated
         answer.moderated = action
         update_answers += [answer]

         question = answer.question.get()
         if not question:
            if not silent:
               self.error('Question {0} not found'.format(answer.question.id()))
            continue
         if prev_moderated == 0:
            if action == 1:
               question.queue += 1 #move this question down in the questions stack
               question.numanswers += 1
               question.lastanswer = answer.answeredon
            question.numassigns -= 1
            update_questions += [question]
         elif prev_moderated == -1:
            question.numanswers += 1
            if question.lastanswer:
               question.lastanswer = max(answer.answeredon, question.lastanswer)
            else:
               question.lastanswer = answer.answeredon
            update_questions += [question]
         elif prev_moderated == 1 and action == -1:
            question.numanswers -= 1
            update_questions += [question]

         modified = False
         answerer = answer.answeredby.get()
         if answerer:
            if prev_moderated == 0:
               answerer.points += POINTS_FOR_ANSWER*action
               modified = True
               update_users += [answerer]

            for aq in answerer.assignedquestions:
               if aq.question == question.key:
                  aq.status = 'answered' if action == 1 else 'banned'
                  modified = True
            prev_unanswered_count = num_unanswered_questions(answerer)
            update_assigned_questions(answerer, True)
            if prev_unanswered_count != num_unanswered_questions(answerer) and not modified:
               update_users += [answerer]

         if action == 1:
            if question.askedby:
               asker = question.askedby.get()
               if asker is not None and asker.devicetoken:
                  try:
                     from push import SendPushMessage
                     sender = SendPushMessage()
                     sender.post(asker.devicetoken, answer.key.id(), 'You have a new answer')
                  except Exception, e:
                     receiver = asker.email if hasattr(asker, 'email') else question.askedby.id()
                     logging.error('Push message to {0} failed {1}'.format(receiver, e))
            #else: #can load new questions right after approval, no need to wait for rating (for a while)
            if answerer:
               if answerer.devicetoken is not None and prev_unanswered_count == 0:
                  logging.info('sending push to {0}'.format(answerer.devicetoken))
                  try:
                     from push import SendPushMessage
                     sender = SendPushMessage()
                     sender.post(answerer.devicetoken, 'reload_questions', 'You have new questions')
                  except Exception, e:
                     receiver = answerer.email if hasattr(answerer, 'email') else answer.answeredby.id()
                     logging.error('Push message to {0} failed {1}'.format(receiver, e))
               else:
                  logging.info('{0} has {1} unanswered questions at the moderate moment'.format(answerer.key.id(), prev_unanswered_count))