1
0
Fork 0
mirror of https://gitlab.com/bramw/baserow.git synced 2025-04-11 07:51:20 +00:00

Resolve "Refactor all the has_user calls that raises an exception for has_user(...raise_error=True)."

This commit is contained in:
Bram Wiepjes 2021-02-06 13:47:13 +00:00
parent ba1bc40d93
commit 18d5fa78ff
7 changed files with 35 additions and 103 deletions
backend/src/baserow
contrib/database
core
changelog.md

View file

@ -5,7 +5,6 @@ from django.db import connections
from django.db.utils import ProgrammingError, DataError
from django.conf import settings
from baserow.core.exceptions import UserNotInGroupError
from baserow.core.utils import extract_allowed, set_allowed_attrs
from baserow.contrib.database.db.schema import lenient_schema_editor
from baserow.contrib.database.views.handler import ViewHandler
@ -40,7 +39,6 @@ class FieldHandler:
if this is used the `field_model` parameter doesn't work anymore.
:type base_queryset: Queryset
:raises FieldDoesNotExist: When the field with the provided id does not exist.
:raises UserNotInGroupError: When the user does not belong to the field.
:return: The requested field instance of the provided id.
:rtype: Field
"""
@ -59,8 +57,7 @@ class FieldHandler:
raise FieldDoesNotExist(f'The field with id {field_id} does not exist.')
group = field.table.database.group
if not group.has_user(user):
raise UserNotInGroupError(user, group)
group.has_user(user, raise_error=True)
return field
@ -84,7 +81,6 @@ class FieldHandler:
:type do_schema_change: bool
:param kwargs: The field values that need to be set upon creation.
:type kwargs: object
:raises UserNotInGroupError: When the user does not belong to the related group.
:raises PrimaryFieldAlreadyExists: When we try to create a primary field,
but one already exists.
:return: The created field instance.
@ -92,8 +88,7 @@ class FieldHandler:
"""
group = table.database.group
if not group.has_user(user):
raise UserNotInGroupError(user, group)
group.has_user(user, raise_error=True)
# Because only one primary field per table can exist and we have to check if one
# already exists. If so the field cannot be created and an exception is raised.
@ -146,7 +141,6 @@ class FieldHandler:
:param kwargs: The field values that need to be updated
:type kwargs: object
:raises ValueError: When the provided field is not an instance of Field.
:raises UserNotInGroupError: When the user does not belong to the related group.
:raises CannotChangeFieldType: When the database server responds with an
error while trying to change the field type. This should rarely happen
because of the lenient schema editor, which replaces the value with null
@ -159,8 +153,7 @@ class FieldHandler:
raise ValueError('The field is not an instance of Field.')
group = field.table.database.group
if not group.has_user(user):
raise UserNotInGroupError(user, group)
group.has_user(user, raise_error=True)
old_field = deepcopy(field)
field_type = field_type_registry.get_by_model(field)
@ -278,7 +271,6 @@ class FieldHandler:
:param field: The field instance that needs to be deleted.
:type field: Field
:raises ValueError: When the provided field is not an instance of Field.
:raises UserNotInGroupError: When the user does not belong to the related group.
:raises CannotDeletePrimaryField: When we try to delete the primary field
which cannot be deleted.
"""
@ -287,8 +279,7 @@ class FieldHandler:
raise ValueError('The field is not an instance of Field')
group = field.table.database.group
if not group.has_user(user):
raise UserNotInGroupError(user, group)
group.has_user(user, raise_error=True)
if field.primary:
raise CannotDeletePrimaryField('Cannot delete the primary field of a '
@ -329,12 +320,10 @@ class FieldHandler:
:type field: Field
:param select_options: A list containing dicts with the desired select options.
:type select_options: list
:raises UserNotInGroupError: When the user does not belong to the related group.
"""
group = field.table.database.group
if not group.has_user(user):
raise UserNotInGroupError(user, group)
group.has_user(user, raise_error=True)
existing_select_options = field.select_options.all()

View file

