Skip to content

Commit

Permalink
feat: Add support for Django redirects in the project
Browse files Browse the repository at this point in the history
Implemented `Redirect` model handling in admin, registered it, and added a sample fixture. Updated settings to include `django.contrib.redirects` in `INSTALLED_APPS` and middleware. Added documentation for resolving migration-related issues.
  • Loading branch information
JasonLovesDoggo committed Jan 6, 2025
1 parent 43e2d59 commit 7535cbc
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,22 @@
UserAdmin as BaseUserAdmin,
)
from django.contrib.auth.models import Group
from django.contrib.redirects.models import Redirect
from django.db import models
from django.utils.translation import gettext_lazy as _
from unfold.admin import ModelAdmin
from unfold.contrib.forms.widgets import WysiwygWidget
from unfold.decorators import display
from unfold.forms import AdminPasswordChangeForm, UserChangeForm

from django.contrib.redirects.admin import RedirectAdmin as BaseRedirectAdmin
from .models import Category, Hackathon, Hacker, School
from .models import HackathonLocation


class RedirectAdmin(ModelAdmin, BaseRedirectAdmin):
pass


class EmailAddressAdmin(BaseEmailAddress, ModelAdmin):
pass

Expand Down Expand Up @@ -155,3 +160,6 @@ class SchoolAdmin(ModelAdmin):

admin.site.unregister(EmailAddress)
admin.site.register(EmailAddress, EmailAddressAdmin)

admin.site.unregister(Redirect)
admin.site.register(Redirect, RedirectAdmin)
77 changes: 77 additions & 0 deletions docs/internal/migrations/redirects.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Resolving `django.db.migrations.exceptions.InconsistentMigrationHistory` Error

If you encounter the error
`django.db.migrations.exceptions.InconsistentMigrationHistory: Migration socialaccount.0001_initial is applied before its dependency sites.0001_initial on database 'default'.`
while migrating, follow these steps to resolve it:

---

## Steps to Resolve

### 1. Comment Out Apps and Middleware

Open the **settings file** (`common.py`) and temporarily comment out the following configurations:

In the `INSTALLED_APPS` section, comment out:

```python
'django.contrib.sites',
'django.contrib.redirects',
```

In the `MIDDLEWARE` section, comment out:

```python
'django.contrib.redirects.middleware.RedirectFallbackMiddleware',
```

After updating the settings, save the file.

---

### 2. Roll Back the Problematic Migrations

Run the following Django command in your terminal to roll back the `socialaccount` app's migrations to the start (zero):

```bash
python manage.py migrate socialaccount zero
```

This will unapply all migrations related to the `socialaccount` app.

---

### 3. Restore Settings

After rolling back the migrations, revert the changes you made in `common.py`:

1. Uncomment `'django.contrib.sites'` and `'django.contrib.redirects'` in the `INSTALLED_APPS` section.
2. Uncomment `'django.contrib.redirects.middleware.RedirectFallbackMiddleware'` in the `MIDDLEWARE` section.

Save the file.

---

### 4. Reapply Migrations

Re-run the migrations step-by-step:

```bash
python manage.py migrate
```

This will reapply all migrations in the correct order, resolving the inconsistent migration history.

---

### Summary of Commands

Here’s a quick list of the commands you’ll need:

```bash
# Step 2: Roll back socialaccount migrations
python manage.py migrate socialaccount zero

# Step 4: Reapply migrations
python manage.py migrate
```
Binary file added fixtures/redirects.json
Binary file not shown.
4 changes: 4 additions & 0 deletions settings/components/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.sites",
"django.contrib.redirects",
"django.contrib.messages",
"django.contrib.staticfiles",
"django_countries",
Expand Down Expand Up @@ -112,6 +114,7 @@
# # Eliminate need to provide username, as it's a very old practice
# ACCOUNT_USERNAME_REQUIRED = False


MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
Expand All @@ -121,6 +124,7 @@
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"allauth.account.middleware.AccountMiddleware",
"django.contrib.redirects.middleware.RedirectFallbackMiddleware",
]

ROOT_URLCONF = "hackathons_canada.urls"
Expand Down

0 comments on commit 7535cbc

Please sign in to comment.