Exemple #1
0
 def filter_comparison_components(
         component_templates: QuerySet,
         components: QuerySet) -> Tuple[QuerySet, QuerySet]:
     if config["exclude_virtual_interfaces"]:
         components = components.exclude(type__in=VIRTUAL_IFACE_TYPES)
         component_templates = component_templates.exclude(
             type__in=VIRTUAL_IFACE_TYPES)
     return component_templates, components
def test_availability_slot_manager_get_get_between_dates(
        availability_slots: QuerySet):
    """Should return slots between the dates."""
    professional = availability_slots.first().professional
    service = availability_slots.exclude(service__isnull=True).first().service
    manager: AvailabilitySlotManager = AvailabilitySlot.objects
    start = arrow.utcnow().replace(
        hour=0,
        minute=0,
        second=0,
        microsecond=0,
    )
    end = start.shift(days=10)

    result = manager.get_between_dates(start, end, professional)
    assert result.count() == 10
    assert result.first().professional == professional

    result = manager.get_between_dates(
        start,
        end,
        service.professional,
        service,
    )
    assert result.count() == 10
    assert result.first().service == service

    end = start.shift(days=100)
    result = manager.get_between_dates(start, end, professional)
    assert result.count() == 61
Exemple #3
0
def apply_goods_rules_for_good(good, flagging_rules: QuerySet = None):
    # If the flagging rules are specified then these is the only one we expect, else get all active
    flagging_rules = get_active_flagging_rules_for_level(
        FlagLevels.GOOD) if not flagging_rules else flagging_rules

    # get a list of flag_id's where the flagging rule matching value is equivalent to the good control code
    ratings = [
        r for r in good.control_list_entries.values_list("rating", flat=True)
    ]
    group_ratings = []
    for rating in ratings:
        group_ratings.extend(get_clc_parent_nodes(rating))

    flagging_rules = flagging_rules.filter(
        Q(matching_values__overlap=ratings)
        | Q(matching_groups__overlap=group_ratings)).exclude(
            excluded_values__overlap=(ratings + group_ratings))

    if isinstance(good, Good) and good.status != GoodStatus.VERIFIED:
        flagging_rules = flagging_rules.exclude(
            is_for_verified_goods_only=True)

    flags = flagging_rules.values_list("flag_id", flat=True)

    if flags:
        good.flags.add(*flags)
    def filter(search_str,
               qs: QuerySet,
               _or_lookup,
               _and_lookup=None,
               _not_lookup=None):
        search_list = re.split(r'\s*,\s*', search_str.strip().strip(","))
        _not_search_list = [
            i[1:].strip() for i in search_list if i.startswith('-')
        ]
        _and_search_list = [
            i[1:].strip() for i in search_list if i.startswith('&')
        ]
        _or_search_list = [i for i in search_list if i[0] not in ['-', '&']]

        if _or_search_list:
            query = reduce(operator.or_,
                           (Q(**{_or_lookup: i}) for i in _or_search_list))
            qs = qs.filter(query)
        if _and_search_list:
            query = reduce(operator.and_, (Q(**{_and_lookup or _or_lookup: i})
                                           for i in _and_search_list))
            qs = qs.filter(query)
        if _not_search_list:
            query = reduce(operator.or_, (Q(**{_not_lookup or _or_lookup: i})
                                          for i in _not_search_list))
            qs = qs.exclude(query)
        return qs
Exemple #5
0
    def filter(queryset: QuerySet, request: Request) -> QuerySet:
        queryset = Manager_Workers.filter_list(
            queryset=queryset,
            request=request,
            name_filter='workersSelected',
            name_field='id_worker'
        )

        states_block = set(request.query_params.getlist('statesBlock[]'))
        combine_with_and = json.loads(request.query_params.get('combineWithAnd', 'false'))
        # if len(states_block) > 0:
        if combine_with_and == True:
            print('COMBINE WITH AND')
            block_soft = 'block_soft' in states_block
            queryset = queryset.filter(is_blocked_global=block_soft)

            block_project = 'block_project' in states_block
            if block_project == True:
                queryset = queryset.filter(worker_blocks_project__project=database_object_project)
            else:
                queryset = queryset.exclude(worker_blocks_project__project=database_object_project)
        else:
            pass

        return queryset