@ -7,7 +7,6 @@ from django.db.models import Max, F, Q
from django.db.models.fields.related import ManyToManyField
from django.conf import settings
from baserow.core.exceptions import UserNotInGroupError
from baserow.contrib.database.fields.models import Field
from .exceptions import RowDoesNotExist
@ -155,7 +154,6 @@ class RowHandler:
:param model: If the correct model has already been generated it can be
provided so that it does not have to be generated for a second time.
:type model: Model
:raises UserNotInGroupError: When the user does not belong to the related group.
:raises RowDoesNotExist: When the row with the provided id does not exist.
:return: The requested row instance.
:rtype: Model
@ -165,8 +163,7 @@ class RowHandler:
model = table.get_model()
group = table.database.group
if not group.has_user(user):
raise UserNotInGroupError(user, group)
group.has_user(user, raise_error=True)
try:
row = model.objects.get(id=row_id)
@ -192,7 +189,6 @@ class RowHandler:
:param before: If provided the new row will be placed right before that row
instance.
:type before: Table
:raises UserNotInGroupError: When the user does not belong to the related group.
:return: The created row instance.
:rtype: Model
"""
@ -201,8 +197,7 @@ class RowHandler:
values = {}
group = table.database.group
if not group.has_user(user):
raise UserNotInGroupError(user, group)
group.has_user(user, raise_error=True)
if not model:
model = table.get_model()
@ -255,15 +250,13 @@ class RowHandler:
:param model: If the correct model has already been generated it can be
provided so that it does not have to be generated for a second time.
:type model: Model
:raises UserNotInGroupError: When the user does not belong to the related group.
:raises RowDoesNotExist: When the row with the provided id does not exist.
:return: The updated row instance.
:rtype: Model
"""
group = table.database.group
if not group.has_user(user):
raise UserNotInGroupError(user, group)
group.has_user(user, raise_error=True)
if not model:
model = table.get_model()
@ -302,13 +295,11 @@ class RowHandler:
:type table: Table
:param row_id: The id of the row that must be deleted.
:type row_id: int
:raises UserNotInGroupError: When the user does not belong to the related group.
:raises RowDoesNotExist: When the row with the provided id does not exist.
"""
group = table.database.group
if not group.has_user(user):
raise UserNotInGroupError(user, group)
group.has_user(user, raise_error=True)
model = table.get_model(field_ids=[])

View file

@ -1,7 +1,6 @@
from django.db import connections
from django.conf import settings
from baserow.core.exceptions import UserNotInGroupError
from baserow.core.utils import extract_allowed, set_allowed_attrs
from baserow.contrib.database.fields.models import TextField
from baserow.contrib.database.views.handler import ViewHandler
@ -31,7 +30,6 @@ class TableHandler:
object from. This can for example be used to do a `select_related`.
:type base_queryset: Queryset
:raises TableDoesNotExist: When the table with the provided id does not exist.
:raises UserNotInGroupError: When the user does not belong to the related group.
:return: The requested table of the provided id.
:rtype: Table
"""
@ -45,8 +43,7 @@ class TableHandler:
raise TableDoesNotExist(f'The table with id {table_id} doe not exist.')
group = table.database.group
if not group.has_user(user):
raise UserNotInGroupError(user, group)
group.has_user(user, raise_error=True)
return table
@ -71,13 +68,11 @@ class TableHandler:
:type first_row_header: bool
:param kwargs: The fields that need to be set upon creation.
:type kwargs: object
:raises UserNotInGroupError: When the user does not belong to the related group.
:return: The created table instance.
:rtype: Table
"""
if not database.group.has_user(user):
raise UserNotInGroupError(user, database.group)
database.group.has_user(user, raise_error=True)
if data is not None:
fields, data = self.normalize_initial_table_data(data, first_row_header)
@ -233,7 +228,6 @@ class TableHandler:
:param kwargs: The fields that need to be updated.
:type kwargs: object
:raises ValueError: When the provided table is not an instance of Table.
:raises UserNotInGroupError: When the user does not belong to the related group.
:return: The updated table instance.
:rtype: Table
"""
@ -241,8 +235,7 @@ class TableHandler:
if not isinstance(table, Table):
raise ValueError('The table is not an instance of Table')
if not table.database.group.has_user(user):
raise UserNotInGroupError(user, table.database.group)
table.database.group.has_user(user, raise_error=True)
table = set_allowed_attrs(kwargs, ['name'], table)
table.save()
@ -260,15 +253,12 @@ class TableHandler:
:param table: The table instance that needs to be deleted.
:type table: Table
:raises ValueError: When the provided table is not an instance of Table.
:raises UserNotInGroupError: When the user does not belong to the related group.
"""
if not isinstance(table, Table):
raise ValueError('The table is not an instance of Table')
if not table.database.group.has_user(user):
raise UserNotInGroupError(user, table.database.group)
table.database.group.has_user(user, raise_error=True)
table_id = table.id
# Delete the table schema from the database.

View file

@ -3,7 +3,6 @@ from django.utils import timezone
from rest_framework.request import Request
from baserow.core.exceptions import UserNotInGroupError
from baserow.core.utils import random_string
from baserow.contrib.database.models import Database, Table
from baserow.contrib.database.exceptions import DatabaseDoesNotBelongToGroup
@ -49,8 +48,6 @@ class TokenHandler:
:type base_queryset: Queryset
:raises TokenDoesNotExist: Raised when the requested token was not found or
if it does not belong to the user.
:raises UserNotInGroupError: When the user does not belong to the group
anymore.
:return: The fetched token.
:rtype: Token
"""
@ -67,8 +64,7 @@ class TokenHandler:
raise TokenDoesNotExist(f'The token with id {token_id} does not exist.')
group = token.group
if not group.has_user(user):
raise UserNotInGroupError(user, group)
group.has_user(user, raise_error=True)
return token
@ -112,13 +108,11 @@ class TokenHandler:
:type group: Group
:param name: The name of the token.
:type name: str
:raises UserNotInGroupError: If the user does not belong to the group.
:return: The created token instance.
:rtype: Token
"""
if not group.has_user(user):
raise UserNotInGroupError(user, group)
group.has_user(user, raise_error=True)
token = Token.objects.create(
name=name,

View file

@ -1,6 +1,5 @@
from django.db.models import Q, F
from baserow.core.exceptions import UserNotInGroupError
from baserow.core.utils import extract_allowed, set_allowed_attrs
from baserow.contrib.database.fields.registries import field_type_registry
from baserow.contrib.database.fields.models import Field
@ -41,7 +40,6 @@ class ViewHandler:
if this is used the `view_model` parameter doesn't work anymore.
:type base_queryset: Queryset
:raises ViewDoesNotExist: When the view with the provided id does not exist.
:raises UserNotInGroupError: When the user does not belong to the related group.
:type view_model: View
:return:
"""
@ -60,8 +58,7 @@ class ViewHandler:
raise ViewDoesNotExist(f'The view with id {view_id} does not exist.')
group = view.table.database.group
if not group.has_user(user):
raise UserNotInGroupError(user, group)
group.has_user(user, raise_error=True)
return view
@ -77,14 +74,12 @@ class ViewHandler:
:type type_name: str
:param kwargs: The fields that need to be set upon creation.
:type kwargs: object
:raises UserNotInGroupError: When the user does not belong to the related group.
:return: The created view instance.
:rtype: View
"""
group = table.database.group
if not group.has_user(user):
raise UserNotInGroupError(user, group)
group.has_user(user, raise_error=True)
# Figure out which model to use for the given view type.
view_type = view_type_registry.get(type_name)
@ -116,7 +111,6 @@ class ViewHandler:
:param kwargs: The fields that need to be updated.
:type kwargs: object
:raises ValueError: When the provided view not an instance of View.
:raises UserNotInGroupError: When the user does not belong to the related group.
:return: The updated view instance.
:rtype: View
"""
@ -125,8 +119,7 @@ class ViewHandler:
raise ValueError('The view is not an instance of View.')
group = view.table.database.group
if not group.has_user(user):
raise UserNotInGroupError(user, group)
group.has_user(user, raise_error=True)
view_type = view_type_registry.get_by_model(view)
allowed_fields = [
@ -150,15 +143,13 @@ class ViewHandler:
:param view: The view instance that needs to be deleted.
:type view: View
:raises ViewDoesNotExist: When the view with the provided id does not exist.
:raises UserNotInGroupError: When the user does not belong to the related group.
"""
if not isinstance(view, View):
raise ValueError('The view is not an instance of View')
group = view.table.database.group
if not group.has_user(user):
raise UserNotInGroupError(user, group)
group.has_user(user, raise_error=True)
view_id = view.id
view.delete()
@ -289,7 +280,6 @@ class ViewHandler:
object. This can for example be used to do a `select_related`.
:type base_queryset: Queryset
:raises ViewFilterDoesNotExist: The the requested view does not exists.
:raises UserNotInGroupError: When the user does not belong to the related group.
:return: The requested view filter instance.
:type: ViewFilter
"""
@ -309,8 +299,7 @@ class ViewHandler:
)
group = view_filter.view.table.database.group
if not group.has_user(user):
raise UserNotInGroupError(user, group)
group.has_user(user, raise_error=True)
return view_filter
@ -330,7 +319,6 @@ class ViewHandler:
:type type_name: str
:param value: The value that the filter must apply to.
:type value: str
:raises UserNotInGroupError: When the user does not belong to the related group.
:raises ViewFilterNotSupported: When the provided view does not support
filtering.
:raises ViewFilterTypeNotAllowedForField: When the field does not support the
@ -342,8 +330,7 @@ class ViewHandler:
"""
group = view.table.database.group
if not group.has_user(user):
raise UserNotInGroupError(user, group)
group.has_user(user, raise_error=True)
# Check if view supports filtering
view_type = view_type_registry.get_by_model(view.specific_class)
@ -388,7 +375,6 @@ class ViewHandler:
:param kwargs: The values that need to be updated, allowed values are
`field`, `value` and `type_name`.
:type kwargs: dict
:raises UserNotInGroupError: When the user does not belong to the related group.
:raises ViewFilterTypeNotAllowedForField: When the field does not supports the
filter type.
:raises FieldNotInTable: When the provided field does not belong to the
@ -398,8 +384,7 @@ class ViewHandler:
"""
group = view_filter.view.table.database.group
if not group.has_user(user):
raise UserNotInGroupError(user, group)
group.has_user(user, raise_error=True)
type_name = kwargs.get('type_name', view_filter.type)
field = kwargs.get('field', view_filter.field)
@ -439,12 +424,10 @@ class ViewHandler:
:type user: User
:param view_filter: The view filter instance that needs to be deleted.
:type view_filter: ViewFilter
:raises UserNotInGroupError: When the user does not belong to the related group.
"""
group = view_filter.view.table.database.group
if not group.has_user(user):
raise UserNotInGroupError(user, group)
group.has_user(user, raise_error=True)
view_filter_id = view_filter.id
view_filter.delete()
@ -530,7 +513,6 @@ class ViewHandler:
object from. This can for example be used to do a `select_related`.
:type base_queryset: Queryset
:raises ViewSortDoesNotExist: The the requested view does not exists.
:raises UserNotInGroupError: When the user does not belong to the related group.
:return: The requested view sort instance.
:type: ViewSort
"""
@ -550,8 +532,7 @@ class ViewHandler:
)
group = view_sort.view.table.database.group
if not group.has_user(user):
raise UserNotInGroupError(user, group)
group.has_user(user, raise_error=True)
return view_sort
@ -568,7 +549,6 @@ class ViewHandler:
:param order: The desired order, can either be ascending (A to Z) or
descending (Z to A).
:type order: str
:raises UserNotInGroupError: When the user does not belong to the related group.
:raises ViewSortNotSupported: When the provided view does not support sorting.
:raises FieldNotInTable: When the provided field does not belong to the
provided view's table.
@ -577,8 +557,7 @@ class ViewHandler:
"""
group = view.table.database.group
if not group.has_user(user):
raise UserNotInGroupError(user, group)
group.has_user(user, raise_error=True)
# Check if view supports sorting.
view_type = view_type_registry.get_by_model(view.specific_class)
@ -624,15 +603,13 @@ class ViewHandler:
:param kwargs: The values that need to be updated, allowed values are
`field` and `order`.
:type kwargs: dict
:raises UserNotInGroupError: When the user does not belong to the related group.
:raises FieldNotInTable: When the field does not support sorting.
:return: The updated view sort instance.
:rtype: ViewSort
"""
group = view_sort.view.table.database.group
if not group.has_user(user):
raise UserNotInGroupError(user, group)
group.has_user(user, raise_error=True)
field = kwargs.get('field', view_sort.field)
order = kwargs.get('order', view_sort.order)
@ -680,12 +657,10 @@ class ViewHandler:
:type user: User
:param view_sort: The view sort instance that needs to be deleted.
:type view_sort: ViewSort
:raises UserNotInGroupError: When the user does not belong to the related group.
"""
group = view_sort.view.table.database.group
if not group.has_user(user):
raise UserNotInGroupError(user, group)
group.has_user(user, raise_error=True)
view_sort_id = view_sort.id
view_sort.delete()

View file

@ -11,8 +11,8 @@ from .models import (
)
from .exceptions import (
GroupDoesNotExist, ApplicationDoesNotExist, BaseURLHostnameNotAllowed,
UserNotInGroupError, GroupInvitationEmailMismatch, GroupInvitationDoesNotExist,
GroupUserDoesNotExist, GroupUserAlreadyExists
GroupInvitationEmailMismatch, GroupInvitationDoesNotExist, GroupUserDoesNotExist,
GroupUserAlreadyExists
)
from .utils import extract_allowed, set_allowed_attrs
from .registries import application_type_registry
@ -34,7 +34,6 @@ class CoreHandler:
object. This can for example be used to do a `prefetch_related`.
:type base_queryset: Queryset
:raises GroupDoesNotExist: When the group with the provided id does not exist.
:raises UserNotInGroupError: When the user does not belong to the group.
:return: The requested group instance of the provided id.
:rtype: Group
"""
@ -500,7 +499,6 @@ class CoreHandler:
:type base_queryset: Queryset
:raises ApplicationDoesNotExist: When the application with the provided id
does not exist.
:raises UserNotInGroupError: When the user does not belong to the group.
:return: The requested application instance of the provided id.
:rtype: Application
"""
@ -517,8 +515,7 @@ class CoreHandler:
f'The application with id {application_id} does not exist.'
)
if not application.group.has_user(user):
raise UserNotInGroupError(user, application.group)
application.group.has_user(user, raise_error=True)
return application
@ -535,13 +532,11 @@ class CoreHandler:
:type type_name: str
:param kwargs: The fields that need to be set upon creation.
:type kwargs: object
:raises UserNotInGroupError: When the user does not belong to the related group.
:return: The created application instance.
:rtype: Application
"""
if not group.has_user(user):
raise UserNotInGroupError(user, group)
group.has_user(user, raise_error=True)
# Figure out which model is used for the given application type.
application_type = application_type_registry.get(type_name)
@ -568,7 +563,6 @@ class CoreHandler:
:param kwargs: The fields that need to be updated.
:type kwargs: object
:raises ValueError: If one of the provided parameters is invalid.
:raises UserNotInGroupError: When the user does not belong to the related group.
:return: The updated application instance.
:rtype: Application
"""
@ -576,8 +570,7 @@ class CoreHandler:
if not isinstance(application, Application):
raise ValueError('The application is not an instance of Application.')
if not application.group.has_user(user):
raise UserNotInGroupError(user, application.group)
application.group.has_user(user, raise_error=True)
application = set_allowed_attrs(kwargs, ['name'], application)
application.save()
@ -595,14 +588,12 @@ class CoreHandler:
:param application: The application instance that needs to be deleted.
:type application: Application
:raises ValueError: If one of the provided parameters is invalid.
:raises UserNotInGroupError: When the user does not belong to the related group.
"""
if not isinstance(application, Application):
raise ValueError('The application is not an instance of Application')
if not application.group.has_user(user):
raise UserNotInGroupError(user, application.group)
application.group.has_user(user, raise_error=True)
application_id = application.id
application = application.specific

View file

@ -5,6 +5,8 @@
* Added single select field form option validation.
* Changed all cookies to SameSite=lax.
* Fixed the "Ignored attempt to cancel a touchmove" error.
* Refactored the has_user everywhere such that the raise_error argument is used when
possible.
## Released (2021-02-04)