1
0
Fork 0
mirror of https://gitlab.com/bramw/baserow.git synced 2025-04-03 04:35:31 +00:00

refactor: Clean up rating element code & fix linting errors

* Remove print statement from registry and reorder imports
* Fix imports ordering and unused imports in rating tests
* Order element and collection field type registrations alphabetically
* Clean up pytest parameters for rating elements
This commit is contained in:
Evren Ozkan 2025-01-07 14:12:36 +01:00 committed by peter_baserow
parent e57587a57c
commit 03dc4e13aa
12 changed files with 111 additions and 48 deletions
backend
web-frontend/modules
builder
database/components

View file

@ -183,14 +183,14 @@ class BuilderConfig(AppConfig):
ImageElementType,
InputTextElementType,
LinkElementType,
RatingElementType,
RatingInputElementType,
MenuElementType,
RecordSelectorElementType,
RepeatElementType,
SimpleContainerElementType,
TableElementType,
TextElementType,
RatingElementType,
RatingInputElementType,
)
from .elements.registries import element_type_registry
@ -301,9 +301,9 @@ class BuilderConfig(AppConfig):
ButtonCollectionFieldType,
ImageCollectionFieldType,
LinkCollectionFieldType,
RatingCollectionFieldType,
TagsCollectionFieldType,
TextCollectionFieldType,
RatingCollectionFieldType,
)
from .elements.registries import collection_field_type_registry

View file

@ -1,6 +1,7 @@
from typing import Any, Dict, Generator, TypedDict, Union
from django.core.validators import MinValueValidator, MaxValueValidator
from django.core.validators import MinValueValidator
from rest_framework import serializers
from baserow.contrib.builder.elements.element_types import NavigationElementManager

View file

