Skip to content

Commit

Permalink
Merge pull request #163 from UtsavBhamra/blog
Browse files Browse the repository at this point in the history
Migrated blogs from old site to new Django app (Envision project)
  • Loading branch information
yukitya-1811 authored Dec 6, 2024
2 parents 4872803 + 4d8fddb commit 9796f9b
Show file tree
Hide file tree
Showing 70 changed files with 8,609 additions and 232 deletions.
1,653 changes: 1,653 additions & 0 deletions corpus/accounts/authors_data.json

Large diffs are not rendered by default.

Empty file.
Empty file.
37 changes: 37 additions & 0 deletions corpus/accounts/management/commands/load_json_data_exec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import json

from accounts.models import ExecutiveMember
from accounts.models import User
from config.models import SIG
from django.core.management.base import BaseCommand


class Command(BaseCommand):
help = "to migrate authors from the json file to the ExecutiveMember model"

def add_arguments(self, parser):
parser.add_argument("json_file", type=str, help="Path to the json file")

def handle(self, *args, **kwargs):
json_file = kwargs["json_file"]

with open(json_file, "r") as file:
data = json.load(file)

counter = 0
for item in data.values():
edu_email = "noemail" + str(counter) + "@nitk.edu.in"
roll_number = "111GG" + str(counter)
reg_number = "1111" + str(counter)
model_sig = SIG.objects.all()[0]
user_model_obj = User.objects.get(email=item["email"])
new_user = ExecutiveMember(
sig=model_sig,
user=user_model_obj,
edu_email=edu_email,
roll_number=roll_number,
reg_number=reg_number,
)
new_user.save()
counter = counter + 1
print(f"user number {counter} with email created")
37 changes: 37 additions & 0 deletions corpus/accounts/management/commands/load_json_data_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import json

from accounts.models import User
from django.core.management.base import BaseCommand


class Command(BaseCommand):
help = "to migrate authors from the json file to the User model"

def add_arguments(self, parser):
parser.add_argument("json_file", type=str, help="Path to the json file")

def handle(self, *args, **kwargs):
json_file = kwargs["json_file"]

with open(json_file, "r") as file:
data = json.load(file)

counter = 0
for item in data.values():
phone_num = counter + 9999999000
new_user = User(
email=item["email"],
phone_no=phone_num,
gender="N",
)
new_user.save()
name_arr = item['name'].split()
first_name = name_arr[0]
last_name = name_arr[-1]
user = User.objects.get(email=item['email'])
user.first_name = first_name
user.last_name = last_name
user.save()

counter = counter + 1
print(f"user number={counter} created")
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
# Generated by Django 4.2.7 on 2024-06-29 07:27

# Generated by Django 4.2.7 on 2024-07-12 05:54
import datetime
from django.db import migrations, models

from django.db import migrations
from django.db import models


class Migration(migrations.Migration):

dependencies = [
('accounts', '0005_alter_executivemember_date_joined_and_more'),
("accounts", "0005_alter_executivemember_date_joined_and_more"),
]

operations = [
migrations.AlterField(
model_name='executivemember',
name='date_joined',
field=models.DateTimeField(default=datetime.datetime(2024, 6, 29, 7, 27, 12, 372509, tzinfo=datetime.timezone.utc), verbose_name='Date Joined'),
model_name="executivemember",
name="date_joined",
field=models.DateTimeField(
default=datetime.datetime(
2024, 7, 12, 5, 54, 40, 426496, tzinfo=datetime.timezone.utc
),
verbose_name="Date Joined",
),
),
]
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Generated by Django 4.2.7 on 2024-11-06 20:34

from django.db import migrations, models
import django.utils.timezone

Expand Down
9 changes: 7 additions & 2 deletions corpus/accounts/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@
path("signup/", signup, name="accounts_signup"),
path("login/", signin, name="accounts_signin"),
path("logout/", signout, name="accounts_signout"),
path("reset/", PasswordResetView.as_view(html_email_template_name="../templates/registration/password_reset_email.html"),
name="password_reset"),
path(
"reset/",
PasswordResetView.as_view(
html_email_template_name="../templates/registration/password_reset_email.html"
),
name="password_reset",
),
path("reset/done/", PasswordResetDoneView.as_view(), name="password_reset_done"),
path(
"reset/confirm/<uidb64>/<token>/",
Expand Down
Empty file added corpus/blog/__init__.py
Empty file.
7 changes: 7 additions & 0 deletions corpus/blog/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.contrib import admin

from .models import Post
from .models import Tag

admin.site.register(Post)
admin.site.register(Tag)
6 changes: 6 additions & 0 deletions corpus/blog/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class BlogConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "blog"
5,633 changes: 5,633 additions & 0 deletions corpus/blog/blog_data_cpy3.json

Large diffs are not rendered by default.

Empty file.
Empty file.
51 changes: 51 additions & 0 deletions corpus/blog/management/commands/load_blog_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import json
from datetime import datetime

from accounts.models import ExecutiveMember
from blog.models import Post
from blog.models import Tag
from django.core.management.base import BaseCommand


class Command(BaseCommand):
help = "to migrate authors from the json file to the User model"

def add_arguments(self, parser):
parser.add_argument("json_file", type=str, help="Path to the json file")

def handle(self, *args, **kwargs):
json_file = kwargs["json_file"]

with open(json_file, "r") as file:
data = json.load(file)

counter = 0
for post in data:
name_arr = post["author_name"].split()
first_name = name_arr[0]
last_name = name_arr[-1]
tags = Tag.objects.filter(tag_name=post["categories"][0])
author = ExecutiveMember.objects.filter(
user__first_name=first_name, user__last_name=last_name
)[0]
layout = post["layout"]
title = post["title"]
slug = post["slug"]
description = post["description"]
author_github = post["github_username"]
text = post["text"]
published_date = datetime.strptime(post["date"], "%Y-%m-%dT%H:%M:%S")
blog_post = Post(
layout=layout,
title=title,
author=author,
slug=slug,
description=description,
author_github=author_github,
text=text,
published_date=published_date,
)
blog_post.save()
blog_post.blog_tag.set(tags)
print(counter)
counter = counter + 1
33 changes: 33 additions & 0 deletions corpus/blog/media_url_change.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import json
import re
import os

# Get the directory of the script file
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))

