How to add calendar events with Django-Bootstrap-Calendar? - django

Django-Bootstrap-Calendar (https://github.com/sandlbn/django-bootstrap-calendar) is a Django implementation of this calendar application: http://bootstrap-calendar.azurewebsites.net/
However, I can't figure out how to add events to the calendar via Django. The "non-Django" bootstrap calendar is rather simple, you just add events to the JSON file that gets loaded.
Has anyone here added events to Django-Bootstrap-Calendar before? I emailed the author of the project, however, he never responded to me.
The closest post I found to my question is here: Getting a bootstrap-calendar component to work with Django However, the author figured out how to add entries to the non-Django calendar only.
Edit: I figured out how to add events via the Admin panel.
In models.py, you simply add:
###Admin
class CalendarEventAdmin(admin.ModelAdmin):
list_display = ["title", "url", "css_class", "start", "end"]
list_filler = ["title"]
admin.site.register(CalendarEvent,CalendarEventAdmin)
Then, in the admin panel you can add events (ultimately, I want non-admin users and anonymous visitors to be able to register events on the Calendar.)
However, this reveals a larger problem: the calendar doesn't display.
Edit 2: I have the calendar loading now by adding
{% load bootstrap_calendar %}
{% bootstrap_calendar_css %}
{% bootstrap_calendar_js language="template" %}
{% bootstrap_calendar_init language="template" %}
<!-- {% bootstrap_controls 'optional-css-classes' %} -->
{% bootstrap_calendar 'optional-css-classes' %}
to my index.html file. However, the calendar doesn't display the event(s) created with the Admin Panel.

Simply put
{% bootstrap_calendar_init language="template" %}
after
{% bootstrap_calendar 'optional-css-classes' %}

Related

Change Celery task to run on-demand with clicking a button

I have a celery task that runs periodically and fetches some data for all companies that use a certain accounting service. I now need to make that task run on-demand with a click of a button, and fetch only the current company's data.
Modified celery task:
#APP.task
def import_data_for_company(company_id):
for integration in settings.INTEGRATIONS:
if integration["owner"]["company_id"] == company_id:
print("omlette du fromage")
Current button in Django template
{% for integration in settings.INTEGRATIONS %}
{% if company.company_id == integration.owner.company_id %}
<div class="button">
<a class="btn" href="#">Import Data</a>
</div>
{% endif %}
{% endfor %}
I need to trigger that task from the Django template and pass a company ID as an argument while doing it. But according to this comment, I cannot pass arguments from the Django template. Writing a custom template tag/filter seems not to help here.
What are my options for calling that function via this button?
Have to note, that I'm very green at this still.
You should create a view that takes your company id. Within this view, you will trigger import_data_for_company. Then you can redirect back to your initial view. Or you can trigger the view with AJAX as well.
Add the URL in your template accordingly: {% url 'some_name' company.company_id %}
urls.py:
path(
"your_desired_trigger_url/<id:company_id>/",
trigger_company_data,
name="some_name",
),
views.py:
def trigger_company_data(request, company_id):
import_data_for_company(company_id)
return HttpResponseRedirect(reverse("your_initial_view"))

Make navbar active on url and any url's following from it

I am trying to make my navbar element active if the user is on the current url and any url's leading from it.
For example, I want the navbar to be active on:
http://example.com/products
AND
http://example.com/products/discounted-items
I was using this:
{% if url_name == 'products' %}active{% endif %}
and was very happy till I realised that once I progress from the 'products' page to 'products/discounted-items' it would cease being active.
Is there a clean way to do this in django?
Thank you very much.
In your case you could simply do the following:
{% if 'products' in url_name %}active{% endif %}
Be aware that this also causes /productsfoobar to be active.
To prevent that you could use products/ instead of checking it without the trailing-slash.
In case (as i seen the page) if u are using Bootstrap u can add "active" in the class field in nav

Additional field shows up outside table in admin change_list view

I have a model called Project in an app called projects that I registered with the admin site so the instances can be added/edited/etc. This works as expected. Now I want to add a button for each project in the change list view on the admin site, that links to a custom form that requires a Project instance to do things. I followed a bunch of different tutorials to customize the admin site and managed to add another field to the table of the change list view. However the entries show up outside the table (see image).
I added the custom field by overwriting the admin/change_list.html template and calling a custom template tag custom_result_list within it. This tag adds a table field to the change list and then calls the admin/change_list_results.html template to render it. I have confirmed with a debugger that the item is added to the entries of the change list before the template is rendered (see image).
I cannot explain why the table is not rendered correctly even though the additional field has the same structure as the auto-generated ones. I have to admit I have resorted to Cargo Cult Programming, because I do not understand how this is supposed to work, despite spending too many hours trying to solve this simple problem.
Here's the relevant code.
In file /projects/templatetags/custom_admin_tags.py:
from django import template
from django.contrib.admin.templatetags.admin_list import result_list as admin_result_list
def custom_result_list(chl):
extended_cl = {}
extended_cl.update(admin_result_list(chl))
extended_cl["result_headers"].append({
'class_attrib': r' class="column-__str__"',
'sortable': False,
'text': 'Configure Project'
})
idx = 0
snippet = '<td class="action-button">{}</td>'
for project in chl.result_list:
extended_cl["results"][idx].append(snippet.format(project.id, project.unmod_name))
idx += 1
return extended_cl
register = template.Library()
register.inclusion_tag('admin/change_list_results.html')(custom_result_list)
In file templates/admin/projects/project/change_list.html:
{% extends "admin/change_list.html" %}
{% load i18n admin_urls static admin_list %}
{% load custom_admin_tags %}
{% block result_list %}
{% if action_form and actions_on_top and cl.show_admin_actions %}{% admin_actions %}{% endif %}
{% custom_result_list cl %}
{% if action_form and actions_on_bottom and cl.show_admin_actions %}{% admin_actions %}{% endif %}
{% endblock %}
To fix your issue:
from django.utils.html import format_html
replace your snippet.format(...) with format_html(snippet,...)
Explanation:
in django, all strings you pass from python are automatically HTML escaped. which here means, all your tags will not be considered as HTML. Such limitation is added to avoid any potential exploits by hackers. In your case, use of a template to render html is highly recommended. However, you can also send raw html from python using format_html helper function.

Django, display count of unread messages in base template

I am working on a project in which I have implemented messaging system like facebook where users can send messages to each other. The number of unread messages of a particular thread is also displayed on the messages page.
Now what I want is to display the count of unread messages in the navigation bar (which is there in base.html) each time user logs in. How to do this whenever a user logs in?
Please suggest, and I don't want to use any other app for this purpose. Thanks
You can write a simple tag which can do this for you.
def unread_messages(user):
return user.messages_set.filter(read=False).count()
#replace the messages_set with the appropriate related_name, and also the filter field. (I am assuming it to be "read")
register.simple_tag(unread_messages)
and in the base template:
{% load <yourtemplatetagname> %}
{% if request.user.is_authenticated %}
{{ request.user|unread_messages }}
{% endif %}

admin panel recent actions

I was wondering if I can get django admin (logged as super user) to display in some kind of "recent actions box", changes other users (non super users) made?
Thanx,
Luka
LogEntry.objects.log_action()
http://www.djangosnippets.org/snippets/1052/
Tying in to Django Admin's Model History
it was my problem too, and it has a simple answer
Just go this address and do the following;
Address: your virtulenv/lib/site-packages/django/contrib/admin/templates/admin
And find base.html, inside of this file edit after {% load Log %} and simply add this :
{% if user.is_superuser %}
{% get _admin_log (numbers of actions you want) as admin_log %}
And for the else do the same but at the end don't forget to add for_user user 😊