Default specific option checked in ParentalManyToManyField wagtail, using django-form - django-forms

I am working with a multi-site wagtail project. And I want an event class which contains ParentalManyToManyField field called sites. The idea is that each event will be associated with multiple sites not necessarily all.
Two sites X and Y have lots of events and most of them are common. So I want to make default checkbox selected for two of the sites while resting unchecked in event content_panels.
My event class is as follows.
from django import forms
from wagtail.core.models import Orderable, Page, Site
from modelcluster.fields import ParentalKey, ParentalManyToManyField
class Event(Page):
....
sites = ParentalManyToManyField(Site)
...
Event.content_panels = [
...
FieldPanel('sites', widget=forms.CheckboxSelectMultiple),
...
]
I found following code somewhere and tried it but it makes all options checked.
Event.content_panels = [
...
FieldPanel('sites', widget=forms.CheckboxSelectMultiple(attrs={"checked":""}))
I want just two specific options to be checked and rest unchecked by default.

Related

Django Rest Framework: Disable format extensions

I would like to do something like this:
from rest_framework import routers
router = routers.DefaultRouter(suffix_required=False)
When I look at the definition of DefaultRouter:
class DefaultRouter(SimpleRouter):
"""
The default router extends the SimpleRouter, but also adds in a default
API root view, and adds format suffix patterns to the URLs.
"""
include_root_view = True
include_format_suffixes = True
Notice include_format_suffixes is hardcoded to True.
I must be missing something, but how to I turn off format extensions preferabley for the whole project, or at least for a certain router? I always use JSON, and do not wish to have these .json extensions in the urls created from ViewSets.
I realize I could use Simple router, but I would the " default API root view, that returns a response containing hyperlinks to all the list views" that the DefaultRouter provides.

When is module rendered in drupal?

I am a newcomer in Drupal, and I want to make a custom module in Drupal8.
I have developed the module, but there are some problems to solve.
The module displays GET parameter value when a page containing the module is shown to users.
For example, I connect with http://localhost/drupal/?keyword=banana and the module displays "banana".
But after the above, when I connect with http://localhost/drupal/?keyword=apple again, then the module displays "banana" too.
In other words, the module works well when the page containing the module is shown firstly and works wrong when I connect secondly, thirdly and so on.
I have tested some and build() method in the module is called only once.
So, I think that the module is rendered only once when I connect to the page and can't be rendered after the first.
Also, I think that it can be the problem related to cache, but I set admin/configuration/performance/cache to "no-cache".
I am not sure that it is possible to display "apple" after "banana" is displayed by the module.
Please help me and let me know more details...
Thanks.
There are a couple of possible solutions depending on your constraints: disable the cache for this particular page, or use routing wildcards.
You can disable the cache on a particular page by using the page cache kill switch service, which you trigger like this in your controller:
\Drupal::service('page_cache_kill_switch')->trigger();
This will disable the cache just for this particular request, so you won't get the effect of seeing stale content.
A better solution, if possible, is to use routing parameters instead of your GET parameters. This will allow your separate URLs (for example page/banana, page/apple etc.) to be cached and still show the contents you'd like them to. For example, in your module.routing.yml file:
mymodule.route:
path: '/path/{parameter}'
defaults:
_controller: '\Drupal\mymodule\Controller\MyModuleController::page'
_title: 'My Module Page'
requirements:
_permission: 'access content'
The {parameter} parameter can then be accessed in your controller like so:
public function page($parameter) {
return ['#markup' => $parameter];
}
More information: https://www.drupal.org/node/2186285

Django autocomplete Light in list filters for admin

I have successfully setup the Autocomplete Registry and have my django admin forms where if you go to the form, the auto completes works. I would like to be able to extend the autocompletes to work on the list_filter view as well. So when you are looking at the view generated by Admin.py -- that the list_filter inputs that are generated would also use the autocomplete jquery + service URL.
I didn't see anything listed in the documentation, anyone have any pointers?
If you are using Django version greater then 2.0, you can try using the built-in autocomplete fields for this purpose.
By default, the admin uses a select-box interface () for those fields. Sometimes you don’t want to incur the overhead of selecting all the related instances to display in the dropdown.
The Select2 input looks similar to the default input but comes with a search feature that loads the options asynchronously
There is a simple app which does this:
To install use: pip install django-admin-autocomplete-filter
Then add admin_auto_filters to your INSTALLED_APPS inside settings.py of your project.
Let's say we have following models:
class Artist(models.Model):
name = models.CharField(max_length=128)
class Album(models.Model):
name = models.CharField(max_length=64)
artist = models.ForeignKey(Artist, on_delete=models.CASCADE)
cover = models.CharField(max_length=256, null=True, default=None)
And you would like to filter results in Album Admin on the basis of artist, then you can define search fields in Artist and then define filter as:
from admin_auto_filters.filters import AutocompleteFilter
class ArtistFilter(AutocompleteFilter):
title = 'Artist' # display title
field_name = 'artist' # name of the foreign key field
class ArtistAdmin(admin.ModelAdmin):
search_fields = ['name'] # this is required for django's autocomplete functionality
...
class AlbumAdmin(admin.ModelAdmin):
list_filter = [ArtistFilter]
'''
defining this class is required for AutocompleteFilter
it's a bug and I am working on it.
'''
class Media:
pass
After following these steps you may see the filter as:
You should define your own admin filter that inherits from django.contrib.admin.SimpleListFilter. Then should provide your own HTML template for this filter which will use one of django-autocomplete-light widgets. As a parameter for widget you should define required autocomplete URL. And do not forget to include proper JS and CSS for it.
All of this is done in special app for this: dal-admin-filters

Joomla progressive cache doesn't handle modules with variable output

I have a module which allows the user to choose a category, which is then used for filtering the component's output. So when the user first clicks on the menu item, the view shows items from all categories, then as they click on the module, a param such as &catid=69 etc. is added to the url and used to filter the items displayed.
A system plugin complements the behaviour by registering the extra catid param i.e.
$registeredurlparams->catid = 'INT';
$app->set('registeredurlparams', $registeredurlparams);
The module uses the category id to create the cache id, and shows the top-level categories + the subcategories of the category that was chosen.
This works fine with both conservative cache enabled in the system configuration and the System Cache plugin enabled.
My concern is that I cannot get it to work with progressive cache: even though the component output is cached correctly, the module doesn't get updated (so I never see the subcategories).
Eventually I plan to make the extension available on the JED, and I'd like to be compatible with all possible cache configurations. Is there any possibility to force the progressive cache to add the parameters I want to the cache id?
Workarounds such as sending the full category tree and doing it with ajax will not be accepted.
One thing you could look at is ContentModelArticle in the back end. You will notice that cleanCache()
forcibly clears the content modules that could be impacted by a save or create.
protected function cleanCache($group = null, $client_id = 0)
{
parent::cleanCache('com_content');
parent::cleanCache('mod_articles_archive');
parent::cleanCache('mod_articles_categories');
parent::cleanCache('mod_articles_category');
parent::cleanCache('mod_articles_latest');
parent::cleanCache('mod_articles_news');
parent::cleanCache('mod_articles_popular');
}
I've always thought this was a sledge hammer/kludge since it doesn't let the webmaster control whether or not to do this, but you could do something along the lines of making a custom cleanCache() for your model.

Extending Joomla 2.5 Banner Component

I really hope someone can help me.
I need to be able to serve banners in categories which are dependant on a session variable - and can't find a component which does that. So I'd like to extend the Joomla Banner component in order to select banners based on a session variable which contains the category path.
The correct session variable is being stored correctly.
In order to do this I added an option in the banners module .xml to allow for a session variable and the name of the session variable. This is being stored correctly in the module table within the params field along with the other module parameters.
Then I started on the
components > banners > com_banners > models > banners.php
by adding two lines of code in getListQuery where the SQL is assembled. They are:
$sess_vars = $this->getState('filter.sess_vars');
$sess_vars_name = $this->getState('filter.sess_vars_name');
But both variables contain nothing even though the ones the component already has can be retrieved fine. Without a doubt I need to change something somewhere else as well - but just can't figure out what to do.
Any help would be greatly appreciated.
The first thing to do is not hack the core files, hacking the core prevents you from using the built-in update feature to apply the regular bug fixes and security patches released by Joomla! (e.g. the recently released 2.5.9 version).
Rather make a copy of them and modify it so it's called something else like com_mybanners. Apart from the folder name and the entry point file (i.e. banners.php becomes mybanners.php) you will also want to update the components banners.xml to mybanners.php.(You will need to duplicate and modify both the front end /components/com_banners/ and /administrator/components/mybanners.php.)
Because of the way Banners work (i.e. banners are displayed in a module) you will also need to duplicate and modify /modules/mod_banners/,/modules/mod_banners/mod_banners.php and /modules/mod_banners/mod_banners.xml. Changing mod_banners to mod_mybanners in each location.
In Joomla! components the state is usually populated when JModel is instantiated, however, in this case the component is really about managing banners and recording clicks the display is handled by mod_banners. So, you will want to add some code to mod_mybanners.php to use the session variables you want to act on. Normally when a models state is queried you will collect the variables via JInput and add them to your object's state e.g.
protected function populateState()
{
$jApp = JFactory::getApplication('site');
// Load state from the request.
$pk = $jApp->input->get('id',0,'INT');
$this->setState('myItem.id', $pk);
$offset = $jApp->input->get('limitstart',0,'INT');
$this->setState('list.offset', $offset);
// Load the parameters.
$params = $app->getParams();
$this->setState('params', $params);
// Get the user permissions
$user = JFactory::getUser();
if ((!$user->authorise('core.edit.state', 'com_mycomponent')) && (!$user->authorise('core.edit', 'com_mycomponent')))
{
$this->setState('filter.published', 1);
$this->setState('filter.archived', 2);
}
}
The populateState() method is called when a state is read by the getState method.
This means you will have to change your copy of /components/com_banners/models/banner.php to capture your variables into the objects state similar to my example above.
From there it's all your own code.
You can find all of this information in the Developing a Model-View-Controller tutorial on the Joomla Doc's site

Resources