Returns the list of submissions and corresponding aggragations based on inputs

def get_submissions(self, course, user_input):
        """ Returns the list of submissions and corresponding aggragations based on inputs """

        # Build lists of wanted users based on classrooms and specific users
        list_classroom_id = [ObjectId(o) for o in user_input.aggregation]
        classroom = list(self.database.aggregations.find({"_id": {"$in": list_classroom_id}}))
        more_username = [s["students"] for s in classroom]  # Extract usernames of students
        more_username = [y for x in more_username for y in x]  # Flatten lists
        
        # Get tasks based on organisational tags
        more_tasks = []
        for org_tag in user_input.org_tags:
            if org_tag in course.get_organisational_tags_to_task():
                more_tasks += course.get_organisational_tags_to_task()[org_tag]

        #Base query
        query_base = {
                "username": {"$in": user_input.user + more_username},
                "courseid": course.get_id(),
                "taskid": {"$in": user_input.task + more_tasks}
                }

        # Additional query field
        query_advanced = {}
        if user_input.grade_min and not user_input.grade_max:
            query_advanced["grade"] = {"$gte": float(user_input.grade_min)}
        elif not user_input.grade_min and user_input.grade_max:
            query_advanced["grade"] = {"$lte": float(user_input.grade_max)}
        elif user_input.grade_min and user_input.grade_max:
            query_advanced["grade"] = {"$gte": float(user_input.grade_min), "$lte": float(user_input.grade_max)}

        try:
            date_before = datetime.strptime(user_input.date_before, "%Y-%m-%d %H:%M:%S") if user_input.date_before else ''
            date_after = datetime.strptime(user_input.date_after, "%Y-%m-%d %H:%M:%S") if user_input.date_after else ''

            if date_before and not date_after:
                query_advanced["submitted_on"] = {"$lte": date_before}
            elif not date_before and date_after:
                query_advanced["submitted_on"] = {"$gte": date_after}
            elif date_before and date_after:
                query_advanced["submitted_on"] = {"$gte": date_after, "$lte": date_before}
        except ValueError:  # If match of datetime.strptime() fails
            pass
        
        # Query with tags
        if len(user_input.filter_tags) == len(user_input.filter_tags_presence):
            for i in range(0, len(user_input.filter_tags)):
                if id_checker(user_input.filter_tags[i]):
                    state = (user_input.filter_tags_presence[i] in ["True", "true"])
                    query_advanced["tests." + user_input.filter_tags[i]] = {"$in": [None, False]} if not state else True
            
        # Mongo operations
        data = list(self.database.submissions.find({**query_base, **query_advanced}).sort([(user_input.sort_by, 
            pymongo.DESCENDING if user_input.order == "0" else pymongo.ASCENDING)]))
        data = [dict(list(f.items()) + [("url", self.submission_url_generator(str(f["_id"])))]) for f in data]

        # Get best submissions from database
        user_tasks = list(self.database.user_tasks.find(query_base, {"submissionid": 1, "_id": 0}))
        best_submissions_list = [u["submissionid"] for u in user_tasks]  # list containing ids of best submissions
        for d in data:
            d["best"] = d["_id"] in best_submissions_list  # mark best submissions

        # Keep best submissions
        if "eval" in user_input or ("eval_dl" in user_input and "download" in web.input()):
            data = [d for d in data if d["best"]]
        return data, classroom