Exemple #6
0
def user_story_overworked_pie_chart_data(queryset: QuerySet) -> List:
    queryset = queryset.exclude(status=UserStory.Status.BACKLOG)
    acceptable_estimation_count = queryset.filter(
        annotated_actual_effort__gte=F("planned_effort") * 0.9,
        annotated_actual_effort__lte=F("planned_effort") * 1.1,
    ).count()
    underestimated_count = queryset.filter(
        annotated_actual_effort__gt=F("planned_effort") * 1.1).count()
    overestimated_count = queryset.filter(
        annotated_actual_effort__lt=F("planned_effort") * 0.9).count()
    return [
        {
            "name": _("Estimación aceptable (+-10%)"),
            "color": "#4CAF50",
            "y": acceptable_estimation_count
        },
        {
            "name": _("Sobreestimación (>10%)"),
            "color": "#FF9800",
            "y": overestimated_count
        },
        {
            "name": _("Subestimación (<10%)"),
            "color": "#F44336",
            "y": underestimated_count
        },
    ]
def test_validate_order_client_location(
    user: User,
    admin: User,
    user_locations: QuerySet,
    services: QuerySet,
):
    """Should validate orders client locations."""
    service = services.exclude(professional__user=user).first()
    order = Order()
    order.client = user
    order.service = service
    order.service.service_type = Service.TYPE_CLIENT_LOCATION

    with pytest.raises(ValidationError) as error:
        validate_order_client_location(order)
    assert "client location is empty" in str(error)

    order.client_location = user_locations.filter(user=admin).first()

    with pytest.raises(ValidationError) as error:
        validate_order_client_location(order)
    assert "client location from the other user" in str(error)

    order.service.service_type = Service.TYPE_ONLINE
    validate_order_client_location(order)

    order.service.service_type = Service.TYPE_CLIENT_LOCATION
    order.client_location = user_locations.filter(user=user).first()
    validate_order_service_location(order)

    order.service = None  # type: ignore
    with pytest.raises(ValidationError) as error:
        validate_order_client_location(order)
    assert "The service, client, or client location is empty" in str(error)
Exemple #8
0
def get_appointments_in_datetime_range(
        queryset: models.QuerySet,
        datetime_from: Optional[datetime.datetime] = None,
        datetime_to: Optional[datetime.datetime] = None,
        exclude_statuses: Optional[List[AppointmentStatus]] = None,
        **kwargs) -> models.QuerySet:
    """
    Filter queryset of appointments based on given datetime range.
    :param queryset: Queryset to filter
    :param datetime_from: datetime at which first appointment is present
    :param datetime_to: datetime by which last appointment starts
    :param exclude_statuses: list of statuses to be excluded from the resulting query
    :param kwargs: any optional filter kwargs to be applied
    :return: Resulting Appointment queryset
    """

    if datetime_from is not None:
        queryset = queryset.filter(datetime_start_at__gt=datetime_from -
                                   models.F('stylist__service_time_gap'))

    if datetime_to is not None:
        queryset = queryset.filter(datetime_start_at__lt=datetime_to)

    if exclude_statuses:
        assert isinstance(exclude_statuses, list)
        queryset = queryset.exclude(status__in=exclude_statuses)
    return queryset.filter(**kwargs)
def test_validate_order_client(user: User, services: QuerySet):
    """Should validate orders clients."""
    service_user = services.filter(professional__user=user).first()
    service = services.exclude(professional__user=user).first()
    order = Order()
    with pytest.raises(ValidationError) as error:
        validate_order_client(order)
    assert "service or client is empty" in str(error)
    order.service = service_user
    order.client = user

    with pytest.raises(ValidationError) as error:
        validate_order_client(order)
    assert "identical" in str(error)

    order.service = service
    with pytest.raises(ValidationError) as error:
        validate_order_client(order)
    assert "name is empty" in str(error)

    order.first_name = "first name"
    with pytest.raises(ValidationError) as error:
        validate_order_client(order)
    assert "name is empty" in str(error)

    order.last_name = "last name"
    with pytest.raises(ValidationError) as error:
        validate_order_client(order)
    assert "phone is empty" in str(error)

    order.phone = "phone"
    validate_order_client(order)
