fix docstrings 3

This commit is contained in:
Matthias 2022-05-28 02:27:28 +02:00
parent 80510e8ba2
commit cb6fd63343
No known key found for this signature in database
GPG Key ID: AB6D0E6C4CB65093
8 changed files with 37 additions and 83 deletions

View File

@ -6,10 +6,7 @@ import sys
def calculate_coverage(filename): def calculate_coverage(filename):
""" """Calculate translation coverage for a .po file"""
Calculate translation coverage for a .po file
"""
with open(filename, 'r') as f: with open(filename, 'r') as f:
lines = f.readlines() lines = f.readlines()

View File

@ -212,9 +212,7 @@ class StockLocationList(generics.ListCreateAPIView):
serializer_class = StockSerializers.LocationSerializer serializer_class = StockSerializers.LocationSerializer
def filter_queryset(self, queryset): def filter_queryset(self, queryset):
"""Custom filtering: """Custom filtering: - Allow filtering by "null" parent to retrieve top-level stock locations"""
- Allow filtering by "null" parent to retrieve top-level stock locations
"""
queryset = super().filter_queryset(queryset) queryset = super().filter_queryset(queryset)
params = self.request.query_params params = self.request.query_params
@ -397,7 +395,9 @@ class StockFilter(rest_filters.FilterSet):
tracked = rest_filters.BooleanFilter(label='Tracked', method='filter_tracked') tracked = rest_filters.BooleanFilter(label='Tracked', method='filter_tracked')
def filter_tracked(self, queryset, name, value): def filter_tracked(self, queryset, name, value):
"""Filter by whether this stock item is *tracked*, meaning either: """Filter by whether this stock item is *tracked*.
Meaning either:
- It has a serial number - It has a serial number
- It has a batch code - It has a batch code
""" """
@ -616,8 +616,7 @@ class StockList(APIDownloadMixin, generics.ListCreateAPIView):
return DownloadFile(filedata, filename) return DownloadFile(filedata, filename)
def list(self, request, *args, **kwargs): def list(self, request, *args, **kwargs):
"""Override the 'list' method, as the StockLocation objects """Override the 'list' method, as the StockLocation objects are very expensive to serialize.
are very expensive to serialize.
So, we fetch and serialize the required StockLocation objects only as required. So, we fetch and serialize the required StockLocation objects only as required.
""" """
@ -731,7 +730,6 @@ class StockList(APIDownloadMixin, generics.ListCreateAPIView):
def filter_queryset(self, queryset): def filter_queryset(self, queryset):
"""Custom filtering for the StockItem queryset""" """Custom filtering for the StockItem queryset"""
params = self.request.query_params params = self.request.query_params
queryset = super().filter_queryset(queryset) queryset = super().filter_queryset(queryset)
@ -1155,7 +1153,6 @@ class StockItemTestResultList(generics.ListCreateAPIView):
Also, check if an attachment was uploaded alongside the test result, Also, check if an attachment was uploaded alongside the test result,
and save it to the database if it were. and save it to the database if it were.
""" """
# Capture the user information # Capture the user information
test_result = serializer.save() test_result = serializer.save()
test_result.user = self.request.user test_result.user = self.request.user
@ -1267,7 +1264,6 @@ class StockTrackingList(generics.ListAPIView):
Here we override the default 'create' implementation, Here we override the default 'create' implementation,
to save the user information associated with the request object. to save the user information associated with the request object.
""" """
serializer = self.get_serializer(data=request.data) serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True) serializer.is_valid(raise_exception=True)

View File

