django-findreplace 0.2__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.
- django_findreplace-0.2.dist-info/LICENSE +27 -0
- django_findreplace-0.2.dist-info/METADATA +59 -0
- django_findreplace-0.2.dist-info/RECORD +9 -0
- django_findreplace-0.2.dist-info/WHEEL +5 -0
- django_findreplace-0.2.dist-info/top_level.txt +1 -0
- findreplace/__init__.py +0 -0
- findreplace/management/__init__.py +0 -0
- findreplace/management/commands/__init__.py +0 -0
- findreplace/management/commands/findreplace.py +70 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
Copyright (c) 2018-2024, Developer Society Limited
|
|
2
|
+
All rights reserved.
|
|
3
|
+
|
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
|
6
|
+
|
|
7
|
+
* Redistributions of source code must retain the above copyright notice, this
|
|
8
|
+
list of conditions and the following disclaimer.
|
|
9
|
+
|
|
10
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
|
11
|
+
this list of conditions and the following disclaimer in the documentation
|
|
12
|
+
and/or other materials provided with the distribution.
|
|
13
|
+
|
|
14
|
+
* Neither the name of the copyright holder nor the names of its
|
|
15
|
+
contributors may be used to endorse or promote products derived from
|
|
16
|
+
this software without specific prior written permission.
|
|
17
|
+
|
|
18
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
19
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
20
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
21
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
22
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
23
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
24
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
25
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
26
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
27
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: django-findreplace
|
|
3
|
+
Version: 0.2
|
|
4
|
+
Summary: Django Find Replace
|
|
5
|
+
Maintainer-email: The Developer Society <studio@dev.ngo>
|
|
6
|
+
Project-URL: Homepage, https://github.com/developersociety/django-findreplace
|
|
7
|
+
Classifier: Intended Audience :: Developers
|
|
8
|
+
Classifier: License :: OSI Approved :: BSD License
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Classifier: Programming Language :: Python
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Framework :: Django
|
|
17
|
+
Classifier: Framework :: Django :: 3.2
|
|
18
|
+
Classifier: Framework :: Django :: 4.2
|
|
19
|
+
Requires-Python: >=3.9
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: Django>=3.2
|
|
23
|
+
|
|
24
|
+
# Django Find Replace
|
|
25
|
+
|
|
26
|
+
A [Django](https://www.djangoproject.com/) management command which will replace all instances of
|
|
27
|
+
a string throughout a database with another - useful for bulk content changes.
|
|
28
|
+
|
|
29
|
+
## Installation
|
|
30
|
+
|
|
31
|
+
Using [pip](https://pip.pypa.io/):
|
|
32
|
+
|
|
33
|
+
```console
|
|
34
|
+
$ pip install django-findreplace
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Edit your Django project's settings module, and add the application to ``INSTALLED_APPS``:
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
INSTALLED_APPS = [
|
|
41
|
+
# ...
|
|
42
|
+
"findreplace",
|
|
43
|
+
# ...
|
|
44
|
+
]
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Usage
|
|
48
|
+
|
|
49
|
+
To replace all instances of *foo* with *bar*:
|
|
50
|
+
|
|
51
|
+
```console
|
|
52
|
+
$ ./manage.py findreplace foo bar
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
To use this command without being asked for confirmation:
|
|
56
|
+
|
|
57
|
+
```console
|
|
58
|
+
$ ./manage.py findreplace --noinput foo bar
|
|
59
|
+
```
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
findreplace/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
findreplace/management/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
+
findreplace/management/commands/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
findreplace/management/commands/findreplace.py,sha256=x6Gq-I81QU3Kxz6_5wcAL5NgN08KlUCQpkpJO_UMLXc,2452
|
|
5
|
+
django_findreplace-0.2.dist-info/LICENSE,sha256=XGMu9iBBTft-0mgZ4IGTqCVHquL9kzM9W4oG5qGXgug,1508
|
|
6
|
+
django_findreplace-0.2.dist-info/METADATA,sha256=FSAZBcPfSGxW8NQm9CVQsckVcmB5BQsgNpzDgLYXU6w,1578
|
|
7
|
+
django_findreplace-0.2.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
|
|
8
|
+
django_findreplace-0.2.dist-info/top_level.txt,sha256=h6H8p1SOLbsj59tHEvD0MIUxkdRD0hID5vyzZavOv9U,12
|
|
9
|
+
django_findreplace-0.2.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
findreplace
|
findreplace/__init__.py
ADDED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
from django.apps import apps
|
|
2
|
+
from django.core.management.base import BaseCommand
|
|
3
|
+
from django.db import models
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class Command(BaseCommand):
|
|
7
|
+
def add_arguments(self, parser):
|
|
8
|
+
parser.add_argument("find")
|
|
9
|
+
parser.add_argument("replace")
|
|
10
|
+
parser.add_argument(
|
|
11
|
+
"--noinput",
|
|
12
|
+
"--no-input",
|
|
13
|
+
action="store_false",
|
|
14
|
+
dest="interactive",
|
|
15
|
+
default=True,
|
|
16
|
+
help="Tells Django to NOT prompt the user for input of any kind.",
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
def handle(self, *args, **options):
|
|
20
|
+
if options["interactive"]:
|
|
21
|
+
self.stdout.write(
|
|
22
|
+
'\nReplace ALL instances of "{}" with "{}"?\n\n'.format(
|
|
23
|
+
options["find"], options["replace"]
|
|
24
|
+
)
|
|
25
|
+
)
|
|
26
|
+
confirm = input(" Type 'yes' to continue, or 'no' to cancel: ")
|
|
27
|
+
else:
|
|
28
|
+
confirm = "yes"
|
|
29
|
+
|
|
30
|
+
updated_count = 0
|
|
31
|
+
|
|
32
|
+
if confirm == "yes":
|
|
33
|
+
for field in self.get_fields():
|
|
34
|
+
updated_count += self.find_replace(
|
|
35
|
+
field=field, find=options["find"], replace=options["replace"]
|
|
36
|
+
)
|
|
37
|
+
else:
|
|
38
|
+
self.stdout.write("Find and replace cancelled.")
|
|
39
|
+
|
|
40
|
+
def find_replace(self, field, find, replace):
|
|
41
|
+
updated_count = 0
|
|
42
|
+
model = field.model
|
|
43
|
+
filter_kwargs = {f"{field.name}__contains": find}
|
|
44
|
+
|
|
45
|
+
# To avoid signals at all costs, we have to:
|
|
46
|
+
# - Use values_list to avoid pre/post init
|
|
47
|
+
# - Use update to avoid pre/post save
|
|
48
|
+
queryset = model._default_manager.filter(**filter_kwargs).values_list("pk", field.name)
|
|
49
|
+
for pk, field_data in queryset.iterator():
|
|
50
|
+
update_kwargs = {field.name: field_data.replace(find, replace)}
|
|
51
|
+
updated_count += model._default_manager.filter(pk=pk).update(**update_kwargs)
|
|
52
|
+
|
|
53
|
+
return updated_count
|
|
54
|
+
|
|
55
|
+
def get_fields(self):
|
|
56
|
+
"""
|
|
57
|
+
Return a list of fields from installed apps which can be updated.
|
|
58
|
+
|
|
59
|
+
Any registered models with fields which extend from CharField or TextField will be returned
|
|
60
|
+
as a list of fields.
|
|
61
|
+
"""
|
|
62
|
+
fields = []
|
|
63
|
+
|
|
64
|
+
for app in apps.get_app_configs():
|
|
65
|
+
for model in app.get_models():
|
|
66
|
+
for field in model._meta.get_fields():
|
|
67
|
+
if isinstance(field, (models.CharField, models.TextField)):
|
|
68
|
+
fields.append(field)
|
|
69
|
+
|
|
70
|
+
return fields
|