Ejemplo n.º 1
0
 def get_test_result(cls, test_result_uuid):
     load_test = LoadTest.objects(results__uuid=test_result_uuid)
     if not load_test.count():
         raise DoesNotExist(
             "There is no Load Test with a Test Result with uuid '%s'" %
             test_result_uuid)
     load_test = load_test.first()
     for result in load_test.results:
         if str(result.uuid) == test_result_uuid:
             return load_test, result
     raise DoesNotExist(
         "There is no Test Result with uuid '%s' in Load Test '%s'" %
         (test_result_uuid, load_test.uuid))
Ejemplo n.º 2
0
    def _handle_get(self, request):
        """
        Download the requested content using the content unit catalog and dispatch
        a celery task that causes Pulp to download the newly cached unit.

        :param request: The content request.
        :type  request: twisted.web.server.Request
        """
        catalog_path = urlparse(request.uri).path
        with Responder(request) as responder:
            try:
                catalog_entry = model.LazyCatalogEntry.objects(
                    path=catalog_path).order_by('importer_id').first()
                if not catalog_entry:
                    raise DoesNotExist()
                self._download(catalog_entry, request, responder)
            except DoesNotExist:
                logger.debug(_('Failed to find a catalog entry with path'
                               ' "{rel}".'.format(rel=catalog_path)))
                request.setResponseCode(NOT_FOUND)
            except PluginNotFound:
                msg = _('Catalog entry for {rel} references a plugin id'
                        ' which is not valid.')
                logger.error(msg.format(rel=catalog_path))
                request.setResponseCode(INTERNAL_SERVER_ERROR)
            except Exception:
                logger.exception(_('An unexpected error occurred while handling the request.'))
                request.setResponseCode(INTERNAL_SERVER_ERROR)
Ejemplo n.º 3
0
 def public_term_by_key(self, key):
     if not key:
         raise DoesNotExist("No key value given")
     key_query = Q(key=key)
     return self.get(key_query & self.public_query
                     & (self.is_draft_not_set_query
                        | self.is_not_draft_query))
Ejemplo n.º 4
0
def unfulfill_request(instructor, request):
    if request not in instructor.requests_received:
        raise DoesNotExist(f'{request} has not been received by {instructor} or has been revoked')
    if request.status != STATUS_FULFILLED:
        raise ActionError(f'{request} not yet fulfilled')
    request.update(set__status=STATUS_UNFULFILLED, unset__date_fulfilled=True)
    return request
Ejemplo n.º 5
0
    def test_handle_get_all_failed(self, model, _download, _on_all_failed,
                                   responder):
        """
         Three catalog entries.
         All (3) failed.
        """
        request = Mock(uri='http://content-world.com/content/bear.rpm')
        responder.return_value.__enter__.return_value = responder.return_value
        report = DownloadReport('', '')
        _download.side_effect = SideEffect(PluginNotFound(), DoesNotExist(),
                                           DownloadFailed(report))
        catalog = [
            Mock(url='url-a'),
            Mock(url='url-b'),
            Mock(url='url-c'),
        ]
        model.objects.filter.return_value.order_by.return_value.all.return_value = catalog
        model.objects.filter.return_value.order_by.return_value.count.return_value = len(
            catalog)

        # test
        streamer = Streamer(Mock())
        streamer._handle_get(request)

        # validation
        model.objects.filter.assert_called_once_with(path='/content/bear.rpm')
        model.objects.filter.return_value.order_by.\
            assert_called_once_with('-_id', '-revision')
        responder.assert_called_once_with(request)
        _on_all_failed.assert_called_once_with(request)
        self.assertEqual(_download.call_args_list, [
            call(request, catalog[0], responder.return_value),
            call(request, catalog[1], responder.return_value),
            call(request, catalog[2], responder.return_value)
        ])