@ -46,6 +46,7 @@ class StockLocation(MetadataMixin, InvenTreeTree):
def delete(self, *args, **kwargs): def delete(self, *args, **kwargs):
"""Custom model deletion routine, which updates any child locations or items. """Custom model deletion routine, which updates any child locations or items.
This must be handled within a transaction.atomic(), otherwise the tree structure is damaged This must be handled within a transaction.atomic(), otherwise the tree structure is damaged
""" """
with transaction.atomic(): with transaction.atomic():
@ -161,7 +162,8 @@ class StockLocation(MetadataMixin, InvenTreeTree):
def item_count(self): def item_count(self):
"""Simply returns the number of stock items in this location. """Simply returns the number of stock items in this location.
Required for tree view serializer.""" Required for tree view serializer.
"""
return self.stock_item_count() return self.stock_item_count()
@ -248,10 +250,7 @@ class StockItem(MetadataMixin, MPTTModel):
return reverse('api-stock-list') return reverse('api-stock-list')
def api_instance_filters(self): def api_instance_filters(self):
""" """Custom API instance filters"""
Custom API instance filters
"""
return { return {
'parent': { 'parent': {
'exclude_tree': self.pk, 'exclude_tree': self.pk,
@ -538,6 +537,7 @@ class StockItem(MetadataMixin, MPTTModel):
def format_barcode(self, **kwargs): def format_barcode(self, **kwargs):
"""Return a JSON string for formatting a barcode for this StockItem. """Return a JSON string for formatting a barcode for this StockItem.
Can be used to perform lookup of a stockitem using barcode Can be used to perform lookup of a stockitem using barcode
Contains the following data: Contains the following data:
@ -1025,7 +1025,7 @@ class StockItem(MetadataMixin, MPTTModel):
def installStockItem(self, other_item, quantity, user, notes): def installStockItem(self, other_item, quantity, user, notes):
"""Install another stock item into this stock item. """Install another stock item into this stock item.
Args Args:
other_item: The stock item to install into this stock item other_item: The stock item to install into this stock item
quantity: The quantity of stock to install quantity: The quantity of stock to install
user: The user performing the operation user: The user performing the operation
@ -1117,7 +1117,8 @@ class StockItem(MetadataMixin, MPTTModel):
def child_count(self): def child_count(self):
"""Return the number of 'child' items associated with this StockItem. """Return the number of 'child' items associated with this StockItem.
A child item is one which has been split from this one.""" A child item is one which has been split from this one.
"""
return self.children.count() return self.children.count()
@property @property
@ -1581,6 +1582,7 @@ class StockItem(MetadataMixin, MPTTModel):
@transaction.atomic @transaction.atomic
def stocktake(self, count, user, notes=''): def stocktake(self, count, user, notes=''):
"""Perform item stocktake. """Perform item stocktake.
When the quantity of an item is counted, When the quantity of an item is counted,
record the date of stocktake record the date of stocktake
""" """
@ -1611,6 +1613,7 @@ class StockItem(MetadataMixin, MPTTModel):
@transaction.atomic @transaction.atomic
def add_stock(self, quantity, user, notes=''): def add_stock(self, quantity, user, notes=''):
"""Add items to stock """Add items to stock
This function can be called by initiating a ProjectRun, This function can be called by initiating a ProjectRun,
or by manually adding the items to the stock location or by manually adding the items to the stock location
""" """
@ -1769,7 +1772,7 @@ class StockItem(MetadataMixin, MPTTModel):
def requiredTestStatus(self): def requiredTestStatus(self):
"""Return the status of the tests required for this StockItem. """Return the status of the tests required for this StockItem.
return: Return:
A dict containing the following items: A dict containing the following items:
- total: Number of required tests - total: Number of required tests
- passed: Number of tests that have passed - passed: Number of tests that have passed

View File

@ -325,9 +325,7 @@ class StockItemTest(StockAPITestCase):
StockLocation.objects.create(name='C', description='location c', parent=top) StockLocation.objects.create(name='C', description='location c', parent=top)
def test_create_default_location(self): def test_create_default_location(self):
"""Test the default location functionality, """Test the default location functionality, if a 'location' is not specified in the creation request."""
if a 'location' is not specified in the creation request.
"""
# The part 'R_4K7_0603' (pk=4) has a default location specified # The part 'R_4K7_0603' (pk=4) has a default location specified
response = self.client.post( response = self.client.post(

View File

@ -212,7 +212,6 @@ class StockItemConvert(AjaxUpdateView):
def get_form(self): def get_form(self):
"""Filter the available parts.""" """Filter the available parts."""
form = super().get_form() form = super().get_form()
item = self.get_object() item = self.get_object()

View File

@ -30,8 +30,7 @@ class RuleSetInline(admin.TabularInline):
class InvenTreeGroupAdminForm(forms.ModelForm): class InvenTreeGroupAdminForm(forms.ModelForm):
""" """Custom admin form for the Group model.
Custom admin form for the Group model.
Adds the ability for editing user membership directly in the group admin page. Adds the ability for editing user membership directly in the group admin page.
""" """
@ -86,8 +85,7 @@ class RoleGroupAdmin(admin.ModelAdmin): # pragma: no cover
'stock_item', 'build', 'purchase_order', 'sales_order') 'stock_item', 'build', 'purchase_order', 'sales_order')
def get_rule_set(self, obj, rule_set_type): def get_rule_set(self, obj, rule_set_type):
''' Return list of permissions for the given ruleset ''' """Return list of permissions for the given ruleset"""
# Get all rulesets associated to object # Get all rulesets associated to object
rule_sets = RuleSet.objects.filter(group=obj.pk) rule_sets = RuleSet.objects.filter(group=obj.pk)
@ -156,12 +154,10 @@ class RoleGroupAdmin(admin.ModelAdmin): # pragma: no cover
filter_horizontal = ['permissions'] filter_horizontal = ['permissions']
def save_model(self, request, obj, form, change): def save_model(self, request, obj, form, change):
""" """This method serves two purposes:
This method serves two purposes:
- show warning message whenever the group users belong to multiple groups - show warning message whenever the group users belong to multiple groups
- skip saving of the group instance model as inlines needs to be saved before. - skip saving of the group instance model as inlines needs to be saved before.
""" """
# Get form cleaned data # Get form cleaned data
users = form.cleaned_data['users'] users = form.cleaned_data['users']
@ -189,8 +185,7 @@ class RoleGroupAdmin(admin.ModelAdmin): # pragma: no cover
class InvenTreeUserAdmin(UserAdmin): class InvenTreeUserAdmin(UserAdmin):
""" """Custom admin page for the User model.
Custom admin page for the User model.
Hides the "permissions" view as this is now handled Hides the "permissions" view as this is now handled
entirely by groups and RuleSets. entirely by groups and RuleSets.

View File

@ -20,8 +20,7 @@ class OwnerList(generics.ListAPIView):
serializer_class = OwnerSerializer serializer_class = OwnerSerializer
def filter_queryset(self, queryset): def filter_queryset(self, queryset):
""" """Implement text search for the "owner" model.
Implement text search for the "owner" model.
Note that an "owner" can be either a group, or a user, Note that an "owner" can be either a group, or a user,
so we cannot do a direct text search. so we cannot do a direct text search.
@ -59,8 +58,7 @@ class OwnerDetail(generics.RetrieveAPIView):
class RoleDetails(APIView): class RoleDetails(APIView):
""" """API endpoint which lists the available role permissions
API endpoint which lists the available role permissions
for the current user for the current user
(Requires authentication) (Requires authentication)

View File

@ -20,8 +20,7 @@ logger = logging.getLogger("inventree")
class RuleSet(models.Model): class RuleSet(models.Model):
""" """A RuleSet is somewhat like a superset of the django permission class,
A RuleSet is somewhat like a superset of the django permission class,
in that in encapsulates a bunch of permissions. in that in encapsulates a bunch of permissions.
There are *many* apps models used within InvenTree, There are *many* apps models used within InvenTree,
@ -222,7 +221,6 @@ class RuleSet(models.Model):
@classmethod @classmethod
def check_table_permission(cls, user, table, permission): def check_table_permission(cls, user, table, permission):
"""Check if the provided user has the specified permission against the table""" """Check if the provided user has the specified permission against the table"""
# If the table does *not* require permissions # If the table does *not* require permissions
if table in cls.RULESET_IGNORE: if table in cls.RULESET_IGNORE:
return True return True
@ -253,11 +251,7 @@ class RuleSet(models.Model):
@staticmethod @staticmethod
def get_model_permission_string(model, permission): def get_model_permission_string(model, permission):
""" """Construct the correctly formatted permission string, given the app_model name, and the permission type."""
Construct the correctly formatted permission string,
given the app_model name, and the permission type.
"""
model, app = split_model(model) model, app = split_model(model)
return "{app}.{perm}_{model}".format( return "{app}.{perm}_{model}".format(
@ -295,7 +289,6 @@ class RuleSet(models.Model):
def get_models(self): def get_models(self):
"""Return the database tables / models that this ruleset covers.""" """Return the database tables / models that this ruleset covers."""
return self.RULESET_MODELS.get(self.name, []) return self.RULESET_MODELS.get(self.name, [])
@ -324,8 +317,7 @@ def split_permission(app, perm):
def update_group_roles(group, debug=False): def update_group_roles(group, debug=False):
""" """Iterates through all of the RuleSets associated with the group,
Iterates through all of the RuleSets associated with the group,
and ensures that the correct permissions are either applied or removed from the group. and ensures that the correct permissions are either applied or removed from the group.
This function is called under the following conditions: This function is called under the following conditions:
@ -335,7 +327,6 @@ def update_group_roles(group, debug=False):
The RuleSet model has complete control over the permissions applied to any group. The RuleSet model has complete control over the permissions applied to any group.
""" """
if not canAppAccessDatabase(allow_test=True): if not canAppAccessDatabase(allow_test=True):
return # pragma: no cover return # pragma: no cover
@ -361,15 +352,13 @@ def update_group_roles(group, debug=False):
permissions_to_delete = set() permissions_to_delete = set()
def add_model(name, action, allowed): def add_model(name, action, allowed):
""" """Add a new model to the pile:
Add a new model to the pile:
args: args:
name - The name of the model e.g. part_part name - The name of the model e.g. part_part
action - The permission action e.g. view action - The permission action e.g. view
allowed - Whether or not the action is allowed allowed - Whether or not the action is allowed
""" """
if action not in ['view', 'add', 'change', 'delete']: # pragma: no cover if action not in ['view', 'add', 'change', 'delete']: # pragma: no cover
raise ValueError("Action {a} is invalid".format(a=action)) raise ValueError("Action {a} is invalid".format(a=action))
@ -412,16 +401,13 @@ def update_group_roles(group, debug=False):
add_model(model, 'delete', ruleset.can_delete) add_model(model, 'delete', ruleset.can_delete)
def get_permission_object(permission_string): def get_permission_object(permission_string):
""" """Find the permission object in the database, from the simplified permission string
Find the permission object in the database,
from the simplified permission string
Args: Args:
permission_string - a simplified permission_string e.g. 'part.view_partcategory' permission_string - a simplified permission_string e.g. 'part.view_partcategory'
Returns the permission object in the database associated with the permission string Returns the permission object in the database associated with the permission string
""" """
(app, perm) = permission_string.split('.') (app, perm) = permission_string.split('.')
perm, model = split_permission(app, perm) perm, model = split_permission(app, perm)
@ -490,23 +476,19 @@ def update_group_roles(group, debug=False):
@receiver(post_save, sender=Group, dispatch_uid='create_missing_rule_sets') @receiver(post_save, sender=Group, dispatch_uid='create_missing_rule_sets')
def create_missing_rule_sets(sender, instance, **kwargs): def create_missing_rule_sets(sender, instance, **kwargs):
""" """Called *after* a Group object is saved.
Called *after* a Group object is saved.
As the linked RuleSet instances are saved *before* the Group, As the linked RuleSet instances are saved *before* the Group,
then we can now use these RuleSet values to update the then we can now use these RuleSet values to update the
group permissions. group permissions.
""" """
update_group_roles(instance) update_group_roles(instance)
def check_user_role(user, role, permission): def check_user_role(user, role, permission):
""" """Check if a user has a particular role:permission combination.
Check if a user has a particular role:permission combination.
If the user is a superuser, this will return True If the user is a superuser, this will return True
""" """
if user.is_superuser: if user.is_superuser:
return True return True
@ -533,8 +515,7 @@ def check_user_role(user, role, permission):
class Owner(models.Model): class Owner(models.Model):
""" """The Owner class is a proxy for a Group or User instance.
The Owner class is a proxy for a Group or User instance.
Owner can be associated to any InvenTree model (part, stock, build, etc.) Owner can be associated to any InvenTree model (part, stock, build, etc.)
owner_type: Model type (Group or User) owner_type: Model type (Group or User)
@ -544,13 +525,11 @@ class Owner(models.Model):
@classmethod @classmethod
def get_owners_matching_user(cls, user): def get_owners_matching_user(cls, user):
""" """Return all "owner" objects matching the provided user:
Return all "owner" objects matching the provided user:
A) An exact match for the user A) An exact match for the user
B) Any groups that the user is a part of B) Any groups that the user is a part of
""" """
user_type = ContentType.objects.get(app_label='auth', model='user') user_type = ContentType.objects.get(app_label='auth', model='user')
group_type = ContentType.objects.get(app_label='auth', model='group') group_type = ContentType.objects.get(app_label='auth', model='group')
@ -602,7 +581,6 @@ class Owner(models.Model):
@classmethod @classmethod
def create(cls, obj): def create(cls, obj):
"""Check if owner exist then create new owner entry""" """Check if owner exist then create new owner entry"""
# Check for existing owner # Check for existing owner
existing_owner = cls.get_owner(obj) existing_owner = cls.get_owner(obj)
@ -618,7 +596,6 @@ class Owner(models.Model):
@classmethod @classmethod
def get_owner(cls, user_or_group): def get_owner(cls, user_or_group):
"""Get owner instance for a group or user""" """Get owner instance for a group or user"""
user_model = get_user_model() user_model = get_user_model()
owner = None owner = None
content_type_id = 0 content_type_id = 0
@ -641,11 +618,10 @@ class Owner(models.Model):
return owner return owner
def get_related_owners(self, include_group=False): def get_related_owners(self, include_group=False):
""" """Get all owners "related" to an owner.
Get all owners "related" to an owner.
This method is useful to retrieve all "user-type" owners linked to a "group-type" owner This method is useful to retrieve all "user-type" owners linked to a "group-type" owner
""" """
user_model = get_user_model() user_model = get_user_model()
related_owners = None related_owners = None
@ -670,21 +646,13 @@ class Owner(models.Model):
@receiver(post_save, sender=Group, dispatch_uid='create_owner') @receiver(post_save, sender=Group, dispatch_uid='create_owner')
@receiver(post_save, sender=get_user_model(), dispatch_uid='create_owner') @receiver(post_save, sender=get_user_model(), dispatch_uid='create_owner')
def create_owner(sender, instance, **kwargs): def create_owner(sender, instance, **kwargs):
""" """Callback function to create a new owner instance after either a new group or user instance is saved."""
Callback function to create a new owner instance
after either a new group or user instance is saved.
"""
Owner.create(obj=instance) Owner.create(obj=instance)
@receiver(post_delete, sender=Group, dispatch_uid='delete_owner') @receiver(post_delete, sender=Group, dispatch_uid='delete_owner')
@receiver(post_delete, sender=get_user_model(), dispatch_uid='delete_owner') @receiver(post_delete, sender=get_user_model(), dispatch_uid='delete_owner')
def delete_owner(sender, instance, **kwargs): def delete_owner(sender, instance, **kwargs):
""" """Callback function to delete an owner instance after either a new group or user instance is deleted."""
Callback function to delete an owner instance
after either a new group or user instance is deleted.
"""
owner = Owner.get_owner(instance) owner = Owner.get_owner(instance)
owner.delete() owner.delete()