I'm wondering if there a simpler way to achieve this, that cuts down on the number of queries submitted to the server:
I have a section on the home page where users can purchase tickets to an event. I don't have ticketed events that often, so I only want that section to display when tickets are available.
The way I've been doing it is like this:
{exp:channel:entries channel="ticketed_events" limit=1}
<h2>Purchase Tickets</h2>
{/exp:channel:entries}
{exp:channel:entries channel="ticketed_events"}
{event_title} - {ticket_price}
{/exp:channel:entries}
I have to do it twice so that the title only displays the one time, instead of once per entry. That seems a little awkward, having to send two queries to the server like that. Is that really the only way to do it?
ty
{exp:channel:entries channel="ticketed_events"}
{if count == 1}<h2>Purchase Tickets</h2>{/if}
{event_title} - {ticket_price}
{/exp:channel:entries}
Without trying that should do it, otherwise try {if {count} == 1}
Related
Can anyone please help me tidy up Google Spreadsheet data responses after a Google Form with a checkbox has been filled?
When someone attends training, a Google Form is filled which automatically populates a Google Spreadsheet with:
the training event
the date of the event and
all of the employees who attended that event.
There could be a number of employees attending one event so in the form, there is a checkbox giving the option to tick multiple employees. Below is an example of the Google 'Response Form' spreadsheet after the Form has been submitted:
The issue is that the Response Form is difficult to look at so I want to tidy it up and make it easy to use the Query function. Ideally, I would like it to look like this:
Can anyone help me?
you can use this formula:
=ARRAYFORMULA(QUERY(SPLIT(TRANSPOSE(SPLIT(TRIM(QUERY(TRANSPOSE(QUERY(TRANSPOSE(
IF(IFERROR(SPLIT(D:D, ";"))<>"", "♦"&B:B&"♥"&C:C&"♥"&
REGEXREPLACE(SPLIT(D:D, ";"), "^, ", ), )),,999^99)),,999^99)), "♦")), "♥"),
"order by Col2", 1))
I have documents, that look like
{
...
date: '2013-05-25T04:06:20.277Z',
user: 'user1'
...
}
I would need to find documents that have a date within a given range, and a given user. I also need to sort by date.
I have tried emit([doc.user, dateToArray(doc.date)], null) but with this, I cannot sort by date, because AFAIK the key that needs to be sorted on has to be on the left side. Is this correct?
If I try to flip the keys the other way around, no matter what user I put in the startKey & endKey it does not change anything.
for example: startKey: [[0,11,21,0,0,0],"user1"], endKey: [[2016,11,21,0,0,0],"user1"] finds documents from all users, even though I would suppose it to find only documents, where the second key is user1.
How should I do this? My document count can go up to millions, so doing stuff on code-side is out of question..
Atleast for now, I ended up having 2 seperate views (byDate and byUserAndDate)
When I need to find only by date I use the byDate view which has the date as the only key, sorting works fine. Also, when I search by particular user I use the byUserAndDate which has [doc.user, doc.date] as its compound key, when the result contains only items for 1 user, the sort obviously works fine because it will sort by user first, and then by date.
In a Django app, I have a queryset for a data model called Comment. This contains text comments left by users.
Imagine 'n' users commented. What's the fastest way to calculate what % of comments were left by which user?
Right now I'm thinking it's going to be:
Comment.objects.filter(commenter=<username>).count()/Comment.objects.count()
What do you suggest? My objective is to flag people who're commenting too much, in order to screen their accounts for possible spamming. I'd be running this query voluminously, hence the focus on performance.
You should avoid making one query for each user in your database. Instead you can just query the number of comments for each user (or even the top n commenters) with something like:
from django.db.models import Count
total_comments = Comment.objects.count()
# Fetch top 10 commenters, annotated with number of comments, ordered by highest first
User.objects.annotate(num_comments=Count('comment')).order_by('-num_comments')[:10]
for user in users:
percentage = user.num_comments / total_comments
This example assumes you have a User model that your Comment has a foreign key to.
The percentage of total comments doesn't actually matter if you are comparing relative numbers of comments.
I am starting a project using Cloudant.
It's a simple system for logging, so I can track the usage of my apps.
My documents looks like this:
{
app:'name of the app',
type:'page view | login | etc..',
owner:'email_of_the_user',
device: 'iphone | android | etc..', date:
'yyyy-mm-dd'
}
I've tried to do some map reducing and faceted searches, but couldn't find so far the result for what I want.
I want to count the number of distinct documents grouped by same owner, date (yyyy-mm-dd), and app.
[For example, if a the same guy logs in the app twice or 20 times in the same date, it will be counted only once.
I want to count how many single users used an app each day, no matter what's the type of the log, or the device he used.]
If it was SQL, assuming that each key of the document is a column, I would query something like this:
SELECT app, date, count(*) FROM LOGS group by date, owner, app
ant the result would be something like:
'App1', '2015-06-01', 200
'App1', '2015-06-02', 232
'App2', '2015-06-01', 142
'App2', '2015-06-02', 120
How can I get the same result using Cloudant/CouchDB?
You can do this using design documents, as Cesar mentioned. A concrete example would be to create a view where your map function emits the field on where you want to group on, such as:
function(doc) {
emit(doc.email, 1);
}
Then, you select your desired reduce function (such as _count). When viewing this on Cloudant dashboard, make sure you select Reduce as part of the query options. When accessing the view via URL you need to pass the appropriate parameters (reduce=true&group=true).
The documentation on Views here is pretty thorough: https://docs.cloudant.com/creating_views.html
For what you need there is a feature on couldant/couchdb called design document. You can check their documentation for this feature for details or this guide:
http://guide.couchdb.org/draft/design.html
Cloudant documentation:
https://docs.cloudant.com/design_documents.html
Design documents are similar views on the SQL world.
Regards,
We were able to do this in our project using the Cloudant Java API...
https://github.com/cloudant/java-cloudant
You should be able to get this sort of result by creating a view that has a map function like this...
function(doc) {
emit([doc.app, doc.date, doc.owner], 1);
}
The reduce function should look like this:
function(keys, values, rereduce){
if (rereduce){
return sum(values);
} else {
return sum(values);
}
}
Then we used the following query to get the data we wanted.
Database db = ....
db.view(viewName).startKey(startKeys).endKey(endKeys)
.group(true).includeDocs(false).query(castClass)
We supplied the view name and some start and end keys (since we emitted a compound key and we needed to supply a filter) and then used the group method to get the data back as you need it.
Revised..
With this new emit key in the map function you should get results like this:
{[
{[app1, 2015,06,28, john#somewhere.net], 12}, <- john visited 12 times on that day...
{[app1, 2015,06,29, john#somewhere.net], 10},
{[app1, 2015,06,28, ann#somewhere.net], 1}
]}
If you use good start and end keys, the amount of records you're querying will stay small and the number of records you get back is the unique visitors you are seeking. Note that in this scenario you are getting back a bit more than you want, but it does work.
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).