Exemple #1
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 #2
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))