mirror of
https://gitlab.com/bramw/baserow.git
synced 2025-04-10 07:37:30 +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:
parent
e57587a57c
commit
03dc4e13aa
12 changed files with 111 additions and 48 deletions
backend
web-frontend/modules
|
@ -183,14 +183,14 @@ class BuilderConfig(AppConfig):
|
||||||
ImageElementType,
|
ImageElementType,
|
||||||
InputTextElementType,
|
InputTextElementType,
|
||||||
LinkElementType,
|
LinkElementType,
|
||||||
|
RatingElementType,
|
||||||
|
RatingInputElementType,
|
||||||
MenuElementType,
|
MenuElementType,
|
||||||
RecordSelectorElementType,
|
RecordSelectorElementType,
|
||||||
RepeatElementType,
|
RepeatElementType,
|
||||||
SimpleContainerElementType,
|
SimpleContainerElementType,
|
||||||
TableElementType,
|
TableElementType,
|
||||||
TextElementType,
|
TextElementType,
|
||||||
RatingElementType,
|
|
||||||
RatingInputElementType,
|
|
||||||
)
|
)
|
||||||
from .elements.registries import element_type_registry
|
from .elements.registries import element_type_registry
|
||||||
|
|
||||||
|
@ -301,9 +301,9 @@ class BuilderConfig(AppConfig):
|
||||||
ButtonCollectionFieldType,
|
ButtonCollectionFieldType,
|
||||||
ImageCollectionFieldType,
|
ImageCollectionFieldType,
|
||||||
LinkCollectionFieldType,
|
LinkCollectionFieldType,
|
||||||
|
RatingCollectionFieldType,
|
||||||
TagsCollectionFieldType,
|
TagsCollectionFieldType,
|
||||||
TextCollectionFieldType,
|
TextCollectionFieldType,
|
||||||
RatingCollectionFieldType,
|
|
||||||
)
|
)
|
||||||
from .elements.registries import collection_field_type_registry
|
from .elements.registries import collection_field_type_registry
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
from typing import Any, Dict, Generator, TypedDict, Union
|
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 rest_framework import serializers
|
||||||
|
|
||||||
from baserow.contrib.builder.elements.element_types import NavigationElementManager
|
from baserow.contrib.builder.elements.element_types import NavigationElementManager
|
||||||
|
|
|
@ -59,6 +59,8 @@ from baserow.contrib.builder.elements.models import (
|
||||||
MenuElement,
|
MenuElement,
|
||||||
MenuItemElement,
|
MenuItemElement,
|
||||||
NavigationElementMixin,
|
NavigationElementMixin,
|
||||||
|
RatingElement,
|
||||||
|
RatingInputElement,
|
||||||
RecordSelectorElement,
|
RecordSelectorElement,
|
||||||
RepeatElement,
|
RepeatElement,
|
||||||
SimpleContainerElement,
|
SimpleContainerElement,
|
||||||
|
@ -66,8 +68,6 @@ from baserow.contrib.builder.elements.models import (
|
||||||
TextElement,
|
TextElement,
|
||||||
VerticalAlignments,
|
VerticalAlignments,
|
||||||
get_default_table_orientation,
|
get_default_table_orientation,
|
||||||
RatingElement,
|
|
||||||
RatingInputElement,
|
|
||||||
)
|
)
|
||||||
from baserow.contrib.builder.elements.registries import (
|
from baserow.contrib.builder.elements.registries import (
|
||||||
ElementType,
|
ElementType,
|
||||||
|
@ -1236,9 +1236,9 @@ class RatingElementType(ElementType):
|
||||||
def get_pytest_params(self, pytest_data_fixture):
|
def get_pytest_params(self, pytest_data_fixture):
|
||||||
return {
|
return {
|
||||||
"max_value": 5,
|
"max_value": 5,
|
||||||
"value": 5,
|
"value": "5",
|
||||||
"color": "dark-orange",
|
"color": "dark-orange",
|
||||||
"style": RatingElement.style.field.choices.STAR.value,
|
"style": "STAR",
|
||||||
}
|
}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -1280,9 +1280,9 @@ class RatingInputElementType(InputElementType):
|
||||||
def get_pytest_params(self, pytest_data_fixture):
|
def get_pytest_params(self, pytest_data_fixture):
|
||||||
return {
|
return {
|
||||||
"max_value": 5,
|
"max_value": 5,
|
||||||
"value": 5,
|
"value": "5",
|
||||||
"color": "dark-orange",
|
"color": "dark-orange",
|
||||||
"style": RatingInputElement.style.field.choices.STAR.value,
|
"style": "STAR",
|
||||||
"label": "",
|
"label": "",
|
||||||
"required": False,
|
"required": False,
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
# Generated by Django 5.0.9 on 2025-01-05 23:56
|
# Generated by Django 5.0.9 on 2025-01-05 23:56
|
||||||
|
|
||||||
import baserow.core.formula.field
|
|
||||||
import django.core.validators
|
import django.core.validators
|
||||||
import django.db.models.deletion
|
import django.db.models.deletion
|
||||||
from django.db import migrations, models
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
import baserow.core.formula.field
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
|
|
@ -781,7 +781,7 @@ class Registry(Generic[InstanceSubClass]):
|
||||||
)
|
)
|
||||||
|
|
||||||
self.registry[instance.type] = instance
|
self.registry[instance.type] = instance
|
||||||
print(f"Registered {self.name} {instance.type}")
|
|
||||||
instance.after_register()
|
instance.after_register()
|
||||||
|
|
||||||
def unregister(self, value: Union[str, InstanceSubClass]):
|
def unregister(self, value: Union[str, InstanceSubClass]):
|
||||||
|
|
|
@ -2,14 +2,15 @@
|
||||||
Test the RatingCollectionFieldType class.
|
Test the RatingCollectionFieldType class.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import pytest
|
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
from baserow.contrib.builder.elements.collection_field_types import (
|
from baserow.contrib.builder.elements.collection_field_types import (
|
||||||
RatingCollectionFieldType,
|
RatingCollectionFieldType,
|
||||||
)
|
)
|
||||||
from baserow.contrib.builder.elements.models import RatingStyles
|
from baserow.contrib.builder.elements.registries import element_type_registry
|
||||||
from baserow.core.formula.serializers import FormulaSerializerField
|
from baserow.contrib.builder.pages.service import PageService
|
||||||
|
|
||||||
MODULE_PATH = "baserow.contrib.builder.elements.collection_field_types"
|
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.
|
Test that the properties of the class are correctly set.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
field_type = RatingCollectionFieldType()
|
field_type = RatingCollectionFieldType()
|
||||||
|
|
||||||
assert field_type.type == "rating"
|
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
|
Ensure that the value is returned by calling the parent class's
|
||||||
deserialize_property() method.
|
deserialize_property() method.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
mock_value = "5"
|
mock_value = "5"
|
||||||
mock_super_deserialize.return_value = mock_value
|
mock_super_deserialize.return_value = mock_value
|
||||||
value = "5"
|
value = "5"
|
||||||
|
@ -72,7 +75,73 @@ def test_deserialize_property_returns_value_from_super_method(
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_import_export_rating_collection_field_type(data_fixture):
|
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.
|
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": "",
|
||||||
|
}
|
||||||
|
|
|
@ -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
|
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
|
from baserow.core.utils import MirrorDict
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -70,6 +70,15 @@ export default {
|
||||||
return this.formElementData?.value ?? this.resolvedValue
|
return this.formElementData?.value ?? this.resolvedValue
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
watch: {
|
||||||
|
resolvedValue: {
|
||||||
|
handler(newValue) {
|
||||||
|
if (this.editable && this.formElementData?.value === undefined) {
|
||||||
|
this.setFormData(newValue)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
if (this.editable) {
|
if (this.editable) {
|
||||||
this.setFormData(this.resolvedValue)
|
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>
|
</script>
|
||||||
|
|
||||||
|
|
|
@ -22,8 +22,8 @@
|
||||||
required
|
required
|
||||||
>
|
>
|
||||||
<input
|
<input
|
||||||
type="number"
|
|
||||||
v-model="values.max_value"
|
v-model="values.max_value"
|
||||||
|
type="number"
|
||||||
:min="1"
|
:min="1"
|
||||||
:max="10"
|
:max="10"
|
||||||
:step="1"
|
:step="1"
|
||||||
|
|
|
@ -26,8 +26,8 @@
|
||||||
:error-message="valueErrorMessage"
|
:error-message="valueErrorMessage"
|
||||||
>
|
>
|
||||||
<InjectedFormulaInput
|
<InjectedFormulaInput
|
||||||
data-test-id="rating-form-value"
|
|
||||||
v-model="values.value"
|
v-model="values.value"
|
||||||
|
data-test-id="rating-form-value"
|
||||||
:placeholder="
|
:placeholder="
|
||||||
values.editable
|
values.editable
|
||||||
? $t('generalForm.valuePlaceholder')
|
? $t('generalForm.valuePlaceholder')
|
||||||
|
@ -45,8 +45,8 @@
|
||||||
required
|
required
|
||||||
>
|
>
|
||||||
<input
|
<input
|
||||||
type="number"
|
|
||||||
v-model="values.max_value"
|
v-model="values.max_value"
|
||||||
|
type="number"
|
||||||
:min="1"
|
:min="1"
|
||||||
:max="10"
|
:max="10"
|
||||||
:step="1"
|
:step="1"
|
||||||
|
|
|
@ -2122,6 +2122,7 @@ export class RatingElementType extends ElementType {
|
||||||
static getType() {
|
static getType() {
|
||||||
return 'rating'
|
return 'rating'
|
||||||
}
|
}
|
||||||
|
|
||||||
get name() {
|
get name() {
|
||||||
return 'Rating'
|
return 'Rating'
|
||||||
}
|
}
|
||||||
|
@ -2174,6 +2175,7 @@ export class RatingElementType extends ElementType {
|
||||||
value: 0,
|
value: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
isValid(element, value) {
|
isValid(element, value) {
|
||||||
return value >= 0 && value <= element.max_value
|
return value >= 0 && value <= element.max_value
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,9 @@
|
||||||
]"
|
]"
|
||||||
>
|
>
|
||||||
<i
|
<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"
|
:key="index"
|
||||||
class="rating__star"
|
class="rating__star"
|
||||||
:class="{
|
:class="{
|
||||||
|
@ -30,7 +32,7 @@ export default {
|
||||||
props: {
|
props: {
|
||||||
readOnly: {
|
readOnly: {
|
||||||
type: Boolean,
|
type: Boolean,
|
||||||
required: true,
|
default: false,
|
||||||
},
|
},
|
||||||
value: {
|
value: {
|
||||||
required: true,
|
required: true,
|
||||||
|
|
Loading…
Add table
Reference in a new issue