Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
MOOC packages by hacklab
django-courses
Commits
7dfe1684
Commit
7dfe1684
authored
Dec 07, 2021
by
Matheus Miranda
Browse files
Update migration for generic relation
parent
f595786b
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
42 additions
and
8 deletions
+42
-8
courses/models.py
courses/models.py
+3
-0
courses/scheduling/migrations/0001_initial.py
courses/scheduling/migrations/0001_initial.py
+5
-1
courses/scheduling/models.py
courses/scheduling/models.py
+11
-6
courses/signals.py
courses/signals.py
+23
-1
No files found.
courses/models.py
View file @
7dfe1684
...
...
@@ -24,6 +24,7 @@ from .notes.models import Note
from
.course_material.models
import
CourseMaterial
from
.classes.models
import
Class
from
.classroom.models
import
Classroom
,
Event
from
.scheduling.models
import
ScheduledTask
import
re
...
...
@@ -948,6 +949,7 @@ class Lesson(PositionedModel):
blank
=
True
,
default
=
None
,
)
scheduled_task
=
GenericRelation
(
ScheduledTask
,
related_query_name
=
'lesson'
)
collection_name
=
'course'
class
Meta
:
...
...
@@ -1047,6 +1049,7 @@ class Unit(PositionedModel):
blank
=
True
,
default
=
None
,
)
scheduled_task
=
GenericRelation
(
ScheduledTask
,
related_query_name
=
'unit'
)
collection_name
=
'lesson'
...
...
courses/scheduling/migrations/0001_initial.py
View file @
7dfe1684
# Generated by Django 2.2.24 on 2021-12-07 1
2:11
# Generated by Django 2.2.24 on 2021-12-07 1
3:32
from
django.db
import
migrations
,
models
import
django.db.models.deletion
class
Migration
(
migrations
.
Migration
):
...
...
@@ -8,6 +9,7 @@ class Migration(migrations.Migration):
initial
=
True
dependencies
=
[
(
'contenttypes'
,
'0002_remove_content_type_name'
),
]
operations
=
[
...
...
@@ -16,9 +18,11 @@ class Migration(migrations.Migration):
fields
=
[
(
'id'
,
models
.
AutoField
(
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
verbose_name
=
'ID'
)),
(
'task_hash'
,
models
.
CharField
(
blank
=
True
,
max_length
=
255
,
verbose_name
=
'Name'
)),
(
'object_id'
,
models
.
PositiveIntegerField
()),
(
'release_date'
,
models
.
DateTimeField
(
blank
=
True
,
default
=
None
,
null
=
True
,
verbose_name
=
'Date'
)),
(
'created_at'
,
models
.
DateTimeField
(
auto_now_add
=
True
)),
(
'updated_at'
,
models
.
DateTimeField
(
auto_now
=
True
)),
(
'content_type'
,
models
.
ForeignKey
(
on_delete
=
django
.
db
.
models
.
deletion
.
CASCADE
,
to
=
'contenttypes.ContentType'
)),
],
options
=
{
'verbose_name'
:
'ScheduledTask'
,
...
...
courses/scheduling/models.py
View file @
7dfe1684
from
django.db
import
models
from
django.conf
import
settings
from
django.urls
import
reverse_lazy
from
courses.models
import
Lesson
,
Unit
from
django.utils.translation
import
ugettext_lazy
as
_
from
django.contrib.contenttypes.fields
import
GenericRelation
from
django.contrib.contenttypes.fields
import
GenericForeignKey
from
django.contrib.contenttypes.models
import
ContentType
class
ScheduledTask
(
models
.
Model
):
...
...
@@ -18,9 +17,15 @@ class ScheduledTask(models.Model):
max_length
=
255
,
blank
=
True
,
)
related_obj
=
GenericRelation
(
Lesson
,
Unit
,)
content_type
=
models
.
ForeignKey
(
ContentType
,
models
.
CASCADE
,
)
object_id
=
models
.
PositiveIntegerField
()
content_object
=
GenericForeignKey
(
'content_type'
,
'object_id'
,
)
release_date
=
models
.
DateTimeField
(
_
(
'Date'
),
null
=
True
,
...
...
courses/signals.py
View file @
7dfe1684
from
django.db.models.signals
import
pre_save
from
django.dispatch
import
receiver
from
django.contrib.contenttypes.models
import
ContentType
from
.models
import
Lesson
,
Unit
from
.scheduling.models
import
ScheduledTask
from
.tasks
import
publish_lesson
,
publish_unit
from
celery.result
import
AsyncResult
@
receiver
(
pre_save
,
sender
=
Lesson
)
def
lesson_created_or_updated
(
sender
,
instance
:
Lesson
,
**
kwargs
):
...
...
@@ -11,7 +15,25 @@ def lesson_created_or_updated(sender, instance: Lesson, **kwargs):
previous_lesson
=
Lesson
.
objects
.
get
(
id
=
instance
.
id
)
if
(
not
instance
.
id
or
instance
.
release_date
!=
previous_lesson
.
release_date
)
and
instance
.
release_date
:
publish_lesson
.
apply_async
(
args
=
[
instance
.
id
],
eta
=
instance
.
release_date
)
previous_task_hask
=
''
ct
=
ContentType
.
objects
.
get_for_model
(
comment
)
st
=
ScheduledTask
.
objects
.
filter
(
Content_type
=
ct
,
object_id
=
instance
.
id
)
if
st
.
exists
():
previous_task_hask
=
st
.
task_hash
else
:
st
=
ScheduledTask
.
objects
.
create
(
content_object
=
instance
)
if
previous_task_hask
:
res
=
AsyncResult
(
previous_task_hask
)
res
.
revoke
()
new_task_hash
=
publish_lesson
.
apply_async
(
args
=
[
instance
.
id
],
eta
=
instance
.
release_date
)
st
.
task_hash
=
task_hash
st
.
content_object
=
instance
st
.
release_date
=
instance
.
release_date
st
.
save
()
@
receiver
(
pre_save
,
sender
=
Unit
)
def
unit_created_or_updated
(
sender
,
instance
:
Unit
,
**
kwargs
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment