Web2py orderby, limitby - mongodb

My database layer (MongoDB):
db.define_table('news',
Field('title', label='Title'),
Field('link', label='Link'),
Field('date', label='Date'),
Field('summary',label='Summary'))
My controller:
def news():
if len(request.args): page=int(request.args[0])
else: page=0
items_per_page=5
limitby=(page*items_per_page,(page+1)*items_per_page+1)
orderby="~date"
qset = db(db['news'])
grid = qset.select(orderby=orderby, limitby=limitby)
return dict(grid=grid,page=page,items_per_page=items_per_page)
My view:
<table class="news-stories">
<th>Date</th>
<th>Title</th>
<th>Summary</th>
{{for i, row in enumerate(grid):}}
{{if i==items_per_page:break}}
<tr>
<td>{{=row.date.strftime("%m/%d/%Y")}}</td>
<td>{{=row.title}}</td>
<td>{{=row.summary}}</td>
</tr>
{{pass}}
</table>
{{if page:}}
previous
{{pass}}
{{if len(grid)>items_per_page:}}
next
{{pass}}
When I set orderby="~date" or orderby="date desc", as in the example above, my dates are not ordered appropriately in desc order; some dates in November come after dates in December (and vice-versa). However, when I set orderby="date", the dates are ordered perfectly in asc order. Am I missing something here?
Dates are inserted as datetime.datetime.strptime(date,"%m/%d/%Y").

It should be:
orderby = ~db.news.date
See details in the documentation.

Related

How can I extract an attribute from multiple elements in Playwright?

