Django time reformat - python

I have a pair of time field inputs in my Django forms. I wanted to create the form in a way that allows the user to enter the time in whichever format they would like and the program will reformat to the correct type.
Not sure what is the best way to go.
Using Django validation but the user does not get feedback about input.
Using html5 time input sounds promising but it has poor support for IE, which users are expected to use.
Javascript/jquery but the user can change the Javascript code to bypass client-side validation.
Any suggestions?

You should check arrow.
Arrow is a Python library that offers a sensible, human-friendly approach to creating, manipulating, formatting and converting dates, times, and timestamps

Related

How to put timestamp when a field is added in document of CouchDB?

I want to record the time when a new field is added to the document in CouchDB using Python.
I have to put a time stamp on every new field and show this time with the respective id of the field on an Html page.
Thanks in advance.
If clients send directly to the couchdb then your best bet is to modify them to use an update function and have the update function write timestamps for each normal field. You could also use a validate function to prevent changing them and to verify that they exist and are roughly correct.
Since validate and update designs are independent, I don't think you can prevent a clever user from still writing misleading information by bypassing the update function. The design document functions also probably need to be written in JavaScript as the default and most complete Query Server is the JavaScript one.

Perform some calculations using django form. Which framework?

I have an input form with couple of input fields mostly numerical. I have a python script which takes the input values then performs some medium calculations (arrays, loops, etc) and gives results.
I would like to display results without a page refresh below the input form. Simplest example would be to display an average from the three input fields below when user clicks calculate button or instantly without even clicking.
Could anybody point me to the right direction how this would be implemented? I don't know what's the best framework to use for this. Just Ajax or Angular? Perform calls on client side or server?

Which datatype in Django model to be used to save HTML5 type "month" in database

I have a HTML form with field which takes input as month
<input type='month' name="last_appraisal" id='txtLastAppraisal' value='2013-12' />
In my Django model this field is defined as
last_appraisal = models.DateField(blank=True, null=True)
When I try to save this value (received from request.POST) in database then I get format error.
last_appraisal = request.POST.get('last_appraisal','')
if last_appraisal != '':
mymodel.last_appraisal = last_appraisal
mymodel.save()
ERROR: raise exceptions.ValidationError(msg)
ValidationError: [u"'2013-01' value has an invalid date format. It must be in YYYY-MM-DD format."]
I know I can achieve this by adding a default day(maybe 01) to this input month and then save into the database. And while fetching back this value I can re-format it like "YYYY-MM" and send it back to template.
But I want to know if there's any better way to achieve this. I am using Django 1.5.1 and Python 2.7.
EDITED:
After reading all the responses it is clear that Django doesn't have built-in support for this format. Now I have few choices
My way to add day into date field.
As suggested by #Odif to take database field as 'CharField'
As suggested by #Ghopper21 to create a Custom Django field
My preference will be the choice1 because using this
I don't have to write some extra complex code compare to choice3.
I get the flexibility to use this field in table searches like 'find all candidates who got their appraisal in Jun 2013' which I loose if I go for chioce2.
Don't know whether my choice is good or bad but considering the size of project and time to complete it I think this will be the best choice. Please comment if you think this is absolutely rubbish or if you are in favor of this approach.
There are two issues here: Django doesn't have a built-in way to deal with month/year-only dates, and nor does Python.
If you are ok with a convenient way to translate the month/year-only dates into full dates with the day set to 1, but just don't want to have to do this manually in your view code every time, you can encapsulate that logic in a custom Django widget, which is a component for translating between the Django field representation (in this case a DateField) and the HTML input field.
If you need to have the underlying Python representation of the date to be month/year-only in a first class way, I'd suggest looking into the excellent Python library dateutil's relativedelta class -- which allows you to specify things like relativedelta(year=2013, month=12). If you want to use that, you'd then have to create a custom Django model field that translates between relativedelta instances and database storage via serialization. You'd then still need a custom widget for the HTML side of things.
Use models.CharField. DateField and DateTimeField expect date and datetime object respectively. Since '2013-01' cannot be coerced into either, then use CharField. Or use integerfield and just post month.

Quickly implement a sortable table of objects in Django

Hey all. I have a question on how to implement the following with Django. I'd like to display a tabular view of my objects with each column corresponding to a particular model field. I'd like to be able to have the user sort the columns or search through all of them. Basically just like the admin, but client facing and read-only. It's simple, but I was wondering if there was a way I could implement this without having to write all that logic from scratch.
Alex Gaynor's django-filter may be what you want.
Depending on how much you wanted to work with it, Yahoo YUI's DataTable control is pretty easy to get working with a JSON data source. See http://developer.yahoo.com/yui/datatable/

Django Form Validation Framework on AppEngine: How to strip out HTML etc.?

I'm using the Django Form Validation Framework on AppEngine (http://code.google.com/appengine/articles/djangoforms.html), like this:
data = MyForm(data=self.request.POST)
if data.is_valid():
entity = data.save(commit=False)
entity.put()
I wonder if there's a way to preprocess the POST data (strip out malicious code, HTML etc.) before storing it. It seems that any form validation library should offer something like that, no?
Thanks
Hannes
Short answer:
forms.is_valid() auto populates a dictionary forms.cleaned_data by calling a method called clean(). If you want to do any custom validation define your own 'clean_filed_name' that returns the cleaned field value or raises forms.ValidationError(). On error, the corresponding error on the field is auto populated.
Long answer:
Refer Documentation
In addition to the answers above, a different perspective: Don't. Store the user input with as little processing as is practical, and sanitize the data on output. Django templates provide filters for this - 'escape' is one that escapes all HTML tags.
The advantage of this approach over sanitizing the data at input time is twofold: You can change how you sanitize data at any time without having to 'grandfather in' all your old data, and when a user wants to edit something, you can show them the original data they entered, rather than the 'cleaned up' version. Cleaning up data at the wrong time is also a major cause of things like double-escaping.
Yes, of course. Have you tried reading the forms documentation?
http://docs.djangoproject.com/en/dev/ref/forms/validation/

Categories