def test_validate_order_service_location(user: User, services: QuerySet):
    """Should validate orders service locations."""
    service = services.exclude(professional__user=user).first()
    other_service = services.filter(professional__user=user).exclude(
        locations=None).first()
    order = Order()
    order.service = service
    order.service.service_type = Service.TYPE_PROFESSIONAL_LOCATION

    with pytest.raises(ValidationError) as error:
        validate_order_service_location(order)
    assert "service location is empty" in str(error)

    order.service_location = other_service.locations.first()

    with pytest.raises(ValidationError) as error:
        validate_order_service_location(order)
    assert "service location from the other service" in str(error)

    order.service.service_type = Service.TYPE_ONLINE
    validate_order_service_location(order)

    order.service.service_type = Service.TYPE_PROFESSIONAL_LOCATION
    order.service_location = service.locations.first()
    validate_order_service_location(order)

    order.service = None  # type: ignore
    with pytest.raises(ValidationError) as error:
        validate_order_service_location(order)
    assert "The service or service location is empty" in str(error)
Exemple #11
0
def disprove_tutorial_comment_action(modeladmin: ModelAdmin,
                                     request: HttpRequest, queryset: QuerySet):

    # After update update_queryset becomes empty because of applied exclusion
    # thus, we store update item primary keys in update_item_pks t use later
    update_queryset = queryset.exclude(
        confirm_status=ConfirmStatusChoices.DISPROVED).filter(is_active=True)
    update_item_pks = [comment.pk for comment in update_queryset]

    updated_count = update_queryset.update(
        confirm_status=ConfirmStatusChoices.DISPROVED)

    # Execute new query to get updated objects for notification
    send_mail_queryset = queryset.filter(
        pk__in=update_item_pks).select_related("user", "tutorial")

    # Send notifications
    confirm_disprove_notifier_result = TutorialCommentConfirmDisproveNotifier(
        request, send_mail_queryset).notify()

    modeladmin.message_user(request, f"{updated_count} مورد با موفقیت رد شد",
                            messages.SUCCESS)

    message_user_email_results(request, modeladmin,
                               confirm_disprove_notifier_result)
Exemple #12
0
    def create_from_questions(cls,
                              user: User,
                              questions: QuerySet,
                              max_n=20,
                              randomise_order=True,
                              include_answered=False,
                              save=True) -> "QuizSession":
        cls.check_no_current_session(user=user)

        if include_answered is False:
            questions = questions.exclude(records__in=Record.objects.filter(
                user=user))

        if randomise_order is True:
            questions = questions.order_by("?")

        max_n = max_n if max_n <= questions.count() else questions.count()
        selected_qs = questions[0:max_n + 1]

        if selected_qs.count() < 1:
            raise RuntimeError(
                "Cannot create Quiz Session without any questions!")

        session = cls(user=user)
        session.save()
        session.questions.set(selected_qs)
        session.save()
        return session
Exemple #13
0
def unique_slugify(
    value: str,
    instance: Model = None,
    queryset: QuerySet = None,
    slug_field_name: str = "name",
    slug_len: Optional[int] = None,
    slug_separator: str = "-",
) -> str:
    """
    Calculate a unique slug of ``value``.

    ``queryset`` doesn't need to be provided if instance is  - it'll default
    to using the ``.all()`` queryset from the model's default manager.

    ``slug_field_name`` should be a string matching the name of the field to
    store the slug in (and the field to check against for uniqueness).

    From https://djangosnippets.org/snippets/690/ with some modifications:
    - does not require an instance (can be used for input validation)
    - returns the slug instead of setting the field
    """
    if slug_len is None:
        if instance:
            slug_len = instance._meta.get_field(slug_field_name).max_length
        else:
            slug_len = 256

    # Sort out the initial slug, limiting its length if necessary.
    slug = slugify(value)
    slug = re.sub("_", slug_separator, slug)
    if slug_len:
        slug = slug[:slug_len]
    slug = slug_strip(slug, slug_separator)
    slug = slug_start_dedigit(slug)
    original_slug = slug

    # Create the queryset if one wasn't explicitly provided and exclude the
    # current instance from the queryset.
    if queryset is None:
        if instance:
            queryset = instance.__class__._default_manager.all()
        else:
            raise RuntimeError("Must provide queryset or instance")
    if instance and instance.pk:
        queryset = queryset.exclude(pk=instance.pk)

    # Find a unique slug. If one matches, at '-2' to the end and try again
    # (then '-3', etc).
    next = 2
    while not slug or queryset.filter(**{slug_field_name: slug}):
        slug = original_slug
        end = "%s%s" % (slug_separator, next)
        if slug_len and len(slug) + len(end) > slug_len:
            slug = slug[: slug_len - len(end)]
            slug = slug_strip(slug, slug_separator)
        slug = "%s%s" % (slug, end)
        next += 1

    return slug