I'm trying to extract some attributes from multiple elements using playwright.
( this is F# but you should be able to figure out what is wrong )
let! pagelinkRows = page.EvalOnSelectorAllAsync("tr[title]","(tr) => tr.getAttribute("title")") |> Async.AwaitTask
I get the error: tr.getAttribute is not a function. I have also tried tr.attr .
What is the simplest way to get the attributes from a collection of elements
<tr title="xxx1"></tr>
<tr title="xxx2"></tr>
<tr title="xxx3"></tr>
<tr title="xxx4"></tr>

Display holidays on antd's datepicker

Can someone tell me how to display holidays using antd-datepicker by getting holiday information from a server.
Before I call the dateRender callback, I want to get the startdate and the enddate of the holiday information.
Try this picking from the example:
<DatePicker
dateRender={(current) => {
const style = {};
if (InHolidayDates(current.date())) {
style.border = '1px solid #1890ff';
style.borderRadius = '50%';
}
return (
<div className="ant-calendar-date" style={style}>
{current.date()}
</div>
);
}}/>
Write a InHolidayDates(date) function which checks if the "date" is in the Holidays Date list. If it exists it returns true. Else false.
Let me know if you need help with the fetching and comparison of dates.

TYPO3 groupedFor viewhelper according to dates

I have a simple model "item" with 3 properties:
title, description, date
Now I want to have a list like this:
10.10.17:
title item 1
title item 5
12.10.17:
title item 8
and so on
According to groupedFor - grouping objects according to dates
I added a getter like
/**
* Get day month year of datetime
*
* #return int
*/
public function getDayMonthYearOfDatetime()
{
return (int)$this->datum->format('dmy');
}
My list view:
<f:groupedFor each="{items}" as="dayMonthYearOfDatetime" groupBy="dayMonthYearOfDatetime" groupKey="datum">
<tr>
<th>{datum}</th>
</tr>
<f:for each="{dayMonthYearOfDatetime}" as="calendar">
<tr>
<td>{item.title}</td>
</tr>
</f:for>
</f:groupedFor>
Now I get the following output:
101017
title item 1
title item 5
121017
title item 8
How to diplay the correct date "10.10.17" instead "101017"?
If I change my getter to:
return (int)$this->datum->format('d.m.y');
It doesn't work.
return (int)$this->datum->format('d.m.y');
Remove the (int)
You can't expect to have a grouping if you return int from a date-string with dots.
Either you need to return string (but I don't know whether the function is allowed to return anything else than int) or you need to calculate your datestring from the calculated integer.
Try a viewhelper which does integer caclulations ( % 100, / 100 %100, /10000) or simple substrings (,0,2 ,2,2 ,4,2) to extract the values for day, month, year for a following concatenation.
You can simply use the real date object of the first group-item.
So it would be
<f:format.date format="d.m.Y">{dayMonthYearOfDatetime.0.datum}</f:format>

Loop over the Mongodb objects to get the values in Meteorjs

I am first Time working With Meteorjs and MongoDB. I am working on a Q&A application. where I have a list of objects which i got using Meteor database call. I want to looper over the list and want to split one of its attribute to the length 50 and then want to send a list of these splitted objects to the database.what I have is this
Template.questions.questions = function () {
questions= Meteor.questions.find({topic_id: Session.get("currentTopicId")});
return questions
}
Every question object in the questions list has an attribute question_text. Using loop i want to split this attribute to length of fifty and then want to push that to an empty list and return that list . Like
questions_list=[]
for(i=0;i<questions.length;i++){
question=questions[i].question_text[0:50] // python syntex to get first 50 char of a string
questions_list.push(question
}
return questions_list
and My HTML is like
<tbody>
{{#each questions}}
<tr>
<td class="span2" style="overflow:hidden;width:50px">
{{question_text}}
</td>
</tr>
{{/each}}
</tbody>
suggest me guys how can i acieve this in meteorjs. my problem is when i try to loop over this questions list there are many attributes like Collection, Results, Queries. here i am unable to iterate this list of objects.
In the same way how can i get the error message thrown back by meteorjs
This will give you list of shortened texts of all questions matching a query:
var questions = Questions.find({...}).map(function(question) {
return question.text.substr(0,50);
});
You could use it directly in a helper:
Template.questions.questions = function () {
return Questions.find({...}).map(function(question) {
return question.text.substr(0,50);
});
};
By the way, Questions.find({...}) does not return a list of questions, but a cursor object that you can use to manipulate query data in an effective way (like that map method above). To get raw array, you need to use fetch on that cursor: Questions.find({...}).fetch().
If you're just displaying the data you could create a handlebars helper:
Template.questions.helpers({
first50: function(question) {
return question.text.substr(0,50);
}
});
Then add the helper:
Template.questions.questions = function() {
return Questions.find({...});
};
Then in your template you could do:
<tbody>
{{#each questions}}
<tr>
<td class="span2" style="overflow:hidden;width:50px">
{{first50 question_text}}
</td>
</tr>
{{/each}}
</tbody>

Entity framework in MVC 4 - Include returning false result

I have two tables:
Products:
ProductID, ProductName
Compounds:
CompoundID, ComponentID, Qty
bound as next:
1 Products.ProductID --> many Compounds.CompoundID
1 Products.ProductID --> many Compounds.ComponentID
A Products item can be a compound, a components, or both.
I need that a view returns for each compound its components names and Quatities.
MyController method :
public ActionResult CompoundProduct()
{
var Compounds = db.Compounds.Include(s => s.Products);
return View(Compounds.ToList());
}
MyView code :
#model IEnumerable
<table>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.CompoundID)
</td>
<td>
#Html.DisplayFor(modelItem => item.ComponentID)
</td>
<td>
#Html.DisplayFor(modelItem => item.Products.ProductName)
</td>
</tr>
}
</table>
With this method I get a list of repeated Compound name instead of distinct Component names per Compound as shown in the image below:
any help please ?
When you created the model from the database EF should have created two navigation properties Products and Products1 related to the two foreign keys CompoundID and ComponentID. You are probably using the wrong navigation property that belongs to CompoundID, not the one that belongs to ComponentID. Instead of Products try to use Products1 in the Include...
var Compounds = db.Compounds.Include(s => s.Products1);
...and in the view:
#Html.DisplayFor(modelItem => item.Products1.ProductName)