Ejemplo n.º 6
0
def fulfill_request(instructor, request, when=None):
    if request not in instructor.requests_received:
        raise DoesNotExist(f'{request} has not been received by {instructor} or has been revoked')
    if request.status == STATUS_FULFILLED:
        raise ActionError(f'{request} already fulfilled')
    # mark `request.status` as `STATUS_FULFILLED`
    request.update(set__status=STATUS_FULFILLED, set__date_fulfilled=when or date.today())
    return request
Ejemplo n.º 7
0
 def update_label(self, node_id: ObjectId, label: str):
     if self.src_node_id == node_id:
         self.src_label = label
     elif self.dst_node_id == node_id:
         self.dst_label = label
     else:
         raise DoesNotExist(
             "Node id provided does not match with any endpoint of an self provided."
         )
     self.save()
Ejemplo n.º 8
0
def withdraw_request(student, request):
    r4c = student.req_for_courses.filter(course=request.course, recommender=request.instructor).get()
    if request in r4c.requests_sent:
        if request.status == STATUS_FULFILLED:
            raise ActionError("This request has been fulfilled")
        r4c.requests_sent.remove(request)
        r4c.requests_quota += 1
        student.save()
        request.delete()
    else:
        raise DoesNotExist(f"Request {request} doesn't exist")
Ejemplo n.º 9
0
    def post(self, login_payload):
        """Generate new access and refresh tokens with password grant_type

        Login a user with a given set of credentials
        """
        try:
            user = User.objects.get(email=login_payload.get('username'))
            if user.password_matches(login_payload.get('password')):
                return UserCredentialsSchema.create(user)
            else:
                raise DoesNotExist()
        except DoesNotExist:
            raise Unauthorized('Invalid credentials')
Ejemplo n.º 10
0
 def __init__(self, id=None, announcement=None):
     try:
         if announcement != None:
             self._announcement = announcement
         elif id != None:
             self._announcement = AnnouncementModel.objects.get(
                 id=ObjectId(id))
         else:
             raise AttributeError("""
                 Require one of announcement, id
             """)
     except Exception:
         raise DoesNotExist("ID not valid!")
Ejemplo n.º 11
0
 def update_project(self, project_name, new_name=None, new_repository=None):
     project_exists = False
     for project in self.projects:
         if project.name == project_name:
             project_exists = True
             project.name = new_name if new_name else project.name
             project.repository = new_repository if new_repository else project.repository
             break
     if project_exists:
         self.save()
     else:
         raise DoesNotExist("Project with name '%s' was not found." %
                            project_name)
Ejemplo n.º 12
0
 def __init__(self, email=None, user=None, id=None):
     if user != None:
         self._user = user
     elif id != None:
         if (ObjectId.is_valid(id)):
             self._user = UserModel.objects.get(id=ObjectId(id))
         else:
             raise DoesNotExist("ID not valid!")
     elif email != None:
         self._user = UserModel.objects.get(email=email)
     else:
         raise AttributeError("""
         Require one of email, user, id
     """)
Ejemplo n.º 13
0
    def post(self):
        try:
            parser = reqparse.RequestParser()
            parser.add_argument("email", type=str)
            parser.add_argument("password", type=str)
            parser.add_argument("hospital", type=str)
            parser.add_argument("name", type=str)
            q = parser.parse_args()
            if q.email is None or q.password is None or q.hospital is None or q.name is None:
                return {
                    "success": False,
                    "message": "Email, password, name and hospital is required"
                }
            user = HospitalAdmin.objects(email=q.email)
            if user:
                return {
                    "success": False,
                    "message": "User already exists"
                }
            hospital = Hospital.objects.get(id=q.hospital)
            if not hospital:
                raise DoesNotExist()
            password = generate_hash(q.password)
            user = HospitalAdmin(email=q.email, hospital=q.hospital, password=password, name=q.name)
            user.save()
            return {
                    "success": True,
                    "token": create_jwt({"email": user.email, "type": "hospital_admin"}),
                    "hospital_admin": user.format()
            }

        except DoesNotExist as e:
            print(e)
            return {
                "success": False,
                "message": "Invalid hospital ID"
            }
        except ValidationError as e:
            print(e)
            return {
                "success": False,
                "message": "Invalid hospital ID"
            }
        except Exception as e:
            print(e)
            print(type(e))
            return {
                "success": False,
                "message": "Something went wrong"
            }