# Define the input and output JSON file paths
INPUT_FILE = os.path.join(SCRIPT_DIR, "blog_data_cpy2.json")
OUTPUT_FILE = os.path.join(SCRIPT_DIR, "blog_data_cpy3.json")

# Regex pattern to match {% static 'path' %}
STATIC_PATTERN = r"{%\s*static\s*'([^']+)'?\s*%}"
REPLACEMENT = r"media\1"

def replace_static_in_json(input_file, output_file):
with open(input_file, "r", encoding="utf-8") as f:
data = json.load(f)

# Process each entry in the JSON file
for entry in data:
if "text" in entry:
# Replace static tags in the "text" field
entry["text"] = re.sub(STATIC_PATTERN, REPLACEMENT, entry["text"])

# Save the updated data back to a new file
with open(output_file, "w", encoding="utf-8") as f:
json.dump(data, f, indent=4, ensure_ascii=False)

print(f"Updated JSON saved to {output_file}")

# Run the function
replace_static_in_json(INPUT_FILE, OUTPUT_FILE)
60 changes: 60 additions & 0 deletions corpus/blog/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Generated by Django 4.2.7 on 2024-06-29 06:03
from django.db import migrations
from django.db import models


class Migration(migrations.Migration):

initial = True

dependencies = []

operations = [
migrations.CreateModel(
name="Tag",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"tag_name",
models.CharField(
choices=[
("CompSoc", "CompSoc"),
("Diode", "Diode"),
("Piston", "Piston"),
],
max_length=50,
),
),
],
),
migrations.CreateModel(
name="Post",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("layout", models.CharField(max_length=20)),
("title", models.CharField(max_length=200)),
("description", models.CharField(max_length=300)),
("author_github", models.CharField(max_length=70)),
("text", models.TextField()),
("created_date", models.DateTimeField(auto_now_add=True)),
("published_date", models.DateTimeField(blank=True, null=True)),
("blog_tag", models.ManyToManyField(to="blog.tag")),
],
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.2.7 on 2024-06-29 07:02
from django.db import migrations
from django.db import models


class Migration(migrations.Migration):

dependencies = [
("blog", "0001_initial"),
]

operations = [
migrations.AlterField(
model_name="post",
name="author_github",
field=models.CharField(blank=True, max_length=70, null=True),
),
migrations.AlterField(
model_name="post",
name="layout",
field=models.CharField(blank=True, max_length=20, null=True),
),
]
25 changes: 25 additions & 0 deletions corpus/blog/migrations/0003_post_author.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 4.2.7 on 2024-07-01 05:59
import django.db.models.deletion
from django.conf import settings
from django.db import migrations
from django.db import models


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("blog", "0002_alter_post_author_github_alter_post_layout"),
]

operations = [
migrations.AddField(
model_name="post",
name="author",
field=models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
),
),
]
24 changes: 24 additions & 0 deletions corpus/blog/migrations/0004_alter_post_author.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 4.2.7 on 2024-07-12 05:54
import django.db.models.deletion
from django.db import migrations
from django.db import models


class Migration(migrations.Migration):

dependencies = [
("accounts", "0006_alter_executivemember_date_joined"),
("blog", "0003_post_author"),
]

operations = [
migrations.AlterField(
model_name="post",
name="author",
field=models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.CASCADE,
to="accounts.executivemember",
),
),
]
18 changes: 18 additions & 0 deletions corpus/blog/migrations/0005_alter_post_published_date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.7 on 2024-07-12 14:49
from django.db import migrations
from django.db import models


class Migration(migrations.Migration):

dependencies = [
("blog", "0004_alter_post_author"),
]

operations = [
migrations.AlterField(
model_name="post",
name="published_date",
field=models.DateTimeField(),
),
]
Loading

0 comments on commit 9796f9b

Please sign in to comment.