Exemple #14
0
 def queryset(self, request, queryset: QuerySet):
     value = self.value()
     if value is not None:
         filter = {"tags__value": "farmacias"}
         if value:
             return queryset.filter(**filter)
         else:
             return queryset.exclude(**filter)
Exemple #15
0
def filter_private_events(events: QuerySet, user: User):
    """
    Given a QuerySet containing SingleDateTimeEvent objects,
    ensure that any objects whose user==user and private==True
    are filtered out. This supports OrganisationDetailView, which
    lists all events for an organisation but which must hide private
    events for the logged-in user.
    """
    return events.exclude(~Q(user=user) & Q(private=True))
Exemple #16
0
 def update_if_not_equal(obj: QuerySet, row: dict):
     # Can not delete the "id" key-value from `rows` as it will manipulate the data which is later used in
     # `delete_unused_objects`
     attributes = {k: v for k, v in row.items() if k != "id"}
     obj = obj.exclude(**attributes)
     if obj.exists():
         obj.update(**attributes)
         if not settings.SUPPRESS_TEST_OUTPUT:
             print(f"UPDATED {obj.model.__name__}: {dict(row)}")
Exemple #17
0
 def add_users(self, users_qs: QuerySet) -> None:
     assert isinstance(users_qs.first(),
                       User), f"{users_qs.first()} is given"
     if users_qs.exists():
         not_added_users_id_qs = users_qs.exclude(
             id__in=self.users.values_list("id", flat=True)).values("id")
         UserTeam.objects.bulk_create([
             UserTeam(user_id=user_id["id"], team_id=self.id)
             for user_id in list(not_added_users_id_qs)
         ])
Exemple #18
0
def user_story_delayed_pie_chart_data(queryset: QuerySet) -> List:
    queryset = queryset.exclude(status=UserStory.Status.BACKLOG)
    condition = Q(status__lte=UserStory.Status.IN_VALIDATION,
                  end_date__lt=date.today()) | Q(
                      status=UserStory.Status.COMPLETED,
                      end_date__lte=F("validated_changed"))
    delayed_count = queryset.filter(condition).count()
    not_delayed_count = queryset.exclude(condition).count()
    return [
        {
            "name": _("En fecha"),
            "color": "#4CAF50",
            "y": not_delayed_count
        },
        {
            "name": _("Retrasadas"),
            "color": "#FF9800",
            "y": delayed_count
        },
    ]
def test_validate_order_dates(user: User, services: QuerySet):
    """Should validate orders dates."""
    service = services.exclude(professional__user=user).first()
    now = arrow.utcnow()
    order = Order()
    with pytest.raises(ValidationError) as error:
        validate_order_dates(order)
    assert "is not set" in str(error)

    order.start_datetime = now.shift(hours=-2).datetime
    order.end_datetime = now.shift(hours=-1).datetime
    with pytest.raises(ValidationError) as error:
        validate_order_dates(order)
    assert "in the future" in str(error)

    order.start_datetime = now.shift(hours=1).datetime
    with pytest.raises(ValidationError) as error:
        validate_order_dates(order)
    assert "in the future" in str(error)

    order.end_datetime = now.shift(minutes=1).datetime
    with pytest.raises(ValidationError) as error:
        validate_order_dates(order)
    assert "is incorrect" in str(error)

    order.start_datetime = now.shift(hours=4).datetime
    order.end_datetime = now.shift(hours=5).datetime
    with pytest.raises(ValidationError) as error:
        validate_order_dates(order)
    assert "service or client is empty" in str(error)

    order.service = service
    order.client = user

    with pytest.raises(ValidationError) as error:
        validate_order_dates(order)
    assert "be a multiple" in str(error)

    order.service.duration = 60
    validate_order_dates(order)
    order.save()

    order.pk = None
    with pytest.raises(ValidationError) as error:
        validate_order_dates(order)
    assert "not overlap" in str(error)

    order.start_datetime = now.shift(hours=5).datetime
    order.end_datetime = now.shift(hours=15).datetime
    validate_order_dates(order)
    order.save()

    order.start_datetime = now.shift(hours=6).datetime
    validate_order_dates(order)