Ejemplo n.º 14
0
 def get_same_results_for_all_load_tests_from_project(
         cls, team, project_name, module, class_name, test_name):
     load_tests = LoadTest.objects(status="Finished",
                                   team=team,
                                   project_name=project_name,
                                   results__config__module=module,
                                   results__config__class_name=class_name,
                                   results__config__test_name=test_name)
     if not load_tests.count():
         raise DoesNotExist(
             "There is no Load Test for team %s, project %s and test %s.%s.%s "
             % (team.name, project_name, module, class_name, test_name))
     results = []
     for load_test in load_tests:
         for result in load_test.results:
             if cls.result_was_for(result, module, class_name, test_name):
                 results.append(result)
     return results
Ejemplo n.º 15
0
 def __init__(self, registration=None, id=None, identifier=None, registration_date=None):
     if registration != None:
         self._registration = registration
     elif id != None:
         if (ObjectId.is_valid(id)):
             self._registration = RegistrationModel.objects.get(id=ObjectId(id))
         else:
             raise DoesNotExist("ID not valid!")
     elif identifier != None:
         self._registrations = RegistrationModel.objects(identifier=identifier)
     elif registration_date != None:
         self._registrations = RegistrationModel.objects(registration_date=registration_date)
     else:
         raise AttributeError(
             """
         Require one of identifier, registration, id
     """
         )
Ejemplo n.º 16
0
def make_request(student, instructor, course, school_applied, program_applied, deadline, date_created=None,
                 date_updated=None, status=STATUS_REQUESTED):
    # make a request
    req = Request(
        student=student,
        instructor=instructor,
        course=course,
        school_applied=school_applied,
        program_applied=program_applied,
        deadline=deadline,
        date_created=date_created if date_created else date.today(),
        date_updated=date_updated if date_updated else date.today(),
        status=status,
    ).save()
    # register request to student
    # TODO use mongoengine syntax once this issue is resolved:
    # https://github.com/MongoEngine/mongoengine/issues/2339

    success = Student.objects(
        __raw__={
            "email": student.email,
            "req_for_courses": {
                "$elemMatch": {
                    "course": course.id,
                    "recommender": instructor.id,
                    "requests_quota": {"$gt": 0},
                }
            }
        }
    ).update(
        __raw__={
            "$inc": {"req_for_courses.$.requests_quota": -1},
            "$push": {"req_for_courses.$.requests_sent": req.id}
        }
    )
    if not success:
        raise DoesNotExist(f"Student {student} has no remaining quota for course {course}")

    # register `request` to `instructor`
    instructor.update(push__requests_received=req)

    return req
Ejemplo n.º 17
0
def search_assignments(max_records: PositiveInt, **kwargs):
    out_asses = []
    filters = {}
    filters_klass = {}
    for k, v in kwargs.items():
        if v is not None:
            if k == "deadline":
                filters[k] = {"$gte": v}
            elif k == "teacher":
                filters[k] = ObjectId(v)
            elif k == "grade":
                filters_klass[k] = Grade(v)
            elif k == "section" and "grade" in kwargs and kwargs["grade"] is not None:
                filters_klass[k] = Section(v)
            elif k == "subject":
                filters[k] = Subject(v)
            elif k == "state":
                filters[k] = AssignmentState(v)

    try:
        if len(filters_klass.keys()) > 0:
            klass_ids = []
            logger.bind(payload=filters_klass).debug("Searching klasses with filters:")
            klass_itr = models_school.Klass.objects(__raw__=filters_klass).only('id')
            for klass in klass_itr:
                klass_ids.append(klass.id)
            if len(klass_ids) > 0:
                filters["klass"] = {"$in": klass_ids}
            else:
                raise DoesNotExist()
        logger.bind(payload=filters).debug("Searching assignments with filters:")
        asses = models_grading.Assignment.objects(__raw__=filters).order_by('+deadline').limit(max_records)
        for ass in asses:
            out_ass = schemas_grading.Assignment.from_orm(ass)
            out_asses.append(out_ass)
    except DoesNotExist:
        logger.info("No assignments exists for given criteria")
    return out_asses
