How to validate number of renderings on item - sitecore

How do i perform validation on number of rendering applied on any item.
i want to restrict rendering with placeholder say 'xyz' to 3 i.e that rendering with placeholder 'xyz' should not exceed 3 times on item.
e.g. if i apply rendering 'rend_card' more than 3 times on item. then it should show me validation error.

If you are have MVC solution check this link : https://ctor.io/limit-the-number-of-components-in-a-sitecore-placeholder/
If you are using Web Forms check : http://www.newguid.net/sitecore/2014/restricting-the-number-of-components-in-the-sitecore-page-editor/

Related

Django: display a preview of an object's attribute - Class based views

Using a ListView Class-based-view, I am looping over the objects present in the database of a certain model in my HTML template, and, for instance, I can access an object's "body_text" attribute with the following syntax: {{object.body_text}}
What if I wanted to only show the first 20 characters of that "body_text" attribute in my HTML template?
How can I set that?
1st Method
Use the truncatechars filter in your HTML template.Truncates a string if it is longer than the specified number of characters. Truncated strings will end with a translatable ellipsis character (“…”).
{{object.body_text|truncatechars:20}}
Reference:
https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#truncatechars
2nd Method
Use the slice filter in your HTML template.
{{object.body_text|slice:":20"}}
Referernce: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#slice
Found it, eventually.
Use the |slice:":" filter in your HTML template.
For example, if you only want to display the first 10 characters of a given attribute, use:
{{object.body_text|slice:":10"}}

Return to scroll position when using django endless pagination

1) Initial load of list page shows 50 items.
2) By "clicking show more" using django endless pagination, there are 100 items on the page now.
3) follow link to 90th item in list and go to another page
4) click back button on the browser
The problem is that after step 4, the list contains only the first 50 items.The last 50 items is not displayed.
Is it possible to return to position of 90th item on list page by hitting the back button
I think it's not special to Django. We can use url hash to return to specified place. So,
1. in html, code snippet <div id="test">...</div>
2. url like http://xxxx/#test will be located to div with id 'test'
But, this way cannot remember the exact position.
For example, cannot remember position between two div each with an id.

Sitecore Search Lucene Ranking

How can I set the priority of fields when I'm searching with Lucene .Net
For example i have :
Item 1
field 1 =contain "search text"
field 2 ="bla bla"
Item 2
field 1 = "loren ipsun"
field 2 = contain "search text"
What should I do to have on results, top item : item 1 first from list, item 2 second one,etc.
I think I understand your question. You want to have hits on a specific field boosted more then on another field and for this you can use the BoostFactor. You can boost the Query that you are using to search in Field one. For example if you use a Query qryName = queryParser.parse(keyword) you can do the following:
qryName.SetBoost(1000f);
This will boost hits on the specific part of your search that your specified in that query. You should therefor use 2 Query's for both fields and boost the first Query in which you search field 1.
You can vary in the boostfactor untill is suits your needs.
Hope this helps ;)

Django Form Models and Editing

Wow, I'm having such a hard time on this. I must be doing something wrong, I'm getting an incredible ammount of queries.
So, my models are the following:
Player
Clan
Match (as in game match)
MatchMap (the maps played of a match)
MatchPlayer (the players of a match)
All of them are related via foreign key, no m2m relationship. A player can be in a clan and a match involves 2 clans. A match can have any ammount of maps and only players from the two clans involved can be in the match (although on the future the players might not be in the same clan they played on that match, so I specify the side they played on the match).
So, I made the submit match, all ok. But to edit this info, it's caos!
For editing MatchPlayers of a match I tried using inlineformset_factory
PlayersFormSet = inlineformset_factory(MatchBetweenClans, MatchPlayer)
playersForms = PlayersFormSet(instance=match)
This already starts bad because for each instance of player on a match, Django hits the database and gets a list of all players. If for example 6 players are on a match, and I have 2 empty forms provived by the inlineformset_factory, I see on the log
SELECT
...
FROM
`accounts_customuser`
8 times.
Now, even if that worked correctly for me, it does not do what I need. When adding players to a match, not all players should be on the list, only those from the 2 specified clans, ideally as the form of checkboxes (each checkbox being a player of a clan). When submitting the match this is easy to do:
clan1PlayerList = CustomUser.objects.filter(clan=clan1Instance)
clan2PlayerList = CustomUser.objects.filter(clan=clan2Instance)
playersClan1 = forms.ModelMultipleChoiceField(queryset=clan1PlayerList, label="Jogadores - "+clan1Instance.tag+"", widget=forms.CheckboxSelectMultiple(attrs={'class':'input_checkbox'}))
playersClan2 = forms.ModelMultipleChoiceField(queryset=clan2PlayerList, label="Jogadores - "+clan2Instance.tag, widget=forms.CheckboxSelectMultiple(attrs={'class':'input_checkbox'}))
Is there anyway I could have this on a formulary to be edited? I can't find a way to send the playerlist of a clan and the currect players of a match to a form.
Thanks
You might want to look at select_related.
Here are the docs.
I had to go read your question again, as the question part of it wasn't clear to me. I thought you had a problem of too many queries hitting the database (which may be a problem too and select related will help there), but your question really is:
How to filter a form field to only allow values based on a different field?
Correct? From your question:
When adding players to a match, not all players should be on the list, only those from the 2 specified clans, ideally as the form of checkboxes (each checkbox being a player of a clan).
In your view, filter the fields on each form by clan. Something like (without your models I can only guess at field names):
form.fields['player'].queryset=form.fields['player'].queryset.filter(clan__in=list_of_selected_clans)
Unrelated suggestion: look at python's string interpolation.
Boa sorte!

Using breadcrumbs with django-filter, QueryDict trouble

I'm using django-filter to drill down and would like to create breadcrumbs for each item that was filtered. For example:
Price ranges:
10,000+
5,000-9,999
1,000-4,999
0-999
Bedrooms:
4
3
2
1
Each of the items under Price ranges and Bedrooms would be a link to drill down in a queryset.
I'd like to create a breadcrumb such as Price range 0-999 or Bedrooms 3 if the user were to click those links, and then show Price range 0-999 > Bedrooms 3 or Bedrooms 3 > Price range 0-999 when they click a second link.
The breadcrumbs should maintain order (the part I'm having trouble with) and work for any number of attributes. Clicking a link in the breadcrumb trail should apply the filter clicked on and all filters before it in the trail.
I'd like to create an empty QueryDict and then iterate through request.GET to build the QueryDict up as I output the breadcrumbs, but for some reason QueryDict iterates through its elements backwards (see the documentation).
What's the cleanest way to accomplish this? Does anyone know why QueryDict works this way? (I imagine there's a use-case I'm missing.) Any advice is appreciated.
keep track of the order in sessions. so when the first filter is clicked (eg 3 beds) store it. then if another one is clicked, build your bread crumbs from sessions. say the second one was 0-999 you'd pull any existing breadcrumbs (in this example you'd find the 3 beds) and then tack on the latest (0-999).