def test_availability_slot_save(services: QuerySet, professionals: QuerySet):
    """Should set a professional during saving."""
    service = services.first()
    slot = AvailabilitySlot()
    slot.service = service
    slot.professional = professionals.exclude(
        pk=service.professional.pk).first()
    slot.start_datetime = arrow.utcnow().datetime
    slot.end_datetime = arrow.utcnow().shift(days=1).datetime
    slot.save()

    assert slot.professional == service.professional
Exemple #21
0
def filter_by_department(queryset: QuerySet,
                         department: List[str]) -> QuerySet:
    if department is None or len(department) == 0:
        return queryset

    if "ALL" in department:
        return queryset
    elif "ETC" in department:
        return queryset.exclude(department__code__in=(set(MAJOR_ACRONYMS) -
                                                      set(department)))
    else:
        return queryset.filter(department__code__in=department)
Exemple #22
0
 def sort_by_payment(queryset: QuerySet, sort_by: SortInputObjectType) -> QuerySet:
     last_payments = (
         queryset.exclude(payments__isnull=True)
         .annotate(payment_id=Max("payments__pk"))
         .values_list("payment_id", flat=True)
     )
     queryset = queryset.annotate(
         last_payment=FilteredRelation(
             "payments", condition=Q(payments__pk__in=last_payments)
         )
     )
     return queryset.order_by(f"{sort_by.direction}last_payment__charge_status")
Exemple #23
0
def org_units_to_gpkg(queryset: QuerySet) -> bytes:
    """Export the provided org unit queryset in Geopackage (gpkg) format."""

    # create df with queryset, excluding entries without geo info
    queryset = queryset.exclude(
        Q(location=None) & Q(geom=None) & Q(simplified_geom=None))
    ou_df = pd.DataFrame(queryset.values(*ORG_UNIT_COLUMNS))

    # cleanup / transforms
    ou_df["parent"] = ou_df["parent__name"] + " (" + ou_df[
        "parent__org_unit_type__name"] + ")"
    ou_df["geography"] = ou_df["geom"].fillna(ou_df["simplified_geom"].fillna(
        ou_df["location"]))
    ou_df = ou_df.drop(columns=[
        "geom", "simplified_geom", "location", "parent__name",
        "parent__org_unit_type__name"
    ])
    ou_df = ou_df.rename(columns={
        "org_unit_type__name": "type",
        "org_unit_type__depth": "depth"
    })
    ou_df["depth"] = ou_df["depth"].fillna(999)
    ou_df = ou_df.set_index("uuid")

    # Convert django geometry values (GEOS) to shapely models
    ou_df["geography"] = ou_df["geography"].map(geos_to_shapely)

    # filter empty geometries
    ou_df["geography"] = ou_df["geography"].loc[ou_df["geography"].notnull()]

    # Convert to geo dataframe, and group by org unit type to handle multiple layers
    ou_gdf = gpd.GeoDataFrame(ou_df, geometry="geography")
    ou_gdf["group_key"] = ou_gdf["depth"].astype(str) + "-" + ou_gdf["type"]
    ou_gdf_by_type = ou_gdf.groupby("group_key")

    # Write to content file
    # Tried to use a mkstemp but it prevents the group.to_file from writing to it and is hard to remove later on
    # NamedTemporaryFile works but the handle cannot be used to read again. So left the plain uuid thing.
    path = os.path.join(tempfile.gettempdir(), str(uuid.uuid4()))

    i = 1
    for group_key, group in ou_gdf_by_type:
        group = group.drop(columns=["depth", "group_key"])
        layer = group_key.split("-", 1)[1]
        group.to_file(path, driver="GPKG", layer=layer)
        i = i + 1

    f = open(path, "rb")
    content = f.read()
    f.close()
    os.remove(path)
    return content