Ejemplo n.º 18
0
    def get(self, **kwargs):
        cid = kwargs["pk"]
        try:
            super().get(**kwargs)  # trigger DoesNotExist if necessary
            nb = Notebooks.objects.get(pk=cid)
            try:
                if not nb.cells[-1]["outputs"]:
                    kernel = client.start_kernel("python3")

                    for idx, cell in enumerate(nb.cells):
                        if cell["cell_type"] == "code":
                            output = kernel.execute(cell["source"])
                            if output:
                                outtype = ("text/html"
                                           if output.startswith("<div") else
                                           "text/plain")
                                cell["outputs"].append({
                                    "data": {
                                        outtype: output
                                    },
                                    "metadata": {},
                                    "transient": {},
                                    "output_type":
                                    "display_data",
                                })
                            sse.publish({"message": idx + 1},
                                        type="notebook",
                                        channel=cid)

                    nb.cells[1] = nbf.new_code_cell(
                        "client = load_client('<your-api-key-here>')")
                    nb.save()  # calls Notebooks.clean()
                    sse.publish({"message": 0}, type="notebook", channel=cid)
                    client.shutdown_kernel(kernel)
            except Exception as ex:
                print(ex)
                sse.publish({"message": -1}, type="notebook", channel=cid)
            return super().get(**kwargs)

        except DoesNotExist:
            nb = None
            try:
                nb = Notebooks.objects.only("pk").get(pk=cid)
            except DoesNotExist:
                # create and save unexecuted notebook, also start entry to avoid rebuild on subsequent requests
                contrib = Contributions.objects.get(id=cid)
                cells = [
                    nbf.new_code_cell(
                        "headers = {'X-Consumer-Groups': 'admin', 'X-Consumer-Username': '******'}\n"
                        "client = load_client(headers=headers)"),
                    nbf.new_code_cell(
                        f"contrib = client.contributions.get_entry(pk='{cid}', _fields=['_all']).result()"
                    ),
                    nbf.new_markdown_cell("## Info"),
                    nbf.new_code_cell(
                        "fields = ['title', 'owner', 'authors', 'description', 'urls']\n"
                        "prov = client.projects.get_entry(pk=contrib['project'], _fields=fields).result()\n"
                        "HierarchicalData(prov)"),
                    nbf.new_markdown_cell("## HData"),
                    nbf.new_code_cell("HierarchicalData(contrib['data'])"),
                ]

                tables = Tables.objects.only("id",
                                             "name").filter(contribution=cid)
                if tables:
                    cells.append(nbf.new_markdown_cell("## Tables"))
                    for table in tables:
                        cells.append(nbf.new_markdown_cell(table.name))
                        cells.append(
                            nbf.new_code_cell(
                                f"table = client.tables.get_entry(pk='{table.id}', _fields=['_all']).result()\n"
                                "Table.from_dict(table)"))
                        cells.append(
                            nbf.new_code_cell("Plot.from_dict(table)"))

                structures = Structures.objects.only(
                    "id", "name").filter(contribution=cid)
                if structures:
                    cells.append(nbf.new_markdown_cell("## Structures"))
                    for structure in structures:
                        cells.append(nbf.new_markdown_cell(structure.name))
                        cells.append(
                            nbf.new_code_cell(
                                "structure = client.structures.get_entry(\n"
                                f"\tpk='{structure.id}', _fields=['lattice', 'sites', 'charge']\n"
                                ").result()\n"
                                "Structure.from_dict(structure)"))

                nb = Notebooks(pk=cid, is_public=contrib.is_public)
                doc = deepcopy(seed_nb)
                doc["cells"] += cells
                self.Schema().update(nb, doc)
                nb.save()  # calls Notebooks.clean()
                return super().get(**kwargs)

            if nb is not None:
                raise DoesNotExist(
                    f"Notebook {nb.id} exists but user not in project group")
