Skip to content

Commit

Permalink
Merge branch 'unstructuredstudio:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
coderatomy authored Nov 30, 2023
2 parents 94115d5 + 7f5c5b0 commit a903801
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 188 deletions.
1 change: 1 addition & 0 deletions zubhub_backend/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
media_store/
media/
# migrations/
__pycache__/
*.pyc
Expand Down
154 changes: 0 additions & 154 deletions zubhub_backend/zubhub/static/js/main.js

This file was deleted.

22 changes: 1 addition & 21 deletions zubhub_backend/zubhub/zubhub/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,14 @@ class PrivacyAdmin(SummernoteModelAdmin):
summernote_fields = ('privacy_policy', 'terms_of_use',)
readonly_fields = ["edited_on"]

class Media:
js = ('https://code.jquery.com/jquery-3.1.1.js', 'js/main.js',)


class HelpAdmin(SummernoteModelAdmin):
summernote_fields = ('about',)
readonly_fields = ["edited_on"]

class Media:
js = ('https://code.jquery.com/jquery-3.1.1.js', 'js/main.js',)

class ChallengeAdmin(SummernoteModelAdmin):
summernote_fields = ('challenge',)
readonly_fields = ["edited_on"]

class Media:
js = ('https://code.jquery.com/jquery-3.1.1.js', 'js/main.js',)


class StatusEnum(Enum):
INACTIVE = 0
ACTIVE = 1
Expand All @@ -63,23 +52,14 @@ def make_selected_active(self, request, queryset):

make_selected_active.short_description = "Select and make active"


class FAQAdmin(SummernoteModelAdmin):
summernote_fields = ('answer',)

class Media:
js = ('https://code.jquery.com/jquery-3.1.1.js', 'js/main.js',)


class AmbassadorsAdmin(SummernoteModelAdmin):
summernote_fields = ('ambassadors',)
readonly_fields = ["edited_on"]
search_fields = ["projects"]

class Media:
js = ('https://code.jquery.com/jquery-3.1.1.js', 'js/main.js',)


admin.site.register(AdminSettings, AdminSettingsAdmin)
admin.site.register(Hero, HeroAdmin)
admin.site.register(Privacy, PrivacyAdmin)
Expand All @@ -96,4 +76,4 @@ class Media:
admin.site.unregister(Site)
admin.site.unregister(SocialAccount)
admin.site.unregister(SocialApp)
admin.site.unregister(SocialToken)
admin.site.unregister(SocialToken)
28 changes: 20 additions & 8 deletions zubhub_backend/zubhub/zubhub/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.db import models
from django.core.validators import FileExtensionValidator
from django.utils.text import slugify
from .utils import MediaStorage, get_upload_path, clean_summernote_html
from .utils import MediaStorage, get_upload_path, clean_summernote_html, replace_summernote_images_with_media_storage_equiv
from projects.models import Project
from django.utils.html import strip_tags

Expand Down Expand Up @@ -81,8 +81,12 @@ def __str__(self):
return self.edited_on.strftime("ZubHub's Guildlines, Policies and Terms of use as edited on %I:%M %p, %d %b %Y %Z")

def save(self, *args, **kwargs):
self.privacy_policy = clean_summernote_html(self.privacy_policy)
self.terms_of_use = clean_summernote_html(self.terms_of_use)
self.privacy_policy = clean_summernote_html(
replace_summernote_images_with_media_storage_equiv(self.privacy_policy)
)
self.terms_of_use = clean_summernote_html(
replace_summernote_images_with_media_storage_equiv(self.terms_of_use)
)
self.edited_on = timezone.now()
super().save(*args, **kwargs)

Expand All @@ -99,7 +103,9 @@ def __str__(self):
return self.edited_on.strftime("About Zubhub as edited on %I:%M %p, %d %b %Y %Z")

def save(self, *args, **kwargs):
self.about = clean_summernote_html(self.about)
self.about = clean_summernote_html(
replace_summernote_images_with_media_storage_equiv(self.about)
)
self.edited_on = timezone.now()
super().save(*args, **kwargs)

Expand All @@ -115,7 +121,9 @@ def __str__(self):
return self.edited_on.strftime("Challenges as edited on %I:%M %p, %d %b %Y %Z")

def save(self, *args, **kwargs):
self.challenge = clean_summernote_html(self.challenge)
self.challenge = clean_summernote_html(
replace_summernote_images_with_media_storage_equiv(self.challenge)
)
self.edited_on = timezone.now()
super().save(*args, **kwargs)

Expand All @@ -131,8 +139,12 @@ def __str__(self):
return self.question