Exemple #24
0
 def apply_to_queryset(self, queryset: models.QuerySet, use_q=False):
     if self.n_children == 0:
         if not use_q:
             queryset = queryset.filter(**self.filters)
             if self.exclude:
                 queryset = queryset.exclude(**self.exclude)
             return queryset
         return self._q()
     return queryset.filter(
         reduce(lambda q, p: p | q, [self._q()] + [
             c.apply_to_queryset(queryset=queryset, use_q=True)
             for c in self.children
         ]))
Exemple #25
0
    def filter_boolean(queryset: QuerySet, request: Request, name_filter: str,
                       name_field: str):
        value = request.query_params.get(name_filter)
        if value is not None:
            if json.loads(
                    request.query_params.get(
                        '{name_filter}Exclude'.format(name_filter=name_filter),
                        'false')):
                queryset = queryset.exclude(**{name_field: json.loads(value)})
            else:
                queryset = queryset.filter(**{name_field: json.loads(value)})

        return queryset
Exemple #26
0
    def filter_boolean(queryset: QuerySet, request: Request, name_filter: str,
                       name_field: str):
        isActive = json.loads(request.query_params.get(name_filter, 'false'))
        if isActive == True:
            if json.loads(
                    request.query_params.get(
                        '{name_filter}Exclude'.format(name_filter=name_filter),
                        'false')):
                queryset = queryset.exclude(**{name_field: True})
            else:
                queryset = queryset.filter(**{name_field: True})

        return queryset
Exemple #27
0
def filter_users(request, users_obj: QuerySet) -> QuerySet:
    customer = request.GET.get('customer')
    if customer == 'Ingen':
        users_obj = users_obj.filter(customer=None)
    elif customer == 'Satt':
        users_obj = users_obj.exclude(customer=None)
    elif customer is not None:
        users_obj = users_obj.filter(customer__name=customer)

    has_employee = request.GET.get('employee')
    if has_employee == 'Ja':
        users_obj = users_obj.exclude(employee=None)
    elif has_employee == 'Nei':
        users_obj = users_obj.filter(employee=None).exclude(ad_object__employeeID=None)
    elif has_employee == 'Ukjent':
        users_obj = users_obj.filter(employee=None, ad_object__employeeID=None)

    has_student = request.GET.get('student')
    if has_student == 'Ja':
        users_obj = users_obj.exclude(student=None)
    elif has_student == 'Nei':
        users_obj = users_obj.filter(student=None)
    return users_obj
Exemple #28
0
    def queryset_train_data(
            queryset: QuerySet) -> Tuple[np.ndarray, np.ndarray]:
        embeddings = []
        subjects = []
        queryset = queryset.exclude(faces__embeddings_bytes__isnull=True)
        for subject in queryset.iterator():
            for face in subject.faces.all():
                embeddings.append(face.embeddings)
                subjects.append(subject.id)

        embeddings = np.array(embeddings, np.float32)
        subjects = np.array(subjects, np.int32)

        return embeddings, subjects
    def filter(self, query_objects: QuerySet) -> QuerySet:
        """Filter the reports according to the inclusive and exclusive fitlers.

        Args:
            query_objects: The reports to filter.
        Returns:
            The reports matching this report counter requirements.

        """
        if self.inclusive_filter:
            query_objects = query_objects.filter(self.inclusive_filter)
        if self.exclusive_filter:
            query_objects = query_objects.exclude(self.exclusive_filter)

        return query_objects
Exemple #30
0
 def apply_to_queryset(self, queryset: models.QuerySet, use_q=False):
     if self.n_children == 0:
         if not use_q:
             queryset = queryset.filter(**self.filters)
             if self.exclude:
                 queryset = queryset.exclude(**self.exclude)
             return queryset
         return self._q()
     if use_q:
         return self._q()
     queries = [self._q()] + [
         c.apply_to_queryset(queryset=queryset, use_q=True)
         for c in self.children
     ]
     return queryset.filter(reduce(operator.or_, queries))