Ejemplo n.º 19
0
    def get(self, **kwargs):
        cid = kwargs["pk"]
        qfilter = lambda qs: self.has_read_permission(request, qs.clone())
        try:
            # trigger DoesNotExist if necessary (due to permissions or non-existence)
            nb = self._resource.get_object(cid, qfilter=qfilter)
            try:
                if not nb.cells[-1]["outputs"]:
                    ws = connect_kernel()
                    for idx, cell in enumerate(nb.cells):
                        if cell["cell_type"] == "code":
                            cell["outputs"] = execute(ws, cid, cell["source"])
                            sse.publish({"message": idx + 1},
                                        type="notebook",
                                        channel=cid)

                    ws.close()
                    nb.cells[1] = nbf.new_code_cell(
                        "client = Client('<your-api-key-here>')")
                    nb.save()  # calls Notebooks.clean()
                    sse.publish({"message": 0}, type="notebook", channel=cid)
            except Exception as ex:
                print(ex)
                sse.publish({"message": -1}, type="notebook", channel=cid)

            return self._resource.serialize(nb, params=request.args)

        except DoesNotExist:
            nb = None
            try:
                nb = Notebooks.objects.only("pk").get(pk=cid)
            except DoesNotExist:
                # create and save unexecuted notebook, also start entry to avoid rebuild on subsequent requests
                from mpcontribs.api.contributions.views import ContributionsResource

                res = ContributionsResource()
                res._params = {"_fields": "_all"}
                contrib = res.get_object(cid, qfilter=qfilter)
                cells = [
                    nbf.new_code_cell(
                        'client = Client(headers={"X-Consumer-Groups": "admin"})'
                    ),
                    nbf.new_markdown_cell("## Project"),
                    nbf.new_code_cell(
                        f'client.get_project("{contrib.project.pk}").pretty()'
                    ),
                    nbf.new_markdown_cell("## Contribution"),
                    nbf.new_code_cell(
                        f'client.get_contribution("{cid}").pretty()'),
                ]

                if contrib.tables:
                    cells.append(nbf.new_markdown_cell("## Tables"))
                    for _, tables in contrib.tables.items():
                        for table in tables:
                            tid = table["id"]
                            cells.append(
                                nbf.new_code_cell(
                                    f'client.get_table("{tid}").plot()'))

                if contrib.structures:
                    cells.append(nbf.new_markdown_cell("## Structures"))
                    for _, structures in contrib.structures.items():
                        for structure in structures:
                            sid = structure["id"]
                            cells.append(
                                nbf.new_code_cell(
                                    f'client.get_structure("{sid}")'))

                nb = Notebooks(pk=cid, is_public=contrib.is_public)
                doc = deepcopy(seed_nb)
                doc["cells"] += cells
                self.Schema().update(nb, doc)
                nb.save()  # calls Notebooks.clean()
                return self._resource.serialize(nb, params=request.args)

            if nb is not None:
                raise DoesNotExist(
                    f"Notebook {nb.id} exists but user not in project group")
Ejemplo n.º 20
0
 def find_last_created_model(cls) -> Model:
     model = cls.objects.order_by("-createdAt").first()
     if model is None:
         raise DoesNotExist("No model in DB")
     return model