Skip to content

Commit

Permalink
docs(management): update guide to avoid bug
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleyboar committed Feb 27, 2023
1 parent ace057a commit bcc4721
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions taccsite_cms/management/commands/group_perms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,37 @@ You may **either** download an appropriate `.html` from [Django CMS - Developer

Use regex to convert the `<option>`s from HTML to Python Django CMS instructions.

- Find:
- Find:\
<sub>(minified HTML)</sub>

```regexp
[\n\s]*<option[\n\s]*value=".+"[\n\s]*title="[\s\w]+ \| [\s\w]+ \| ([\s\w]+)"[\n\s]*>\n[\s]*[\s\w]+ \| [\s\w]+ \| [\s\w]+</option>
<option value=".+?" title="([\s\w]+) \| ([\s\w]+) \| ([\s\w]+)">[\s\w]+ \| [\s\w]+ \| [\s\w]+</option>
```

<sub>(unminified HTML)</sub>

```regexp
[\n\s]*<option[\n\s]*value=".+"[\n\s]*title="([\s\w]+) \| ([\s\w]+) \| ([\s\w]+)"[\n\s]*>\n*[\s]*[\s\w]+ \| [\s\w]+ \| [\s\w]+</option>
```

- Replace:

```text
group.permissions.add( Permission.objects.get(name='$1') )\n
model_name = '$2'.lower().replace(' ', '')
content_type = ContentType.objects.get(app_label='$1', model=model_name)
group.permissions.add( Permission.objects.get(name='$3', content_type=content_type) )
```

## Program Permissions

1. Create a python script in this directory named after the group e.g. `news_writer_advanced.py`.
2. Add this starter code:

```py
from django.contrib.auth.models import Group, Permission
from django.core.management import BaseCommand
```py
from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType
from django.core.management import BaseCommand

def set_group_perms():
group, was_created = Group.objects.get_or_create(
Expand All @@ -53,12 +64,15 @@ Use regex to convert the `<option>`s from HTML to Python Django CMS instructions

1. Open a shell into the CMS container e.g. `docker exec -it core_cms /bin/bash`.
2. In the shell, open a Python shell i.e. `python`.
3. In the Python shell, run these commands:
1. `import os`
2. `import django`
3. `os.environ.setdefault("DJANGO_SETTINGS_MODULE", "taccsite_cms.settings")`
4. `django.setup()`
5. Any additional debugging code or scripts you want to execute.
3. In the Python shell, run the following commands.

```py
import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "taccsite_cms.settings")
django.setup()
# any additional debugging code or scripts you want to execute
```

## Reference

Expand Down

0 comments on commit bcc4721

Please sign in to comment.