Skip to content

Commit

Permalink
feat: Use common package (#1232)
Browse files Browse the repository at this point in the history
* Move models, create common module and migrations

* Make common pypi package

* Make common pypi package part 2

* Rename migrations

* Exclude common package from main package

* Build and upload common package in travis + use portal version

* Update common folder name

* Merge branch 'master' into common-models-test

# Conflicts:
#	.travis.yml

* Install actual PyPi package

* Revert temp settings

* Update lock file

* Merge branch 'master' into common-models-test

* Fix version of cfl-common to portal version

* Use correct models in migration

Signed-off-by: Niket <niket.shah1@ocado.com>

* show correct python version

Signed-off-by: Niket <niket.shah1@ocado.com>

* Add migration test for portal

* Add check the models are under common

* Refactor model imports

* Bring back Guardian model

* update permissions required as they are now in common

Signed-off-by: Niket <niket.shah1@ocado.com>

* Merge branch 'master' into common-models-test

# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.

* Install RR branch

* Don't t-lib

* Add module docstring to migration

* Try installing RR with -U

* Merge branch 'master' into common-models-test

* Try to add branch config to see if it compares to master

* Remove branch parameter

* Merge branch 'master' into common-models-test

* Merge branch 'master' into common-models-test

* Merge branch 'master' into common-models-test

* Install normal RR
  • Loading branch information
faucomte97 committed Aug 20, 2020
1 parent 7dd0618 commit 69af469
Show file tree
Hide file tree
Showing 32 changed files with 448 additions and 399 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -23,3 +23,6 @@ portal/tests/chromedriver
db.sqlite3
example_project/example_project/static/
codeforlife_portal.egg-info
*.egg-info/
build/
dist/
276 changes: 154 additions & 122 deletions Pipfile.lock

Large diffs are not rendered by default.

11 changes: 2 additions & 9 deletions portal/admin.py
Expand Up @@ -34,19 +34,12 @@
# copyright notice and these terms. You must not misrepresent the origins of this
# program; modified versions of the program must be marked as such and not
# identified as the original program.
from common.models import School, Teacher, UserProfile, Class, Student
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User

from portal.models import (
Class,
Student,
Guardian,
Teacher,
School,
UserProfile,
EmailVerification,
)
from portal.models import Guardian, EmailVerification


class ClassAdmin(admin.ModelAdmin):
Expand Down
2 changes: 1 addition & 1 deletion portal/aimmo_game.py
Expand Up @@ -35,7 +35,7 @@
# program; modified versions of the program must be marked as such and not
# identified as the original program.

from portal.models import Class, Student
from common.models import Class, Student


def get_users_for_new_game(request):
Expand Down
1 change: 1 addition & 0 deletions portal/autoconfig.py
Expand Up @@ -80,6 +80,7 @@
"game",
"pipeline",
"portal",
"common",
"django.contrib.admin",
"django.contrib.admindocs",
"django.contrib.auth",
Expand Down
7 changes: 3 additions & 4 deletions portal/forms/organisation.py
Expand Up @@ -35,13 +35,12 @@
# program; modified versions of the program must be marked as such and not
# identified as the original program.
from builtins import object
from django import forms

from portal.models import School

from django_countries.widgets import CountrySelectWidget
from common.models import School
from django import forms
from django.core.exceptions import ObjectDoesNotExist
from django.core.validators import EmailValidator
from django_countries.widgets import CountrySelectWidget


class OrganisationForm(forms.ModelForm):
Expand Down
2 changes: 1 addition & 1 deletion portal/forms/play.py
Expand Up @@ -39,13 +39,13 @@

from captcha.fields import ReCaptchaField
from captcha.widgets import ReCaptchaV2Invisible
from common.models import Class, Student, stripStudentName
from django import forms
from django.contrib.auth import authenticate
from django.contrib.auth.forms import AuthenticationForm
from django.utils import timezone

from portal.helpers.password import form_clean_password
from portal.models import Class, Student, stripStudentName
from portal.permissions import logged_in_as_independent_student


Expand Down
2 changes: 1 addition & 1 deletion portal/forms/registration.py
Expand Up @@ -36,6 +36,7 @@
# identified as the original program.
from captcha.fields import ReCaptchaField
from captcha.widgets import ReCaptchaV2Invisible
from common.models import Student, Teacher
from django import forms
from django.contrib.auth import forms as django_auth_forms
from django.contrib.auth import get_user_model
Expand All @@ -48,7 +49,6 @@
from django.utils.http import urlsafe_base64_encode

from portal.helpers.password import form_clean_password
from portal.models import Student, Teacher


class PasswordResetSetPasswordForm(django_auth_forms.SetPasswordForm):
Expand Down
8 changes: 4 additions & 4 deletions portal/forms/teach.py
Expand Up @@ -34,20 +34,20 @@
# copyright notice and these terms. You must not misrepresent the origins of this
# program; modified versions of the program must be marked as such and not
# identified as the original program.
import re
from builtins import map
from builtins import str
from builtins import range
import re
from builtins import str

from captcha.fields import ReCaptchaField
from captcha.widgets import ReCaptchaV2Invisible
from common.models import Student, stripStudentName
from django import forms
from django.contrib.auth import authenticate
from django.contrib.auth.models import User
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth.models import User

from portal.helpers.password import form_clean_password
from portal.models import Student, stripStudentName

choices = [
("Miss", "Miss"),
Expand Down
3 changes: 1 addition & 2 deletions portal/helpers/generators.py
Expand Up @@ -40,10 +40,9 @@
from builtins import str
from uuid import uuid4

from common.models import Student, Class
from django.contrib.auth.models import User

from portal.models import Student, Class


def get_random_username():
while True:
Expand Down
6 changes: 2 additions & 4 deletions portal/migrations/0051_add_missing_ev_records.py
Expand Up @@ -4,15 +4,13 @@
from uuid import uuid4
from datetime import timedelta

from django.contrib.auth.models import User
from django.db import migrations
from django.utils import timezone

from portal.helpers.emails import generate_token
from portal.models import EmailVerification


def add_missing_ev_records(apps, schema_editor):
User = apps.get_model("auth", "User")
EmailVerification = apps.get_model("portal", "EmailVerification")
verified_users_without_ev = User.objects.filter(
userprofile__awaiting_email_verification=False
).exclude(email_verifications__verified=True)
Expand Down
214 changes: 214 additions & 0 deletions portal/migrations/0058_move_to_common_models.py
@@ -0,0 +1,214 @@
# -*- coding: utf-8 -*-
"""
This migration essentially "replaces" the majority of the models in models.py with
the models in common/models.py.
The models affected here are UserProfile, School, Teacher, Class and Student.
For each of these models, this migration:
- removes each of the model fields,
- updates Guardian's foreign keys to use the models in common/models.py,
- deletes the model, and updates the DB's references of these models to the new ones
created in common/models.py.
The important thing to note here is the use of migrations.SeparateDatabaseAndState.
This operation makes it possible to makes different changes to the state and to the
database. Essentially this is used here to replace Portal's models with Common's
models, without moving or deleting anything from the database.
This migration has been made following the tutorial on how to move Django models:
https://realpython.com/move-django-model/ (following the Django way example).
Since Rapid Router's models used to reference Portal's models, Rapid Router's migration
0071 needs to happen first, to avoid causing an error when migrating.
"""
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('common', '0001_initial'),
('game', '0071_use_common_models'),
('portal', '0057_delete_frontpagenews'),
]

operations = [
migrations.SeparateDatabaseAndState(
state_operations=[
migrations.RemoveField(
model_name='class',
name='teacher',
),
],
database_operations=[],
),
migrations.SeparateDatabaseAndState(
state_operations=[
migrations.RemoveField(
model_name='student',
name='class_field',
),
],
database_operations=[],
),
migrations.SeparateDatabaseAndState(
state_operations=[
migrations.RemoveField(
model_name='student',
name='new_user',
),
],
database_operations=[],
),
migrations.SeparateDatabaseAndState(
state_operations=[
migrations.RemoveField(
model_name='student',
name='pending_class_request',
),
],
database_operations=[],
),
migrations.SeparateDatabaseAndState(
state_operations=[
migrations.RemoveField(
model_name='student',
name='user',
),
],
database_operations=[],
),
migrations.SeparateDatabaseAndState(
state_operations=[
migrations.RemoveField(
model_name='teacher',
name='new_user',
),
],
database_operations=[],
),
migrations.SeparateDatabaseAndState(
state_operations=[
migrations.RemoveField(
model_name='teacher',
name='pending_join_request',
),
],
database_operations=[],
),
migrations.SeparateDatabaseAndState(
state_operations=[
migrations.RemoveField(
model_name='teacher',
name='school',
),
],
database_operations=[],
),
migrations.SeparateDatabaseAndState(
state_operations=[
migrations.RemoveField(
model_name='teacher',
name='user',
),
],
database_operations=[],
),
migrations.SeparateDatabaseAndState(
state_operations=[
migrations.RemoveField(
model_name='userprofile',
name='user',
),
],
database_operations=[],
),
migrations.SeparateDatabaseAndState(
state_operations=[
migrations.AlterField(
model_name='guardian',
name='children',
field=models.ManyToManyField(to='common.Student'),
),
],
# You're reusing an existing table, so do nothing
database_operations=[],
),
migrations.SeparateDatabaseAndState(
state_operations=[
migrations.AlterField(
model_name='guardian',
name='user',
field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='common.UserProfile'),
),
],
# You're reusing an existing table, so do nothing
database_operations=[],
),
migrations.SeparateDatabaseAndState(
state_operations=[
migrations.DeleteModel(
name='Class',
),
],
database_operations=[
migrations.AlterModelTable(
name='Class',
table='common_class',
),
],
),
migrations.SeparateDatabaseAndState(
state_operations=[
migrations.DeleteModel(
name='School',
),
],
database_operations=[
migrations.AlterModelTable(
name='School',
table='common_school',
),
],
),
migrations.SeparateDatabaseAndState(
state_operations=[
migrations.DeleteModel(
name='Student',
),
],
database_operations=[
migrations.AlterModelTable(
name='Student',
table='common_student',
),
],
),
migrations.SeparateDatabaseAndState(
state_operations=[
migrations.DeleteModel(
name='Teacher',
),
],
database_operations=[
migrations.AlterModelTable(
name='Teacher',
table='common_teacher',
),
],
),
migrations.SeparateDatabaseAndState(
state_operations=[
migrations.DeleteModel(
name='UserProfile',
),
],
database_operations=[
migrations.AlterModelTable(
name='UserProfile',
table='common_userprofile',
),
],
),
]

0 comments on commit 69af469

Please sign in to comment.