Editors Note - Please do NOT follow the advice in this posting. There are much simpler ways to achieve your aims when it comes to anything date related! Either ask on or post a question on this site.
How helpful is that?
It took me about 24 hours to figure out. I wasn't sure I got it right. Really, I thought, it must be easier.
But maybe not. Read all about it: Squishdot Dates & Sort Order
For the benefit of other Squish users, here's the abbreviated version.
NOTE: If you only want to change the date format and don't care about making the search returns sort in chronological order, just modify the date format itself in the DTML methods, using "fmstr" as has been documented elsewhere and is also discussed below.
I have close to the current version, Squishdot 1.4.0 (current is 1.4.1). I'm using a plain vanilla site: after one quick look at the "Blue Heaven" version of Squish and the code that creates it, I decided not to spend time modifying the embedded design elements. I prefer working up from simple code as I learn my way around. Also, I'm not a programmer -- though I'm not averse to dipping into the different levels of code.
I found references to various patches. One that returns the date formatted for different locales is mentioned on the site, but the link to the patch in the archives is broken. One that allows for variable date formats can be found in the , but I couldn't figure out how to use it, or whether it would help with the problem sorting search returns. There's also mention of a new date method added in v. 1.3.0 that supposedly allows you to adjust the date_posted format, but it wasn't explained and didn't seem to work for what I needed. Nothing that I found made much sense to me.
There may be smarter and simpler ways to do this, but here's what worked for me.
1. Change the date_posted string to a format that will sort on the numerical values of year.month.day.time instead of alphabetically by day of week.
The default format of date_posted is set in a massive python script, Posting.py, and gives you something like:
Saturday October 12, @09:21PM
Because the search works on formatted date strings, you can't adjust the sort order without going into this script and changing the formatting there -- unless you want them organized by day of the week. Not helpful! Presumably, this is stored in a ZCatalogue, and the actual date value must be in there someplace, too, but where this lives and how to get into it aren't obvious. I tried sorting on "date" instead, which is defined in Postings.py immediately after "date_posted," but couldn't make out any order whatsoever in the results it returned (!).
In order to sort by date, as far as I could figure out, I needed something like:
2002.10.12 :: 21:21 (Saturday October 12 : 09:21 PM)
This is kind of overkill, but gives the date/time in a format that's accurately sortable, first, followed by the same info in more user-friendly (human readable) form. To do this, I went into Posting.py and changed:
FROM:
security.declareProtected(View, 'date_posted')
def date_posted(self,fmstr='%A %B %d, @%I:%M%p'):
# """ date when article was posted """
ltime = localtime(self.created)
return strftime(fmstr,ltime)
TO:
security.declareProtected(View, 'date_posted')
def date_posted(self,fmstr='%Y.%m.%d :: %H:%M (%A %B %d : %I:%M %p)'):
# """ date when article was posted """
ltime = localtime(self.created)
return strftime(fmstr,ltime)
The bit you need to change is this: "fmstr='%A %B %d, @%I:%M%p'," where "%A" gives you the day of the week, "%B" gives you the month by name, "%d" gives you the date, "%I:%M" gives you the time on a 12 hr. clock, and "%p" either AM or PM.
In my modified version, "fmstr='%Y.%m.%d :: %H:%M (%A %B %d : %I:%M %p)'," "%Y" gives you the year, "%m" gives you the month by number, and "%d" again gives the date. "%H:%M" gives the time in a 24 hr. clock.
For a detailed list of these options, see the entry for "strftime" in Time access and conversions in the Python Library Reference.
2. Modify the search results loop in showSearchResults to sort postings by date posted, in reverse order, and insert the date in the results so users can see it.
Adding the sort to the search results is the easiest part:
FROM:
TO:
Next, insert the date in the returned results. I also removed itals from the formatting of the author name and put the date below the main search results for each item. Finally, I adjusted font styles for scannability, reducing the font size, changing the color to grey, and adjusting left-hand padding so it would align with the text above, to the right of the icon:
FROM:
by
TO:
by
3. Re-catalogue all the postings so the new date_format string replaces the old in the catalogue of postings.
Otherwise, you'll have the new format for postings added from now on but the old format for all previous postings.
Go to the "Options" tab on your Squishdot site and click on "Re-catalog All Postings." Note that it says: "This can be resource expensive." If you have a lot of postings, recataloguing them all will take a long time.
4. Adjust formatting of date/time on the postings preview page.
One small step and you're done.
The DTML method, previewPosting, reads the date/time straight off the ZopeTime clock and formats it for the preview, so you need to adjust the date formatting there if you want it to correspond to the new formatting. Because this only affects how the date/time is rendered on the preview page, I left out the portion needed for the search results sort and used only the more legible portion, with a minor adjustment:
2002 October 13 (Sunday) : 05:39 PM
FROM:
, @ by
TO:
( ) : by
Careful readers will notice that a lot of time has elapsed between when I started this process and now!
5. Optional: Streamline the date format everywhere else!
Finally, you might not -- after all this! -- want the very number-crunchy version of the date-time to appear anywhere but in the search results. To do this, AND if that's all care about, you can go into the DTML methods and modify the formatting of the date_format there by changing:
TO whatever you want, but this one is nice:
giving you:
2002 October 13 (Sunday) : 10:02 PM
You need to change it in index_html and posting_html (and, depending, might need to adjust the formatting in previewPosting to match).
Phew! I did catch some sleep in between.
I was hoping something useful might come out of the Squishdot customization contest at FreeZope, for example. They were supposed to post the winners on the 11th, but nothing's up yet. Gee, I wonder why? The results will undoubtedly be posted soon. . . . But given the state of the code and attitude of the developers, you'd have to be crazy to try to customi -- oh, what have I been doing with my life? =}
have fun,
::laura
|