Exemple #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')
Exemple #2
0
   def post(self):
      if self.user.points < POINTS_TO_ASK:
         self.error('Not enough points')
         return

      try:
         form = self.request.body
         form = json.loads(form)
         qtext = form['question']
         if not qtext.endswith('?'):
            qtext = qtext + '?'
         #qtext = self.request.get('question')
      except:
         self.error('Wrong data')
         return
      question = Question()
      question.question = qtext
      question.askedby  = self.user.key
      question.put()
      self.user.points -= POINTS_TO_ASK
      self.user.put()
      self.success(True)

      #notify admin
      try:
         appconfig = ApnConfig.get_or_insert('config')
         admin_devices = appconfig.admin_devices
         if not admin_devices:
            return
         else:
            admin_devices = json.loads(admin_devices)
         tokens = []
         for dev in admin_devices:
            tokens += [dev['device_token']]
         from push import SendPushMessage
         sender = SendPushMessage()
         sender.post_for_admin(tokens, 'There is a new question')
      except Exception, e:
         logging.error('Push admin message failed {0}'.format(e))
Exemple #3
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))
Exemple #4
0
   def post(self):
      try:
         form = self.request.get('form')
         if not form:
            form = self.request.body
         form = json.loads(form)
         question = ndb.Key('Question', form['question'])
         answer_text = form['answer']
      except:
         self.error('Wrong data')
         return
      if not question:
         self.error('Question {0} not found'.format(form['question']))
         return
      assignedquestion = None
      has_unanswered_questions = False
      for aq in self.user.assignedquestions:
         if aq.question == question:
            assignedquestion = aq
         elif aq.status == 'new':
            has_unanswered_questions = True
      if not assignedquestion:
         self.error('This question is no more assigned to you')
         return
      assignedquestion.status = 'in review'
      if not has_unanswered_questions:
         self.user.can_answer = False
      self.user.put()

      answer = Answer()
      answer.question = question
      answer.answer = answer_text
      answer.answeredby = self.user.key
      answer.answeredon = datetime.datetime.utcnow()
      answer.put()

      self.extract_assigned_questions()
      for q in self.user._questions:
         if q['id'] == question.id():
            if not 'answers' in q.keys():
               q['answers'] = []
            found = False
            answer_key_id = answer.key.id()
            for a in q['answers']:
               if a['id'] == answer_key_id:
                  found = True
                  break
            if not found:
               q['answers'].insert(0, {'id': answer_key_id, 'answer': answer_text})

      self.output(self.construct_user_profile())

      #notify admin
      try:
         appconfig = ApnConfig.get_or_insert('config')
         admin_devices = appconfig.admin_devices
         if not admin_devices:
            return
         else:
            admin_devices = json.loads(admin_devices)
         tokens = []
         for dev in admin_devices:
            tokens += [dev['device_token']]
         from push import SendPushMessage
         sender = SendPushMessage()
         sender.post_for_admin(tokens, 'There is a new answer')
      except Exception, e:
         logging.error('Push admin message failed {0}'.format(e))