예제 #1
0
 async def get(self):
     _id = self.get_argument('_id', None)
     # Downloadable is not a necessary parameter
     downloadable = self.get_argument('downloadable', None)
     test_type = self.get_argument('testType')
     collection = switch_response_collection(self, test_type)
     if not collection:
         self.set_error(400, 'Invalid test type')
         return
     # Get a list or a response
     if not _id:
         test_id = self.get_argument('testId')
         data = collection.find({'userId': self.user_id, 'testId': ObjectId(test_id)}).sort(
             'createdAt', pymongo.DESCENDING)
         # data = collection.find({'userId': self.user_id}).sort('createdAt', pymongo.DESCENDING)
     else:
         data = collection.find_one({'userId': self.user_id, '_id': ObjectId(_id)})
     # Check if the responses are downloadable
     if downloadable:
         json_file_name = f"{test_type}-{datetime.now().strftime('%Y%m%d%H%M%S')}.json"
         # Set http response header for downloading file
         self.set_header('Content-Type', 'application/octet-stream')
         self.set_header('Content-Disposition', f'attachment; filename="{json_file_name}"')
     # If it is downloadable, we will need 2 intent
     self.dumps_write(data, 2 if downloadable else None)
예제 #2
0
 async def delete(self):
     _ids = self.loads_body()
     test_type = self.get_argument('testType')
     collection = switch_response_collection(self, test_type)
     if not collection:
         return
     # Multiple deletion
     result = collection.delete_many({'userId': self.user_id, '_id': {'$in': _ids}})
     self.dumps_write(result.raw_result)
 async def get(self):
     test_id = self.get_argument('testId')
     test_type = self.get_argument('testType')
     collection = switch_response_collection(self, test_type)
     responses_count = collection.find({
         'userId': self.user_id,
         'testId': ObjectId(test_id)
     }).count()
     self.dumps_write(responses_count)