def save(self, *args, **kwargs):
self.question = clean_summernote_html(self.question)
self.answer = clean_summernote_html(self.answer)
self.question = clean_summernote_html(
replace_summernote_images_with_media_storage_equiv(self.question)
)
self.answer = clean_summernote_html(
replace_summernote_images_with_media_storage_equiv(self.answer)
)
super().save(*args, **kwargs)


Expand Down Expand Up @@ -178,4 +190,4 @@ def save(self, *args, **kwargs):
super().save(*args, **kwargs)

def __str__(self):
return self.Theme_Name
return self.Theme_Name
3 changes: 2 additions & 1 deletion zubhub_backend/zubhub/zubhub/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from django.contrib import admin
from django.urls import path, include
from django.views.generic import TemplateView
from django.conf.urls.static import static


schema_url_patterns = []
Expand All @@ -34,7 +35,7 @@
import debug_toolbar
urlpatterns = [
path('__debug__/', include(debug_toolbar.urls)),
] + urlpatterns
] + urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


if settings.DEFAULT_BACKEND_DOMAIN.startswith("localhost"):
Expand Down
25 changes: 25 additions & 0 deletions zubhub_backend/zubhub/zubhub/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from lxml.html.clean import Cleaner
import uuid
from math import floor
from pathlib import Path
import re
from django.utils.text import slugify
from django.core.files.storage import Storage
from django.core.files.base import ContentFile
Expand Down Expand Up @@ -56,6 +58,29 @@ def clean_summernote_html(string):
return cleaner.clean_html(string)


def replace_summernote_images_with_media_storage_equiv(text):
"""
Extract all summernote image paths in the html text,
upload them to media storage server,
and replace them with the response url from the media storage server
"""
regex = r'<img[^<>]+src=["\']/api/media/([^"\'<>]+\.(?:gif|png|jpe?g))["\']'
temp_image_paths = re.findall(regex, text, re.I) # e.g. ["django-summernote/2023-09-03/image.jpg", "django-summernote/2023-09-03/image2.jpg"]
for temp_image_path in temp_image_paths:
path = Path(settings.MEDIA_ROOT, temp_image_path)
with path.open(mode="rb") as file:
key = get_upload_path(
type('', (object,), {'MEDIA_PATH': 'zubhub'}),
slugify(path.name)
)
image_url = upload_file_to_media_server(file=file, key=key).json()["url"]
full_temp_image_url = settings.MEDIA_URL + str(temp_image_path)
text = image_url.join(text.split(full_temp_image_url))
path.unlink()
return text





#========================= Docs helper functions =======================
Expand Down
22 changes: 18 additions & 4 deletions zubhub_frontend/zubhub/src/views/create_team/step2/Step2.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export default function Step2({ formik }) {
const [adminSuggestions, setAdminSuggestions] = useState([]);
const [memberSuggestions, setMemberSuggestions] = useState([]);

const { values: { members, admins} } = formik

const allMembers = [...(admins ? admins : []), ...(members ? members : [])]

const handleAdminSelect = (event, value) => {
setSelectedAdmins(value);
setAdminsInputValue('');
Expand All @@ -35,12 +39,17 @@ export default function Step2({ formik }) {
// Perform API call to get admin suggestions based on the input value
try {
const completions = await api.autocompleteCreators({ query: value });
const suggestions = completions.map(({ username, avatar }) => ({
const suggestions = completions.map(({ username, avatar, id }) => ({
title: username,
image: avatar,
link: `/creators/${username}`,
id
}));
setAdminSuggestions(suggestions);

const filteredSuggestions = suggestions.filter(({ id }) =>
!allMembers.some(member => member.id === id)
);
setAdminSuggestions(filteredSuggestions);
} catch (error) {
console.error('Error fetching admin suggestions:', error);
setAdminSuggestions([]);
Expand All @@ -54,12 +63,17 @@ export default function Step2({ formik }) {
// Perform API call to get member suggestions based on the input value
try {
const completions = await api.autocompleteCreators({ query: value });
const suggestions = completions.map(({ username, avatar }) => ({
const suggestions = completions.map(({ username, avatar, id }) => ({
title: username,
image: avatar,
link: `/creators/${username}`,
id
}));
setMemberSuggestions(suggestions);

const filteredSuggestions = suggestions.filter(({ id }) =>
!allMembers.some(member => member.id === id)
);
setMemberSuggestions(filteredSuggestions);
} catch (error) {
console.error('Error fetching member suggestions:', error);
setMemberSuggestions([]);
Expand Down

0 comments on commit a903801

Please sign in to comment.