Skip to content

Commit

Permalink
add bookmark function
Browse files Browse the repository at this point in the history
  • Loading branch information
cuongnb14 committed Oct 21, 2021
1 parent 08a0446 commit 7a4a6ce
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 3 deletions.
32 changes: 32 additions & 0 deletions admin_reskin/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from django.contrib import admin
from django.http import HttpResponse
from django.urls import path
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt

from . import models


@admin.register(models.Bookmark)
class BookmarkAdmin(admin.ModelAdmin):
list_display = ('name', 'url', 'is_active')
list_filter = ('is_active',)
list_editable = ('is_active',)

def get_urls(self):
urls = super().get_urls()
custom_urls = [
path('add-bookmark', self.admin_site.admin_view(self.add_bookmark_view), name='admin_reskin_add_bookmark'),
]
return custom_urls + urls

@method_decorator(csrf_exempt)
def add_bookmark_view(self, request):
if request.method == 'POST':
name = request.POST.get('name')
url = request.POST.get('url')
models.Bookmark.objects.create(
name=name,
url=url,
)
return HttpResponse()
23 changes: 23 additions & 0 deletions admin_reskin/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.2 on 2021-10-21 03:43

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Bookmark',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=45)),
('url', models.CharField(max_length=1000)),
('is_active', models.BooleanField(default=True)),
],
),
]
Empty file.
10 changes: 10 additions & 0 deletions admin_reskin/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.db import models


class Bookmark(models.Model):
name = models.CharField(max_length=45)
url = models.CharField(max_length=1000)
is_active = models.BooleanField(default=True)

def __str__(self):
return self.name
2 changes: 2 additions & 0 deletions admin_reskin/templates/admin/app_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

{% if app_list %}
{% for app in app_list|sort_apps %}
{% if not app.app_label == 'admin_reskin_bookmark' or request.user.is_superuser %}
<div class="app-{{ app.app_label }} module{% if app.app_url in request.path %} current-app{% endif %}">
<table>
<caption data-app-url="{{ app.app_url }}">
Expand Down Expand Up @@ -37,6 +38,7 @@
{% endfor %}
</table>
</div>
{% endif %}
{% endfor %}
{% else %}
<p>{% translate 'You don’t have permission to view or edit anything.' %}</p>
Expand Down
45 changes: 45 additions & 0 deletions admin_reskin/templates/admin/base_site.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@
<script src="{% static 'fontawesomefree/js/all.min.js' %}"></script>
<link href="{% static 'fontawesomefree/css/all.min.css' %}" rel="stylesheet" type="text/css">
<style>
.btn-bookmark {
position:fixed;
width:40px;
height:40px;
bottom:40px;
right:40px;
background-color:#417690;
color:#FFF;
border-radius:50px;
text-align:center;
}

.btn-bookmark:hover {
cursor: pointer;
}

.btn-bookmark .icon {
margin-top: 14px;
}

/* for default btn */
.d-btn {
background-color: #ffffff;
Expand Down Expand Up @@ -163,6 +183,10 @@
text-decoration: none;
}

#nav-sidebar tr a:hover {
color: #6cb6db;
}

#nav-sidebar td, #nav-sidebar th {
border: none;
font-weight: normal;
Expand Down Expand Up @@ -214,8 +238,29 @@
$(this).next().toggle(0);
}
})

$('.btn-bookmark').click(function () {
let name = prompt('Bookmark name:', '');
let data = {
name: name,
url: window.location.pathname
}
$.post(
"{% url 'admin:admin_reskin_add_bookmark' %}",
data,
function( data ) {
alert('Bookmark added');
}
);
})
})


</script>
{% endblock %}

{% block footer %}
<div id="footer">
<span class="btn-bookmark"><i class="icon far fa-bookmark"></i></span>
</div>
{% endblock %}
33 changes: 30 additions & 3 deletions admin_reskin/templatetags/sort_menu_items.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from django import template
from django.conf import settings

from ..models import Bookmark

register = template.Library()

RESKIN_MENU_APP_ORDER = settings.RESKIN_MENU_APP_ORDER if hasattr(settings, 'RESKIN_MENU_APP_ORDER') else []
RESKIN_MENU_MODEL_ORDER = settings.RESKIN_MENU_MODEL_ORDER if hasattr(settings, 'RESKIN_MENU_MODEL_ORDER') else []
RESKIN_APP_ICON = settings.RESKIN_APP_ICON if hasattr(settings, 'RESKIN_APP_ICON') else {'user': 'fas fa-user', 'auth': 'fas fa-users',}
RESKIN_MENU_APP_ORDER = settings.RESKIN_MENU_APP_ORDER
RESKIN_MENU_MODEL_ORDER = settings.RESKIN_MENU_MODEL_ORDER
RESKIN_APP_ICON = settings.RESKIN_APP_ICON


@register.filter
Expand All @@ -25,6 +27,31 @@ def sort_apps(apps):
if x['app_label'] in RESKIN_MENU_APP_ORDER
else max_index
)

bookmarks = Bookmark.objects.filter(is_active=True)
bookmarks_model = []
for bookmark in bookmarks:
item = {
'name': bookmark.name,
'object_name': bookmark.name,
'perms': {'add': False, 'change': False, 'delete': False, 'view': True},
'admin_url': bookmark.url,
'view_only': True,
}
bookmarks_model.append(item)

if bookmarks_model:
bookmark_app = {
'name': 'Bookmark',
'icon': 'fas fa-bookmark',
'app_label': 'admin_reskin_bookmark',
'app_url': '/admin/admin_reskin/bookmark',
'has_module_perms': True,
'models': bookmarks_model,
}

apps = [bookmark_app] + apps

return apps


Expand Down

0 comments on commit 7a4a6ce

Please sign in to comment.