@ -59,6 +59,8 @@ from baserow.contrib.builder.elements.models import (
MenuElement,
MenuItemElement,
NavigationElementMixin,
RatingElement,
RatingInputElement,
RecordSelectorElement,
RepeatElement,
SimpleContainerElement,
@ -66,8 +68,6 @@ from baserow.contrib.builder.elements.models import (
TextElement,
VerticalAlignments,
get_default_table_orientation,
RatingElement,
RatingInputElement,
)
from baserow.contrib.builder.elements.registries import (
ElementType,
@ -1236,9 +1236,9 @@ class RatingElementType(ElementType):
def get_pytest_params(self, pytest_data_fixture):
return {
"max_value": 5,
"value": 5,
"value": "5",
"color": "dark-orange",
"style": RatingElement.style.field.choices.STAR.value,
"style": "STAR",
}
@property
@ -1280,9 +1280,9 @@ class RatingInputElementType(InputElementType):
def get_pytest_params(self, pytest_data_fixture):
return {
"max_value": 5,
"value": 5,
"value": "5",
"color": "dark-orange",
"style": RatingInputElement.style.field.choices.STAR.value,
"style": "STAR",
"label": "",
"required": False,
}

View file

@ -1,10 +1,11 @@
# Generated by Django 5.0.9 on 2025-01-05 23:56
import baserow.core.formula.field
import django.core.validators
import django.db.models.deletion
from django.db import migrations, models
import baserow.core.formula.field
class Migration(migrations.Migration):
dependencies = [

View file

@ -781,7 +781,7 @@ class Registry(Generic[InstanceSubClass]):
)
self.registry[instance.type] = instance
print(f"Registered {self.name} {instance.type}")
instance.after_register()
def unregister(self, value: Union[str, InstanceSubClass]):

View file

@ -2,14 +2,15 @@
Test the RatingCollectionFieldType class.
"""
import pytest
from unittest.mock import patch
import pytest
from baserow.contrib.builder.elements.collection_field_types import (
RatingCollectionFieldType,
)
from baserow.contrib.builder.elements.models import RatingStyles
from baserow.core.formula.serializers import FormulaSerializerField
from baserow.contrib.builder.elements.registries import element_type_registry
from baserow.contrib.builder.pages.service import PageService
MODULE_PATH = "baserow.contrib.builder.elements.collection_field_types"
@ -18,6 +19,7 @@ def test_class_properties_are_set():
"""
Test that the properties of the class are correctly set.
"""
field_type = RatingCollectionFieldType()
assert field_type.type == "rating"
@ -46,6 +48,7 @@ def test_deserialize_property_returns_value_from_super_method(
Ensure that the value is returned by calling the parent class's
deserialize_property() method.
"""
mock_value = "5"
mock_super_deserialize.return_value = mock_value
value = "5"
@ -72,7 +75,73 @@ def test_deserialize_property_returns_value_from_super_method(
@pytest.mark.django_db
def test_import_export_rating_collection_field_type(data_fixture):
"""
Ensure that the RatingCollectionField's properties are exported correctly
Ensure that the RatingCollectionField's formulas are exported correctly
with the updated Data Sources.
"""
pass
user, _ = data_fixture.create_user_and_token()
page = data_fixture.create_builder_page(user=user)
table, fields, _ = data_fixture.build_table(
user=user,
columns=[
("Rating", "rating"),
],
rows=[
[3],
],
)
rating_field = fields[0]
data_source = data_fixture.create_builder_local_baserow_list_rows_data_source(
table=table, page=page
)
table_element = data_fixture.create_builder_table_element(
page=page,
data_source=data_source,
fields=[
{
"name": "Rating Field",
"type": "rating",
"config": {
"value": f"get('data_source.{data_source.id}.0.{rating_field.db_column}')",
"max_value": 5,
"style": "star",
"color": "",
},
},
],
)
# Create a duplicate page to get a new data source
duplicated_page = PageService().duplicate_page(user, page)
data_source2 = duplicated_page.datasource_set.first()
# Create ID mapping for the data sources
id_mapping = {"builder_data_sources": {data_source.id: data_source2.id}}
# Export the element
serialized = element_type_registry.get_by_model(table_element).export_serialized(
table_element
)
# Delete the element
table_element.delete()
# Import it back
imported_element = element_type_registry.get_by_model(
table_element
).import_serialized(
page,
serialized,
id_mapping,
None,
)
# The imported element should have the same field configuration
# with updated data source ID
imported_field = imported_element.fields.get(name="Rating Field")
assert imported_field.config == {
"value": f"get('data_source.{data_source2.id}.0.{rating_field.db_column}')",
"max_value": 5,
"style": "star",
"color": "",
}

View file

@ -1,21 +1,9 @@
import pytest
from django.core.validators import MinValueValidator, MaxValueValidator
from rest_framework.exceptions import ValidationError
from baserow.contrib.builder.data_sources.builder_dispatch_context import (
BuilderDispatchContext,
)
from baserow.contrib.builder.elements.element_types import (
RatingElementType,
RatingInputElementType,
)
from baserow.contrib.builder.elements.models import (
RatingElement,
RatingInputElement,
RatingStyles,
)
from baserow.contrib.builder.elements.registries import element_type_registry
from collections import defaultdict
import pytest
from baserow.contrib.builder.elements.models import RatingElement, RatingStyles
from baserow.contrib.builder.elements.registries import element_type_registry
from baserow.core.utils import MirrorDict

View file

@ -70,6 +70,15 @@ export default {
return this.formElementData?.value ?? this.resolvedValue
},
},
watch: {
resolvedValue: {
handler(newValue) {
if (this.editable && this.formElementData?.value === undefined) {
this.setFormData(newValue)
}
},
},
},
mounted() {
if (this.editable) {
this.setFormData(this.resolvedValue)
@ -82,15 +91,6 @@ export default {
}
},
},
watch: {
resolvedValue: {
handler(newValue) {
if (this.editable && this.formElementData?.value === undefined) {
this.setFormData(newValue)
}
},
},
},
}
</script>

View file

@ -22,8 +22,8 @@
required
>
<input
type="number"
v-model="values.max_value"
type="number"
:min="1"
:max="10"
:step="1"

View file

@ -26,8 +26,8 @@
:error-message="valueErrorMessage"
>
<InjectedFormulaInput
data-test-id="rating-form-value"
v-model="values.value"
data-test-id="rating-form-value"
:placeholder="
values.editable
? $t('generalForm.valuePlaceholder')
@ -45,8 +45,8 @@
required
>
<input
type="number"
v-model="values.max_value"
type="number"
:min="1"
:max="10"
:step="1"

View file

@ -2122,6 +2122,7 @@ export class RatingElementType extends ElementType {
static getType() {
return 'rating'
}
get name() {
return 'Rating'
}
@ -2174,6 +2175,7 @@ export class RatingElementType extends ElementType {
value: 0,
}
}
isValid(element, value) {
return value >= 0 && value <= element.max_value
}

View file

@ -8,7 +8,9 @@
]"
>
<i
v-for="index in props.readOnly && !props.showUnselectedInReadOnly ? props.value : props.maxValue"
v-for="index in props.readOnly && !props.showUnselectedInReadOnly
? props.value
: props.maxValue"
:key="index"
class="rating__star"
:class="{
@ -30,7 +32,7 @@ export default {
props: {
readOnly: {
type: Boolean,
required: true,
default: false,
},
value: {
required: true,