Skip to content

Commit

Permalink
Merge branch 'main' of github.com:2076-Collective/2077-CMS
Browse files Browse the repository at this point in the history
  • Loading branch information
iankressin committed Aug 21, 2024
2 parents 889b2eb + 95ca40a commit a118114
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 90 deletions.
150 changes: 67 additions & 83 deletions research/src/layouts/Footer.astro
Original file line number Diff line number Diff line change
Expand Up @@ -141,91 +141,75 @@

<script>
document.addEventListener('DOMContentLoaded', () => {
const form = document.getElementById(
'subscribe-form',
) as HTMLFormElement

if (form) {
fetch('https://cms.2077.xyz/get-csrf-token/')
.then((response) => response.json())
.then((data: { csrfToken: string }) => {
const csrfToken = data.csrfToken

const input = document.createElement('input')
input.type = 'hidden'
input.name = 'csrfmiddlewaretoken'
input.value = csrfToken
form.appendChild(input)

form.addEventListener('submit', (event) => {
event.preventDefault()

const formData = new FormData(form)

fetch(form.action, {
method: 'POST',
body: formData,
headers: {
'X-CSRFToken': csrfToken,
},
})
.then((response) => {
if (response.ok) {
return response.json()
} else {
return response
.json()
.then(
(data: {
message: string
}) => {
throw new Error(
data.message ||
'An error occurred',
)
},
)
}
})
.then(() => {
const messageElement =
document.getElementById(
'response-message',
) as HTMLElement
if (messageElement) {
messageElement.textContent =
'Subscription successful!'
messageElement.style.color = 'green'
}
})
.catch((error: Error) => {
const messageElement =
document.getElementById(
'response-message',
) as HTMLElement
if (messageElement) {
messageElement.textContent =
error.message ||
'An error occurred. Please try again.'
messageElement.style.color = 'red'
}
})
})
})
.catch((error: Error) => {
console.error('Error fetching CSRF token:', error)
const messageElement = document.getElementById(
'response-message',
) as HTMLElement
if (messageElement) {
messageElement.textContent =
'An error occurred while fetching the CSRF token. Please try again.'
messageElement.style.color = 'red'
}
const form = document.getElementById('subscribe-form');

if (form) {
// Fetch CSRF token
fetch('https://cms.2077.xyz/get-csrf-token/', {
credentials: 'include' // Ensure cookies are sent with the request
})
.then(response => response.json())
.then(data => {
const csrfToken = data.csrfToken;

// Add CSRF token as a hidden input in the form
const input = document.createElement('input');
input.type = 'hidden';
input.name = 'csrfmiddlewaretoken';
input.value = csrfToken;
form.appendChild(input);

// Handle form submission
form.addEventListener('submit', (event) => {
event.preventDefault();

const formData = new FormData(form);

fetch(form.action, {
method: 'POST',
body: formData,
headers: {
'X-CSRFToken': csrfToken,
},
credentials: 'include' // Ensure cookies are sent with the request
})
}
.then(response => {
if (response.ok) {
return response.json();
} else {
return response.json().then(data => {
throw new Error(data.message || 'An error occurred');
});
}
})
.then(() => {
const messageElement = document.getElementById('response-message');
if (messageElement) {
messageElement.textContent = 'Subscription successful!';
messageElement.style.color = 'green';
}
})
.catch((error) => {
const messageElement = document.getElementById('response-message');
if (messageElement) {
messageElement.textContent = error.message || 'An error occurred. Please try again.';
messageElement.style.color = 'red';
}
});
});
})
</script>
.catch(error => {
console.error('Error fetching CSRF token:', error);
const messageElement = document.getElementById('response-message');
if (messageElement) {
messageElement.textContent = 'An error occurred while fetching the CSRF token. Please try again.';
messageElement.style.color = 'red';
}
});
}

});
</script>
</div>

<div class="grid mt-16 flex justify-center">
Expand Down
2 changes: 1 addition & 1 deletion research/src/layouts/Layout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ h1, h2, h3 {
font-family: 'avenir_black';
}

p, a, span, ul, li, button, input {
p, a, ul, li, button, input {
font-family: 'avenir_book';
}

Expand Down
4 changes: 3 additions & 1 deletion server/apps/research/admin/article_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):
"""Admin interface for the Article model."""

class Media:
# Reference the JavaScript file
js = ('ckeditor/js/custom_ckeditor_admin.js',)
fieldsets = [
('Article Details', {'fields': ['title', 'authors', 'categories', 'thumb', 'content', 'summary', 'status', 'scheduled_publish_time']}),
]
Expand Down
19 changes: 19 additions & 0 deletions server/apps/research/migrations/0009_alter_article_thumb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 5.0.8 on 2024-08-21 12:39

import apps.research.models.article
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('research', '0008_alter_article_content'),
]

operations = [
migrations.AlterField(
model_name='article',
name='thumb',
field=models.ImageField(blank=True, default=apps.research.models.article.get_default_thumb, upload_to='images/'),
),
]
1 change: 0 additions & 1 deletion server/core/config/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_URL = 'https://cms.2077.xyz/media/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

Expand Down
46 changes: 45 additions & 1 deletion server/core/config/ckeditor.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
# CKEDITOR CONFIGS
CKEDITOR_UPLOAD_PATH = "ckeditor_uploads/"

iframeEditing = True
removePlugins = 'link'
allowedContent = True
htmlEncodeOutput = True
disallowedContent = {'script', 'iframe', 'javascript'}

CKEDITOR_IMAGE_BACKEND = "pillow"

CKEDITOR_5_ALLOW_ALL_FILE_TYPES = True

CKEDITOR_5_CUSTOM_CSS = "custom.css"

CKEDITOR_5_CUSTOM_JS = {
'pastefilter': '''
CKEDITOR.plugins.add('pastefilter', {
init: function(editor) {
editor.on('paste', function(evt) {
evt.preventDefault();
evt.stopPropagation();
var data = evt.data;
data.html = data.html.replace(/<a[^>]*>/g, function(match) {
return match.replace('href', 'data-href');
});
});
}
});
'''
}

customColorPalette = [
{"color": "hsl(4, 90%, 58%)", "label": "Red"},
{"color": "hsl(340, 82%, 52%)", "label": "Pink"},
Expand All @@ -18,7 +41,28 @@


CKEDITOR_5_CONFIGS = {
'default': {
'default': {
'paste': {
'forcePlainText': True,
'linkCreation': False,
},
'htmlSupport': {
'allow': [
'a',
# Other allowed HTML elements...
],
'disallow': [
'a[href]', # Disallow links with href attribute
# Other disallowed HTML elements...
],
},
'extraPlugins': ','.join(['sourcearea']),
'removePlugins': 'Link', # Disables automatic link handling
# other configurations...
'link': {
'addTargetToExternalLinks': False,
'defaultProtocol': 'https://', # Prevents redirection by controlling protocol behavior
},
"toolbar": [
"heading",
"|",
Expand Down
1 change: 1 addition & 0 deletions server/core/config/local.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .base import *

MEDIA_URL = '/media/'

CSRF_TRUSTED_ORIGINS = [
'http://127.0.0.1:8000',
Expand Down
20 changes: 17 additions & 3 deletions server/core/config/production.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
from .base import *

MEDIA_URL = 'https://cms.2077.xyz/media/'

DEBUG = bool(config('DJANGO_DEBUG', default=False))

ALLOWED_HOSTS = ['74.119.195.253', 'cms.2077.xyz']

CSRF_TRUSTED_ORIGINS = ["https://" + host for host in ALLOWED_HOSTS]
CSRF_TRUSTED_ORIGINS = []
for host in ALLOWED_HOSTS:
CSRF_TRUSTED_ORIGINS.append(f"https://{host}")
CSRF_TRUSTED_ORIGINS.append(f"http://{host}")

CORS_ALLOWED_ORIGINS = [
"https://cms.2077.xyz", # Https version of Django
"http://74.119.195.253", # Http version of Astro
"http://127.0.0.1:4321", # Local Dev
"http://localhost:4321", # Local Dev
]

CORS_ALLOW_CREDENTIALS = True

#REDISCLOUD_URL = config("REDISCLOUD_URL")

Expand All @@ -27,7 +41,7 @@

CSP_SCRIPT_SRC = ("self",)

CSP_IMG_SRC = ("self", "http://cms.2077.xyz", "https:cms.2077.xyz")
CSP_IMG_SRC = ("self", "http://cms.2077.xyz", "https://cms.2077.xyz", "http://74.119.195.253")

CSP_FONT_SRC = ("self",)

Expand All @@ -43,4 +57,4 @@

SECURE_SSL_REDIRECT = False

SESSION_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True
Binary file removed server/db.sqlite3
Binary file not shown.
Binary file added server/media/images/stm.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions server/static/ckeditor/js/custom_ckeditor_admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
document.addEventListener("DOMContentLoaded", function () {
const editorElement = document.querySelector("#id_contents"); // Adjust to match your actual element ID
if (editorElement) {
ClassicEditor.create(editorElement, {
link: {
decorators: {
addTargetToExternalLinks: false,
},
},
removePlugins: ["Link"],
toolbar: {
items: [
"heading",
"|",
"bold",
"italic",
"link",
"bulletedList",
"numberedList",
"blockQuote",
],
},
})
.then((editor) => {
console.log("CKEditor5 initialized successfully");
})
.catch((error) => {
console.error("Error initializing CKEditor:", error);
});
} else {
console.error("CKEditor5 element not found");
}
});

0 comments on commit a118114

Please sign in to comment.