winipedia-django 0.2.24__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,333 @@
1
+ """Command utilities for Django.
2
+
3
+ This module provides utility functions for working with Django commands,
4
+ including command execution and output handling. These utilities help with
5
+ managing and automating Django command-line tasks.
6
+ """
7
+
8
+ import logging
9
+ from abc import abstractmethod
10
+ from argparse import ArgumentParser
11
+ from typing import Any, final
12
+
13
+ from django.core.management import BaseCommand
14
+ from winipedia_utils.oop.mixins.mixin import ABCLoggingMixin
15
+
16
+ logger = logging.getLogger(__name__)
17
+
18
+
19
+ class ABCBaseCommand(ABCLoggingMixin, BaseCommand):
20
+ """Abstract base class for Django management commands with logging and validation.
21
+
22
+ This class serves as a foundation for creating Django management commands that
23
+ require abstract method implementation enforcement and automatic logging.
24
+ It combines Django's BaseCommand with ABCImplementationLoggingMixin to provide
25
+ both command functionality and development-time validation.
26
+
27
+ The class implements a template method pattern where common argument handling
28
+ and execution flow are managed by final methods, while specific implementations
29
+ are defined through abstract methods that subclasses must implement.
30
+
31
+ Key Features:
32
+ - Automatic logging of method calls with performance tracking
33
+ - Compile-time validation that all abstract methods are implemented
34
+ - Structured argument handling with base and custom arguments
35
+ - Template method pattern for consistent command execution flow
36
+
37
+ Inheritance Order:
38
+ The order of inheritance is critical: ABCImplementationLoggingMixin must
39
+ come before BaseCommand because Django's BaseCommand doesn't call
40
+ super().__init__(), so the mixin's metaclass initialization must happen
41
+ first to ensure proper class construction.
42
+
43
+ Example:
44
+ >>> class MyCommand(ABCBaseCommand):
45
+ ... def add_command_arguments(self, parser):
46
+ ... parser.add_argument('--my-option', help='Custom option')
47
+ ...
48
+ ... def handle_command(self, *args, **options):
49
+ ... self.stdout.write('Executing my command')
50
+
51
+ Note:
52
+ - All methods are automatically logged with performance tracking
53
+ - Subclasses must implement add_command_arguments and handle_command
54
+ - The @final decorator prevents overriding of template methods
55
+ """
56
+
57
+ @final
58
+ def add_arguments(self, parser: ArgumentParser) -> None:
59
+ """Configure command-line arguments for the Django management command.
60
+
61
+ This method implements the template method pattern by first adding common
62
+ base arguments that are used across multiple commands, then delegating
63
+ to the abstract add_command_arguments method for command-specific arguments.
64
+
65
+ The @final decorator prevents subclasses from overriding this method,
66
+ ensuring consistent argument handling across all commands while still
67
+ allowing customization through the abstract method.
68
+
69
+ Args:
70
+ parser (ArgumentParser): Django's argument parser instance used to
71
+ define command-line options and arguments for the command.
72
+
73
+ Note:
74
+ - This method is final and cannot be overridden by subclasses
75
+ - Common arguments are added first via _add_arguments()
76
+ - Custom arguments are added via the abstract add_command_arguments()
77
+ - Subclasses must implement add_command_arguments() for specific needs
78
+ """
79
+ # add base args that are used in most commands
80
+ self._add_arguments(parser)
81
+
82
+ # add additional args that are specific to the command
83
+ self.add_command_arguments(parser)
84
+
85
+ @final
86
+ def _add_arguments(self, parser: ArgumentParser) -> None:
87
+ """Add common command-line arguments used across multiple commands.
88
+
89
+ This method defines base arguments that are commonly used across different
90
+ Django management commands. These arguments provide standard functionality
91
+ like dry-run mode, verbosity control, and batch processing options.
92
+
93
+ The method is final to ensure consistent base argument handling, while
94
+ command-specific arguments are handled through the abstract
95
+ add_command_arguments method.
96
+
97
+ Args:
98
+ parser (ArgumentParser): Django's argument parser instance to which
99
+ common arguments should be added.
100
+
101
+ Note:
102
+ - Provides standard arguments for dry-run, verbosity, and batch processing
103
+ - The @final decorator prevents subclasses from overriding this method
104
+ - Command-specific arguments should be added via add_command_arguments()
105
+ """
106
+ parser.add_argument(
107
+ "--dry-run",
108
+ action="store_true",
109
+ help="Show what would be done without actually executing the changes",
110
+ )
111
+
112
+ parser.add_argument(
113
+ "--size",
114
+ type=int,
115
+ default=None,
116
+ help="Size of smth in a command",
117
+ )
118
+
119
+ parser.add_argument(
120
+ "--force",
121
+ action="store_true",
122
+ help="Force an action in a command",
123
+ )
124
+
125
+ parser.add_argument(
126
+ "--delete",
127
+ action="store_true",
128
+ help="Deleting smth in a command",
129
+ )
130
+
131
+ parser.add_argument(
132
+ "--quiet",
133
+ action="store_true",
134
+ help="Suppress non-error output for cleaner automation",
135
+ )
136
+
137
+ parser.add_argument(
138
+ "--debug",
139
+ action="store_true",
140
+ help="Print debug output for detailed tracing",
141
+ )
142
+
143
+ parser.add_argument(
144
+ "--yes",
145
+ action="store_true",
146
+ help="Answer yes to all prompts",
147
+ default=False,
148
+ )
149
+
150
+ parser.add_argument(
151
+ "--config",
152
+ type=str,
153
+ help="A configuration setup like filepath or json string for a command",
154
+ default=None,
155
+ )
156
+
157
+ parser.add_argument(
158
+ "--timeout",
159
+ type=int,
160
+ help="Timeout for a command",
161
+ default=None,
162
+ )
163
+
164
+ parser.add_argument(
165
+ "--batch-size",
166
+ type=int,
167
+ default=None,
168
+ help="Number of items to process in each batch",
169
+ )
170
+
171
+ parser.add_argument(
172
+ "--no-input",
173
+ action="store_true",
174
+ help="Do not prompt for user input",
175
+ )
176
+
177
+ parser.add_argument(
178
+ "--threads",
179
+ type=int,
180
+ default=None,
181
+ help="Number of threads to use for processing",
182
+ )
183
+
184
+ parser.add_argument(
185
+ "--processes",
186
+ type=int,
187
+ default=None,
188
+ help="Number of processes to use for processing",
189
+ )
190
+
191
+ @abstractmethod
192
+ def add_command_arguments(self, parser: ArgumentParser) -> None:
193
+ """Add command-specific arguments to the argument parser.
194
+
195
+ This abstract method must be implemented by subclasses to define
196
+ command-specific command-line arguments. It is called after common
197
+ base arguments are added, allowing each command to customize its
198
+ argument interface while maintaining consistent base functionality.
199
+
200
+ Subclasses should use this method to add arguments specific to their
201
+ command's functionality, such as file paths, configuration options,
202
+ or operational flags.
203
+
204
+ Args:
205
+ parser (ArgumentParser): Django's argument parser instance to which
206
+ command-specific arguments should be added.
207
+
208
+ Example:
209
+ >>> def add_command_arguments(self, parser):
210
+ ... parser.add_argument(
211
+ ... '--input-file',
212
+ ... type=str,
213
+ ... required=True,
214
+ ... help='Path to input file'
215
+ ... )
216
+ ... parser.add_argument(
217
+ ... '--output-format',
218
+ ... choices=['json', 'csv', 'xml'],
219
+ ... default='json',
220
+ ... help='Output format for results'
221
+ ... )
222
+
223
+ Note:
224
+ - This method is abstract and must be implemented by subclasses
225
+ - Called after _add_arguments() adds common base arguments
226
+ - Should focus on command-specific functionality only
227
+ """
228
+
229
+ @final
230
+ def handle(self, *args: Any, **options: Any) -> None:
231
+ """Execute the Django management command using template method pattern.
232
+
233
+ This method implements the main execution flow for the command by first
234
+ calling common handling logic through _handle(), then delegating to
235
+ the command-specific implementation via handle_command().
236
+
237
+ The @final decorator ensures this execution pattern cannot be overridden,
238
+ maintaining consistent command execution flow while allowing customization
239
+ through the abstract handle_command method.
240
+
241
+ Args:
242
+ *args: Positional arguments passed from Django's command execution.
243
+ **options: Keyword arguments containing parsed command-line options
244
+ and their values as defined by add_arguments().
245
+
246
+ Note:
247
+ - This method is final and cannot be overridden by subclasses
248
+ - Common handling logic is executed first via _handle()
249
+ - Command-specific logic is executed via abstract handle_command()
250
+ - All method calls are automatically logged with performance tracking
251
+ """
252
+ self._handle(*args, **options)
253
+ self.handle_command(*args, **options)
254
+
255
+ @final
256
+ def _handle(self, *_args: Any, **options: Any) -> None:
257
+ """Execute common handling logic shared across all commands.
258
+
259
+ This method is intended to contain common processing logic that should
260
+ be executed before command-specific handling. Currently, it serves as
261
+ a placeholder for future common functionality such as logging setup,
262
+ validation, or shared initialization.
263
+
264
+ The method is final to ensure consistent common handling across all
265
+ commands, while command-specific logic is handled through the abstract
266
+ handle_command method.
267
+
268
+ Args:
269
+ *args: Positional arguments passed from Django's command execution.
270
+ Currently unused but reserved for future common processing.
271
+ **options: Keyword arguments containing parsed command-line options.
272
+ Currently unused but reserved for future common processing.
273
+
274
+ Note:
275
+ - Examples might include logging setup, database connection validation, etc.
276
+ - The @final decorator prevents subclasses from overriding this method
277
+ - Called before handle_command() in the template method pattern
278
+ """
279
+ # log each option for the command
280
+ for key, value in options.items():
281
+ logger.info(
282
+ "Command '%s' - runs with option: '%s' with value: '%s'",
283
+ self.__class__.__name__,
284
+ key,
285
+ value,
286
+ )
287
+
288
+ @abstractmethod
289
+ def handle_command(self, *args: Any, **options: Any) -> None:
290
+ """Execute command-specific logic and functionality.
291
+
292
+ This abstract method must be implemented by subclasses to define the
293
+ core functionality of the Django management command. It is called after
294
+ common handling logic is executed, allowing each command to implement
295
+ its specific business logic while benefiting from shared infrastructure.
296
+
297
+ This method should contain the main logic that the command is designed
298
+ to perform, such as data processing, database operations, file manipulation,
299
+ or any other command-specific tasks.
300
+
301
+ Args:
302
+ *args: Positional arguments passed from Django's command execution.
303
+ These are typically not used in Django management commands.
304
+ **options: Keyword arguments containing parsed command-line options
305
+ and their values as defined by add_command_arguments().
306
+
307
+ Example:
308
+ >>> def handle_command(self, *args, **options):
309
+ ... input_file = options['input_file']
310
+ ... dry_run = options['dry_run'] # Base argument
311
+ ... batch_size = options['batch_size'] # Base argument
312
+ ... quiet = options['quiet'] # Base argument
313
+ ...
314
+ ... if dry_run:
315
+ ... self.stdout.write('Dry run mode - no changes will be made')
316
+ ...
317
+ ... if not quiet:
318
+ ... msg = f'Processing {input_file} in batches of {batch_size}'
319
+ ... self.stdout.write(msg)
320
+ ...
321
+ ... # Perform command-specific operations
322
+ ... self.process_file(input_file, batch_size, dry_run)
323
+ ...
324
+ ... if not quiet:
325
+ ... self.stdout.write('Command completed successfully')
326
+
327
+ Note:
328
+ - This method is abstract and must be implemented by subclasses
329
+ - Called after _handle() executes common logic
330
+ - Should contain the main functionality of the command
331
+ - All method calls are automatically logged with performance tracking
332
+ - Use self.stdout.write() for output instead of print()
333
+ """
@@ -0,0 +1,288 @@
1
+ """Database utilities for Django.
2
+
3
+ This module provides utility functions for working with Django models,
4
+ including hashing, topological sorting, and database operations.
5
+ These utilities help with efficient and safe database interactions.
6
+ """
7
+
8
+ from datetime import datetime
9
+ from graphlib import TopologicalSorter
10
+ from typing import TYPE_CHECKING, Any, Self
11
+
12
+ from django.db import connection
13
+ from django.db.models import DateTimeField, Field, Model
14
+ from django.db.models.fields.related import ForeignKey, ForeignObjectRel
15
+ from django.forms.models import model_to_dict
16
+ from winipedia_utils.logging.logger import get_logger
17
+
18
+ if TYPE_CHECKING:
19
+ from django.contrib.contenttypes.fields import GenericForeignKey
20
+ from django.db.models.options import Options
21
+
22
+ logger = get_logger(__name__)
23
+
24
+
25
+ def get_model_meta(model: type[Model]) -> "Options[Model]":
26
+ """Get the Django model metadata options object.
27
+
28
+ Retrieves the _meta attribute from a Django model class, which contains
29
+ metadata about the model including field definitions, table name, and
30
+ other model configuration options. This is a convenience wrapper around
31
+ accessing the private _meta attribute directly.
32
+
33
+ Args:
34
+ model (type[Model]): The Django model class to get metadata from.
35
+
36
+ Returns:
37
+ Options[Model]: The model's metadata options object containing
38
+ field definitions, table information, and other model configuration.
39
+
40
+ Example:
41
+ >>> from django.contrib.auth.models import User
42
+ >>> meta = get_model_meta(User)
43
+ >>> meta.db_table
44
+ 'auth_user'
45
+ >>> len(meta.get_fields())
46
+ 11
47
+ """
48
+ return model._meta # noqa: SLF001
49
+
50
+
51
+ def get_fields(
52
+ model: type[Model],
53
+ ) -> "list[Field[Any, Any] | ForeignObjectRel | GenericForeignKey]":
54
+ """Get all fields from a Django model including relationships.
55
+
56
+ Retrieves all field objects from a Django model, including regular fields,
57
+ foreign key relationships, reverse foreign key relationships, and generic
58
+ foreign keys. This provides a comprehensive view of all model attributes
59
+ that can be used for introspection, validation, or bulk operations.
60
+
61
+ Args:
62
+ model (type[Model]): The Django model class to get fields from.
63
+
64
+ Returns:
65
+ list[Field | ForeignObjectRel | GenericForeignKey]: A list
66
+ containing all field objects associated with the model, including:
67
+ - Regular model fields (CharField, IntegerField, etc.)
68
+ - Foreign key fields (ForeignKey, OneToOneField, etc.)
69
+ - Reverse relationship fields (ForeignObjectRel)
70
+ - Generic foreign key fields (GenericForeignKey)
71
+
72
+ Example:
73
+ >>> from django.contrib.auth.models import User
74
+ >>> fields = get_fields(User)
75
+ >>> field_names = [f.name for f in fields if hasattr(f, 'name')]
76
+ >>> 'username' in field_names
77
+ True
78
+ >>> 'email' in field_names
79
+ True
80
+ """
81
+ return get_model_meta(model).get_fields()
82
+
83
+
84
+ def get_field_names(
85
+ fields: "list[Field[Any, Any] | ForeignObjectRel | GenericForeignKey]",
86
+ ) -> list[str]:
87
+ """Get the names of all fields from a Django model including relationships.
88
+
89
+ Retrieves the names of all field objects from a Django model, including
90
+ regular fields, foreign key relationships, reverse foreign key relationships,
91
+ and generic foreign keys. This provides a comprehensive view of all model
92
+ attributes that can be used for introspection, validation, or bulk operations.
93
+
94
+ Args:
95
+ fields (list[Field | ForeignObjectRel | GenericForeignKey]):
96
+ The list of field objects to get names from.
97
+
98
+ Returns:
99
+ list[str]: A list containing the names of all fields.
100
+
101
+ Example:
102
+ >>> from django.contrib.auth.models import User
103
+ >>> fields = get_fields(User)
104
+ >>> field_names = get_field_names(fields)
105
+ >>> 'username' in field_names
106
+ True
107
+ >>> 'email' in field_names
108
+ True
109
+ """
110
+ return [field.name for field in fields]
111
+
112
+
113
+ def topological_sort_models(models: list[type[Model]]) -> list[type[Model]]:
114
+ """Sort Django models in dependency order using topological sorting.
115
+
116
+ Analyzes foreign key relationships between Django models and returns them
117
+ in an order where dependencies come before dependents. This ensures that
118
+ when performing operations like bulk creation or deletion, models are
119
+ processed in the correct order to avoid foreign key constraint violations.
120
+
121
+ The function uses Python's graphlib.TopologicalSorter to perform the sorting
122
+ based on ForeignKey relationships between the provided models. Only
123
+ relationships between models in the input list are considered.
124
+
125
+ Args:
126
+ models (list[type[Model]]): A list of Django model classes to sort
127
+ based on their foreign key dependencies.
128
+
129
+ Returns:
130
+ list[type[Model]]: The input models sorted in dependency order, where
131
+ models that are referenced by foreign keys appear before models
132
+ that reference them. Self-referential relationships are ignored.
133
+
134
+ Raises:
135
+ graphlib.CycleError: If there are circular dependencies between models
136
+ that cannot be resolved.
137
+
138
+ Example:
139
+ >>> # Assuming Author model has no dependencies
140
+ >>> # and Book model has ForeignKey to Author
141
+ >>> models = [Book, Author]
142
+ >>> sorted_models = topological_sort_models(models)
143
+ >>> sorted_models
144
+ [<class 'Author'>, <class 'Book'>]
145
+
146
+ Note:
147
+ - Only considers ForeignKey relationships, not other field types
148
+ - Self-referential foreign keys are ignored to avoid self-loops
149
+ - Only relationships between models in the input list are considered
150
+ """
151
+ ts: TopologicalSorter[type[Model]] = TopologicalSorter()
152
+
153
+ for model in models:
154
+ deps = {
155
+ field.related_model
156
+ for field in get_fields(model)
157
+ if isinstance(field, ForeignKey)
158
+ and isinstance(field.related_model, type)
159
+ and field.related_model in models
160
+ and field.related_model is not model
161
+ }
162
+ ts.add(model, *deps)
163
+
164
+ return list(ts.static_order())
165
+
166
+
167
+ def execute_sql(
168
+ sql: str, params: dict[str, Any] | None = None
169
+ ) -> tuple[list[str], list[Any]]:
170
+ """Execute raw SQL query and return column names with results.
171
+
172
+ Executes a raw SQL query using Django's database connection and returns
173
+ both the column names and the result rows. This provides a convenient
174
+ way to run custom SQL queries while maintaining Django's database
175
+ connection management and parameter binding for security.
176
+
177
+ The function automatically handles cursor management and ensures proper
178
+ cleanup of database resources. Parameters are safely bound to prevent
179
+ SQL injection attacks.
180
+
181
+ Args:
182
+ sql (str): The SQL query string to execute. Can contain parameter
183
+ placeholders that will be safely bound using the params argument.
184
+ params (dict[str, Any] | None, optional): Dictionary of parameters
185
+ to bind to the SQL query for safe parameter substitution.
186
+ Defaults to None if no parameters are needed.
187
+
188
+ Returns:
189
+ tuple[list[str], list[Any]]: A tuple containing:
190
+ - list[str]: Column names from the query result
191
+ - list[Any]: List of result rows, where each row is a tuple
192
+ of values corresponding to the column names
193
+
194
+ Raises:
195
+ django.db.Error: If there's a database error during query execution
196
+ django.db.ProgrammingError: If the SQL syntax is invalid
197
+ django.db.IntegrityError: If the query violates database constraints
198
+
199
+ Example:
200
+ >>> sql = "SELECT id, username FROM auth_user WHERE is_active = %(active)s"
201
+ >>> params = {"active": True}
202
+ >>> columns, rows = execute_sql(sql, params)
203
+ >>> columns
204
+ ['id', 'username']
205
+ >>> rows[0]
206
+ (1, 'admin')
207
+
208
+ Note:
209
+ - Uses Django's default database connection
210
+ - Automatically manages cursor lifecycle
211
+ - Parameters are safely bound to prevent SQL injection
212
+ - Returns all results in memory - use with caution for large datasets
213
+ """
214
+ with connection.cursor() as cursor:
215
+ cursor.execute(sql=sql, params=params)
216
+ rows = cursor.fetchall()
217
+ column_names = [col[0] for col in cursor.description]
218
+
219
+ return column_names, rows
220
+
221
+
222
+ def hash_model_instance(
223
+ instance: Model,
224
+ fields: "list[Field[Any, Any] | ForeignObjectRel | GenericForeignKey]",
225
+ ) -> int:
226
+ """Hash a model instance based on its field values.
227
+
228
+ Generates a hash for a Django model instance by considering the values
229
+ of its fields. This can be useful for comparing instances, especially
230
+ when dealing with related objects or complex data structures. The hash
231
+ is generated by recursively hashing related objects up to a specified
232
+ depth.
233
+ This is not very reliable, use with caution.
234
+ Only use if working with unsafed objects or bulks, as with safed
235
+
236
+ Args:
237
+ instance (Model): The Django model instance to hash
238
+ fields (list[str]): The fields to hash
239
+
240
+ Returns:
241
+ int: The hash value representing the instance's data
242
+
243
+ """
244
+ if instance.pk:
245
+ return hash(instance.pk)
246
+
247
+ field_names = get_field_names(fields)
248
+ model_dict = model_to_dict(instance, fields=field_names)
249
+ sorted_dict = dict(sorted(model_dict.items()))
250
+ values = (type(instance), tuple(sorted_dict.items()))
251
+ return hash(values)
252
+
253
+
254
+ class BaseModel(Model):
255
+ """Base model for all models in the project.
256
+
257
+ Provides common fields and methods for all models.
258
+ """
259
+
260
+ created_at: DateTimeField[datetime, datetime] = DateTimeField(auto_now_add=True)
261
+ updated_at: DateTimeField[datetime, datetime] = DateTimeField(auto_now=True)
262
+
263
+ class Meta:
264
+ """Mark the model as abstract."""
265
+
266
+ # abstract does not inherit in children
267
+ abstract = True
268
+
269
+ def __str__(self) -> str:
270
+ """Base string representation of a model.
271
+
272
+ Returns:
273
+ str: The string representation of the model as all fields and their values.
274
+ """
275
+ fields_values = ", ".join(
276
+ f"{field.name}={getattr(self, field.name)}"
277
+ for field in get_fields(self.__class__)
278
+ )
279
+ return f"{self.__class__.__name__}({fields_values})"
280
+
281
+ def __repr__(self) -> str:
282
+ """Base representation of a model."""
283
+ return str(self)
284
+
285
+ @property
286
+ def meta(self) -> "Options[Self]":
287
+ """Get the meta options for the model."""
288
+ return self._meta
File without changes