-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/jsolly/blogthedata into l…
…imit-snippet-characters
- Loading branch information
Showing
21 changed files
with
264 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,6 @@ | |
|
||
class BlogConfig(AppConfig): | ||
name = "blog" | ||
|
||
def ready(self): | ||
import blog.signals |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from django.core.management.base import BaseCommand | ||
import json | ||
from blog.models import Post, Category | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Load a list of posts from a JSON file into the Post model" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument("json_file", type=str, help="The JSON file to load") | ||
|
||
def handle(self, *args, **kwargs): | ||
json_file = kwargs["json_file"] | ||
with open(json_file, "r") as f: | ||
posts_json = json.load(f) | ||
|
||
for post_data in posts_json: | ||
category = Category.objects.get(slug=post_data["category_slug"]) | ||
|
||
post = Post( | ||
title=post_data["title"], | ||
content=post_data["content"], | ||
author_id=post_data["user_id"], | ||
category=category, | ||
) | ||
post.save() | ||
|
||
self.stdout.write( | ||
self.style.SUCCESS(f"Successfully imported posts from {json_file}") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Generated by Django 4.2.5 on 2023-11-08 23:37 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("blog", "0041_disallow_null_values_date_updated"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="Similarity", | ||
fields=[ | ||
( | ||
"id", | ||
models.AutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
("score", models.FloatField()), | ||
( | ||
"post1", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="similarities1", | ||
to="blog.post", | ||
), | ||
), | ||
( | ||
"post2", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="similarities2", | ||
to="blog.post", | ||
), | ||
), | ||
], | ||
), | ||
migrations.AddConstraint( | ||
model_name="similarity", | ||
constraint=models.UniqueConstraint( | ||
fields=("post1", "post2"), name="unique_pair" | ||
), | ||
), | ||
] |
18 changes: 18 additions & 0 deletions
18
app/blog/migrations/0043_update_simularities_on_existing_posts.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from django.db import migrations | ||
from blog.utils import compute_similarity | ||
|
||
|
||
def update_similarity(apps, schema_editor): | ||
Post = apps.get_model("blog", "Post") | ||
for post in Post.objects.all(): | ||
compute_similarity(post.id) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("blog", "0042_add_simularity_model"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(update_similarity), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from django.db.models.signals import post_save | ||
from django.dispatch import receiver | ||
from .utils import compute_similarity | ||
from .models import Post | ||
|
||
|
||
@receiver(post_save, sender=Post) | ||
def trigger_similarity_computation(sender, instance, **kwargs): | ||
compute_similarity(instance.id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.