Skip to content

Commit

Permalink
Add option for adding custom CSS
Browse files Browse the repository at this point in the history
  • Loading branch information
sissbruecker committed Mar 16, 2024
1 parent 39782e7 commit fb74aa6
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 2 deletions.
2 changes: 2 additions & 0 deletions bookmarks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ class UserProfile(models.Model):
enable_favicons = models.BooleanField(default=False, null=False)
display_url = models.BooleanField(default=False, null=False)
permanent_notes = models.BooleanField(default=False, null=False)
custom_css = models.TextField(blank=True, null=False)
search_preferences = models.JSONField(default=dict, null=False)


Expand All @@ -348,6 +349,7 @@ class Meta:
"enable_favicons",
"display_url",
"permanent_notes",
"custom_css",
]


Expand Down
4 changes: 4 additions & 0 deletions bookmarks/styles/settings.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
}
}

textarea.custom-css {
font-family: monospace;
}

.input-group > input[type=submit] {
height: auto;
}
Expand Down
3 changes: 3 additions & 0 deletions bookmarks/templates/bookmarks/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
<meta name="theme-color" media="(prefers-color-scheme: dark)" content="#161822">
<meta name="theme-color" media="(prefers-color-scheme: light)" content="#5856e0">
{% endif %}
{% if request.user_profile.custom_css %}
<style>{{ request.user_profile.custom_css }}</style>
{% endif %}
</head>
<body ld-global-shortcuts>

Expand Down
12 changes: 12 additions & 0 deletions bookmarks/templates/settings/general.html
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,18 @@ <h2>Profile</h2>
href="{% url 'bookmarks:shared' %}">shared bookmarks page</a>.
</div>
</div>
<div class="form-group">
<details {% if form.custom_css.value %}open{% endif %}>
<summary>Custom CSS</summary>
<label for="{{ form.custom_css.id_for_label }}" class="text-assistive">Custom CSS</label>
<div class="mt-2">
{{ form.custom_css|add_class:"form-input custom-css"|attr:"rows:6" }}
</div>
</details>
<div class="form-input-hint">
Allows to add custom CSS to the page.
</div>
</div>
<div class="form-group">
<input type="submit" name="update_profile" value="Save" class="btn btn-primary mt-2">
{% if update_profile_success_message %}
Expand Down
21 changes: 21 additions & 0 deletions bookmarks/tests/test_custom_css.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from django.test import TestCase
from django.urls import reverse

from bookmarks.tests.helpers import BookmarkFactoryMixin


class CustomCssTestCase(TestCase, BookmarkFactoryMixin):
def setUp(self):
self.client.force_login(self.get_or_create_test_user())

def test_does_not_render_custom_style_tag_by_default(self):
response = self.client.get(reverse("bookmarks:index"))
self.assertNotContains(response, "<style>")

def test_renders_custom_style_tag_if_user_has_custom_css(self):
profile = self.get_or_create_test_user().profile
profile.custom_css = "body { background-color: red; }"
profile.save()

response = self.client.get(reverse("bookmarks:index"))
self.assertContains(response, "<style>body { background-color: red; }</style>")
3 changes: 3 additions & 0 deletions bookmarks/tests/test_settings_general_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def create_profile_form_data(self, overrides=None):
"tag_search": UserProfile.TAG_SEARCH_STRICT,
"display_url": False,
"permanent_notes": False,
"custom_css": "",
}

return {**form_data, **overrides}
Expand Down Expand Up @@ -63,6 +64,7 @@ def test_update_profile(self):
"tag_search": UserProfile.TAG_SEARCH_LAX,
"display_url": True,
"permanent_notes": True,
"custom_css": "body { background-color: #000; }",
}
response = self.client.post(reverse("bookmarks:settings.general"), form_data)
html = response.content.decode()
Expand Down Expand Up @@ -93,6 +95,7 @@ def test_update_profile(self):
self.assertEqual(
self.user.profile.permanent_notes, form_data["permanent_notes"]
)
self.assertEqual(self.user.profile.custom_css, form_data["custom_css"])
self.assertInHTML(
"""
<p class="form-input-hint">Profile updated</p>
Expand Down
2 changes: 0 additions & 2 deletions siteroot/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@

# Turn off SASS compilation by default
SASS_PROCESSOR_ENABLED = False
# Location where generated CSS files are saved
SASS_PROCESSOR_ROOT = os.path.join(BASE_DIR, "bookmarks", "static")

# Add SASS preprocessor finder to resolve generated CSS
STATICFILES_FINDERS = [
Expand Down

0 comments on commit fb74aa6

Please sign in to comment.