Re: migrate does not store default values in the database

2014-10-24 Thread Jon Dufresne
On Fri, Oct 24, 2014 at 3:24 PM, Peter Coles  wrote:
> I'm trying out the migrations in Django 1.7 and I was surprised when my
> migrations did not set default values in the database. Can someone share the
> motivation for this functionality? (more details below)
>
> This was surprising given the documentation I read:
>
> the release notes for 1.7 suggest using `null=True` for new database
> columns, but only because of the potential performance impact during a site
> update (which seems odd, since the migrations don't set default values
> anyways…)
> the example of a migration file clearly shows a default value of `0` for the
> Author.rating field, even though it turns out the default isn't used
>
> Running `python manage.py sqlmigrate appname migrationnumber` for an example
> like the Author.rating addition in the docs would look something like this:
>
> BEGIN;
> ALTER TABLE "author" ADD COLUMN "rating" integer NOT NULL DEFAULT 0;
> ALTER TABLE "author" ALTER COLUMN "rating" DROP DEFAULT;
>
> COMMIT;
>
> However, looking in the source code, I can see: "(Django usually does not
> use in-database defaults)".
>
> I find it quite problematic for production site updates, since my updates
> generally look like:
>
> generate migrations
> run migrations on prod db, while old webserver code is still running
> after migrations, update webserver
>
> I cannot afford to have breaking problems between steps 2 & 3 if I want to
> add a new db column that is not null and has a default value.
>
> Does anyone here know the motivation for the existing no-defaults-in-the-db
> functionality? I'd like to know, since I'm putting together a backend
> derived from 'django.db.backends.postgresql_psycopg2' that is exactly the
> same, except for setting default values in the db, and I'd like to know what
> concerns there might be for a backend like this?

Before migrations were built-in there was a bug report about this:
. It might be of some help.
It was closed as wontfix. Not sure if the addition of migrations has
changed anyone's mind.

Cheers,
Jon

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CADhq2b5SSyzzYMGaOSyymS%2BsY5xVUU__UH0YbvLgC2qr4Qh-MQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Is there a way to supress blank lines in the HTML?

2014-10-24 Thread Jon Dufresne
Try adding "import re" to the top to import the Python re module.


On Fri, Oct 24, 2014 at 8:35 AM, Daniel Grace  wrote:
> Thanks people for the info.  I was looking for a way to beautify the code,
> rather than for performance / compression.  In particular I came across this
> when I implemented a template tag which, under certain circumstances returns
> an empty string.  This works well but causes blank lines in the code, if
> they are present in the original template.  I did find the almostspaceless
> template tag on http://kuttler.eu/code/django-almost-spaceless-template-tag/
> but I could not get it to work, as I don't know where "re.sub" comes from.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/d4875909-54b2-4655-b459-c71c4bb7efc9%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CADhq2b6C08sYgz4ubqJrtPr4oK3Kiqn5ht4o2GaO7H8Q%2BFeQAg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Is there a way to supress blank lines in the HTML?

2014-10-24 Thread Jon Dufresne
> If you need this for some performance reasons like html compression, you are 
> doing it wrong.
> Instead you could use server-side GZIP compression when serving html which 
> will do the job much more efficiently.

Just a heads up. Compressing dynamic responses over HTTPS is
considered insecure due to the BREACH attack.


Django documentation also contains a blurb about this:


I don't know the specifics here, but the issue is relevant.

Cheers,
Jon

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CADhq2b6uNQKxH10VNfYwYN%3DqVYp9PNwG%2Bcyyvpi5gNWdP54G1g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Restricting file access to users who uploaded the file.

2014-10-21 Thread Jon Dufresne
On Tue, Oct 21, 2014 at 7:09 AM, Ben Lopatin  wrote:
> Presuming you're working with Nginx or
> Apache and local files, you can use X-Accel-Redirects or X-Sendfile to do
> this. Basically the request for a file is made to your Django app, e.g.
> /files/some-file-name.doc, and the app checks if the user can access it. If
> so, the app returns the request with a new header (x-accel-redirect) and the
> file path - this isn't immediately sent to the client, but is instead
> resolved by the web server (i.e. Nginx or Apache) by returning the file from
> its location on the file system. Django verifies, Nginx/Apache does the file
> serving (full explanation here:
> http://wellfireinteractive.com/blog/nginx-django-x-accel-redirects/).

If you're looking for a nice Django app that does this bit for you
with a simple API, check out Django Sendfile
. However, you'll
still need to configure the web server end.

Cheers,
Jon

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CADhq2b6qiFUQVfYfYj7WMsPkCBrTz2%3DvrU0tEDxMuE6oP1U0Zw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: File Storage API: save file to DB model and disk: separate disk path from file name

2014-10-17 Thread Jon Dufresne
On Fri, Oct 17, 2014 at 4:45 PM, Russell Keith-Magee
 wrote:
> Achievable? probably. Easy? No. You're going to be digging in the weeds on
> this one.
>
> If I were trying to do this, I wouldn't try and get this to work with the
> existing FileField - I'd be writing my own Field definition. You can crib
> extensively from the existing definition, but everywhere that the current
> model points at it's own storage to get a filename, you use the foreign key
> to get the full file name.
>
> Essentially, it's going to be a merger of FileField and ForeignKey. I
> haven't done this myself, but my gut tells me this is in the realm of the
> "possible, but complicated".
>
> The other way that might be worth looking into is to follow the lead of
> GenericForeignKey. That's an example of a field that "looks" like a single
> column, but is implemented as two columns under the hood. You'll be looking
> to do a similar thing - write a "wrapper" field that points at the name and
> path fields to produce a composite "full file path" field. That composite
> field will be a lot closer to what FileField is expecting, so it might be a
> little easier to merge *that* with FileField than to merge FileField with
> ForeignKey. Then, you put the composite field on the StoredFile model, and
> traverse down the foreign key relation whenever you need to access the file.

Thanks for the feedback and suggestions.

I'll play around a bit and give it a shot.

My fear with this approach is third party applications with FileFields
will now now handle files differently. Luckily this doesn't affect me
at the moment, but may in the future.

Cheers,
Jon

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CADhq2b7r4KOFjizyNimP4x9vzjD%2B%2BS%2Bh_b0jEq91fz_e_D-5-Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


File Storage API: save file to DB model and disk: separate disk path from file name

2014-10-17 Thread Jon Dufresne
Hi,

I'm trying to use the File Storage API to create a custom handler for
files. Here are my goals:

Stored files are represented both in the database and on disk with the
following model:

class StoredFile(models.Model):
name = models.CharField(max_length=255)
path = models.CharField(max_length=255, unique=True)

The "name" attribute represents the filename presented to the user:
"My document.txt". The path attribute represents the basename as the
file exists on the disk in MEDIA_ROOT, usually created with mktemp:
"tmpAsdf123" in "/path/to/media". The goal of this configuration:

1. Files are stored on disk
2. There are no filename collisions (duplicate names can exist but the
paths will be unique)
3. Uploaded files preserve the name exactly, may be downloaded later,
are never mangled
4. All files are stored in an identical manner

You can think of "name" as an abstract user-oriented name, and path as
the actual on disk representation.

What I want, saving a file should save the file to path and record an
entry for the StoredFile model. Ideally FileFields would be a FK to
this model--currently it is a string.

I'm having a little trouble getting started. Some issues:

1. FileFields store strings, would like it to be a one-to-one FK (or
an intereger id)
2. Most of the storage API takes a single argument "name" Should this
be the name as stored on disk? The name as presented to the user? The
PK from the StoredFile model?

Is what I'm trying to do achievable? Am I approaching the problem correctly?

This design was taken from an existing application, so
interoperability is also a goal.

Cheers,
Jon

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CADhq2b6aCJ3H%2BWA%2BLL7XNo7AxN5XWGNJG_ruRELRs-eY6cWpNA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Nesting custom template tags

2014-03-29 Thread Jon Dufresne
Hi,

I am trying to create a custom template tag that generates a repetitive 
HTML snippet. This tag is an anchor tag "a" with extra classes and styles, 
plus some logic to apply additional classes and styles. I want the syntax 
in the template to look something like:

{% my_anchor URL %}this is text between opening and closing tags{% 
end_my_anchor %}

Where URL is set to the href attribute of the anchor tag.

The rendered result should look something like:

this is 
text between opening and closing tags

I also do not want to hard code my URLs. So as one might expect, I am using 
the "url" template tag to create correct URLs. Naively, I tried to nest the 
tags:

{% my_anchor {% url ... %} %}this is text between opening and closing 
tags{% end_my_anchor %}

But this is invalid syntax. Is there a way to handle this?

Cheers

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/e3f4e619-0560-4f62-ad28-dd4775e2e3c2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Model field's verbose_name from database?

2014-02-05 Thread Jon Dufresne
> Why not use dictionary to Map user-level names to database-fields

I'm not sure I fully follow what you're suggesting. But it sounds like
you're suggesting I move the configuration of field name out of the
database and into a Python file using a dictionary to map actual
fields to preferred field names. Is this correct?

I can't do this because:

* I do not know the user's preference ahead of time
* Their preference could change as time goes on
* It is unreasonable (in my case) to expect my users to modify python files
* These preferences could be different across different installations

For this reason, these fields are configured as a sort of "user
setting". The user is in complete control. But, once they are set,
they should be used everywhere.

Cheers,
Jon

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CADhq2b7%3D_EmMiQtpnLVHUOSPY3wvjBsdiKF%2BbP2WHL%3DhJocvNg%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Model field's verbose_name from database?

2014-02-04 Thread Jon Dufresne
Hi,

I have a model with fields that allows the user to modify the
user-facing names of these fields. Think "first name", "last name",
"email", the user may prefer these be displayed as "given name",
"surname", "email address". This is configured exactly once throughout
the system. So in this example *all* instances of "first name" should
instead be displayed as "given name". Is it possible have a model
where "verbose_name" is not a static string, but instead comes from
the database? Perhaps with some lazy loading trickery? I want to do
something in the spirit of:

first_name = models.CharField(..., verbose_name=lazy_load_name_from_db())

My hope is to eliminate the need to think about what to call this
field when displayed to the user.

Thanks,
Jon

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CADhq2b6hoXB66hE2oWkhXe4TsvNmfWE%2Bw72dHj2ipkyYRGv1bw%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Spammed with 'Invalid HTTP_HOST header' messages

2013-12-06 Thread Jon Dufresne
> Can you not add a filter to your logger handler? 
> http://stackoverflow.com/a/19534738

I will give this a shot.

But what I don't understand is that the post claims that this is fixed
in newer versions. I am using version 1.6. By "fixed" does he simply
mean 500 error is now a 400 error? Or is he implying this should not
be emailed anymore? I assumed the latter which makes me think
something is wrong with my configuration, but I could easily be wrong.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CADhq2b6RSzLPorvAgPESxqHKiOmS1-8cPhcjEZbjyYk%3D%3DUDnbA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Spammed with 'Invalid HTTP_HOST header' messages

2013-12-06 Thread Jon Dufresne
On Fri, Dec 6, 2013 at 7:47 AM, Zach Borboa  wrote:
> SuspiciousOperation should now return a 400 error [1].

It is true, the response is a 400 Bad Request error, but I still
receive emails going to my admins/managers email account. I thought
400 errors shouldn't be mailed out. I would prefer to avoid installing
Sentry if there is a simple fix. Any ideas?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CADhq2b7C7X2%2Bx%3DOUkaECzBMftNBoxM%3D_h%2BhHJbPmZ%3DfV3X%2B3kA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Spammed with 'Invalid HTTP_HOST header' messages

2013-12-05 Thread Jon Dufresne
I am running Django 1.6.

I receive a lot of false positive errors from my Django application
when accessed with an invalid HTTP_HOST. I have verified that these
are in fact HTTP_HOST values that I am not interested in. So I *do*
want to return an error to the user. However, it doesn't help me as an
admin to receive an email every time this happens.

What is the most straight forward way to prevent these errors from
emailing the admins? I want to continue to receive other errors.

Thanks,
Jon

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CADhq2b4hnhJvnocevJVw46Euhf%3Dtz2DizzVPUo91gTQa_NMa2Q%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Apache environment variables in Django 1.6

2013-11-22 Thread Jon Dufresne
> With you setup is it possible to run into some race conditions when two 
> simultaneous request coming for the first time when _application is still 
> uninitialized?

That is a good question, to which I do not have a good answer. If you
come across any issues with this solution I'd be interested to hear
about them.

I filed a ticket #21486 .
If you have any useful information to provide, please add it to the
ticket. This concern over race conditions might be a good thing to
mention.

Cheers,
Jon

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CADhq2b5nO2RBXmAOT91rxE1vwfJVVACR0vHEjKhPvBGsGau-3w%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Apache environment variables in Django 1.6

2013-11-21 Thread Jon Dufresne
On Thu, Nov 21, 2013 at 8:46 AM, Mike Starov  wrote:
> I encountered same issue in my deployment. Have you found a solution?
>

Yes I did. I am still not sure if this is a bug or intentional. It
appears that in 1.6, settings.py is now imported *before* the first
run of the WSGI application. Therefore the settings.py is loaded
before the environment variables can be setup. I now load the WSGI
application as late as possible using the following code. This way,
the settings.py isn't imported until I've received the environ from
Apache. Please feel free to use it to fix your project:

---
import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
_application = None

def application(environ, start_response):
os.environ['MY_SETTING'] = environ['MY_SETTING']
global _application
if _application is None:
from django.core.wsgi import get_wsgi_application
_application = get_wsgi_application()
return _application(environ, start_response)
---

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CADhq2b5YFMNOoC5pdjyXUED6sAEXKMUbCcYCDZxVdpMJ1yHPtg%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Apache environment variables in Django 1.6

2013-11-20 Thread Jon Dufresne
Hi,

I recently upgraded my Django application from 1.5 to 1.6. This
application runs on Apache with mod_wsgi. After upgrading, Django
seems unable to access environment variables set by Apache using
SetEnv and SetEnvIf. Using Django 1.5 I was able to access this using
the following recipe in wsgi.py:

---
import os

from django.core.wsgi import get_wsgi_application
_application = get_wsgi_application()

def application(environ, start_response):
os.environ['MY_SETTING'] = environ['MY_SETTING']
return _application(environ, start_response)
---

Then, in settings.py, I would access MY_SETTING using os.environ['MY_SETTING'].

Is this a bug that this no longer works in Django 1.6? Is there a
better way to access Apache environment variables?

Thanks

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CADhq2b5vxuURi4Zq%2B77dL2EAiDzyrA3C9RHwhiTK6dssAkEqYQ%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Logging warnings inside settings.py

2013-11-01 Thread Jon Dufresne
Hi,

I am trying to log warnings inside settings.py. I want to log warnings
when settings (from an outside source) are not provided, but the
application can continue. Is this possible? It seems like this might
not work as logging requires settings.py in order to be configured,
but I want to log earlier than that.

Thanks,
Jon

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CADhq2b7%3DuvtQpffe3%3DEBZnbKUxe-YNTbc0ykdQEROdWF58TH1A%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


ModelMultipleChoiceField ignore invalid choices?

2013-10-07 Thread Jon Dufresne
Hi,

I am using a ModelForm with a ModelMultipleChoiceField. I would like
for this field to silently ignore invalid choices instead of creating
a form error. Here is my use case:

* User A opens form select model choice 1, 2, and 3
* User B (different browser) deletes model choice 3 entirely from system
* User A submits form and receives error

I understand why this should be an error in the general case, but in
my case, I want this to silently ignore the invalid choice 3 and
continue with choice 1 and 2? Is this possible?

Thanks,
Jon

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CADhq2b5R3owKd%3D%3DGAeJpJD8UBaG4xyq13%2B0mKT%2BXCyeUSckTQA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Advice on deploying multiple Django instances to single Apache server

2013-08-23 Thread Jon Dufresne
Hi,

I am getting ready to deploy my Django application. This application must
serve several independent parties. In the past, I've done this with
multiple instances of a PHP application on the server. Is this the right
approach in the Django world?

My server is running Apache and MySQL. Each instance of the application
will have its own configuration and database. These instances will not
communicate with each other in any way. These instances are all (possibly)
different versions of the same application running in their own virtualenv.

My instinct is to go with the Apache mod_wsgi approach. As Apache is
already running successfully on the server serving PHP an static files.

Any advice or good resources on how to accomplish what I am doing? All the
documentation seems to be targeted at one server with one Django
application.

Thanks,
Jon

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.


Adding custom WHERE conditions to query sets (MySQL user defined functions)

2013-08-20 Thread Jon Dufresne
Hi,

My Django application was ported from a legacy application. This legacy
application has some MySQL user defined functions that are frequently used
in the WHERE clause of queries. Is it possible to use these stored
functions and still return query sets? I want to do something like

MyModel.objects.filter(field_one='blah',
field_two='blah').call('my_function(,
)').order_by('field_three')

Is anything like this possible or do I need to resort to custom SQL? I
don't mind if I need to write a custom QuerySet or Manager. I am concerned
about using custom SQL as most operation and views seem to prefer lazy
query sets instead of lists of models.

Thanks for any help.
Jon

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Importing project URLs without a database

2013-08-01 Thread Jon Dufresne
On Thu, Aug 1, 2013 at 2:38 PM, Phil  wrote:

> Could you post the entire content of your script? There is no way those 2
> lines (and the necessary "os" import) can cause this error.
>
>
>
You are right. I investigated much deeper. Turns out this is what I did.

1. Script imported root URL conf
2. Through an URL include, many other URL confs were imported
3. Many URL confs import CBVs from views.py
4. These views.py may import models or import modules that import models
5. At some point a queryset was evaluated during an import

In this particular case, it was a choices field on a form. I have since
changed this to use ModelMultipleChoiceField.

I guess I'll need to be really careful to not evaluate a queryset or stop
using CBV (to avoid the imports). Seems a bit fragile.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.




Importing project URLs without a database

2013-08-01 Thread Jon Dufresne
Hi,

I am trying to write a script that outputs information about all URLs in
the project. The script should be able to do this without having a database
populated or even created.

Right now the script gets to the following line:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
from project import urls

However, this raises an exception _mysql_exceptions.OperationalError:
(1049, "Unknown database 'project'").

It appears that Django is trying to connect to the database, but all I want
to do is analyze the URLs. Is this possible?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.




Forms with fields created dynamically client side

2013-07-26 Thread Jon Dufresne
Hi,

I am creating a form for the user to add, edit, and remove all tags in the
database. The tag models are handled by taggit. The form will look like like

_Tag1_[del]
_Tag2_[del]
__<--- User enters next tag here

[add tag]

Clicking add tag will fire some javascript to add a new text input to the
form. I trying to create this form in Django to handle the request. This is
what I have so far. I am curious if I am going about this the right way. It
all seems very... roundabout. Thoughts? One part that bothers me is I must
inspect the incoming data to decide how many form elements to create.

Coming from PHP, I would create form names with array notation (tags[pk] =
name) and iterate over the array during the request.

The existing_tag dictionary allows me to map form names to tag objects. The
new_tags list allows me to iterate over tag fields that must be created as
new tags.

class TagsForm(forms.Form):
def __init__(self, data=None, *args, **kwargs):
super(TagsForm, self).__init__(data, *args, **kwargs)

self.existing_tags = {}
self.new_tags = []

for tag in Tag.objects.all():
name = 'tag%i' % tag.pk
self.fields[name] = forms.CharField(
initial=tag.name,
required=False)
self.existing_tags[name] = tag

if data:
for name in data:
if name.startswith('newtags'):
self.fields[name] = forms.CharField(required=False)
self.new_tags.append(name)
else:
name = 'newtags'
self.fields[name] = forms.CharField(required=False)
self.new_tags.append(name)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.




Re: Every view requires authentication

2013-07-18 Thread Jon Dufresne
On Thu, Jul 18, 2013 at 1:36 AM, Tom Evans <tevans...@googlemail.com> wrote:

> On Thu, Jul 18, 2013 at 1:11 AM, Jon Dufresne <jon.dufre...@gmail.com>
> wrote:
> >  Oh, I see what you're saying. The login_required does not need to be
> added
> > explicitly, because it is added implicitly by the authorization check.
> Yeah
> > that makes sense. As every page will require some different authorization
> > anyway, there is no need for extra middleware. Is that right?
> >
>
> No, that's not what he's saying.
>
> "authentication" -> "who am I?"
> "authorization" -> "am I allowed to do this?"
>
> His point was, even though every view requires authentication, it will
> also require authorization - checking that the user has permission to
> access that specific page. Presumably each page will have it's own,
> different permissions check - it's not something you can add with a
> single piece of middleware - and the way that one adds permission
> checks in django is an extension of the login_required decorator.
>
> IE, since you will need to add authorization checks to each view, this
> will implicitly have the same effect as adding login_required to each
> view.
>

Yeah. That is what I said. Authorization check implies an authentication
check.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.




Re: Every view requires authentication

2013-07-17 Thread Jon Dufresne
On Wed, Jul 17, 2013 at 4:24 PM, Arnold Krille  wrote:

> Still interesting that you need authentication while not needing any
> authorization...


 Oh, I see what you're saying. The login_required does not need to be added
explicitly, because it is added implicitly by the authorization check. Yeah
that makes sense. As every page will require some different authorization
anyway, there is no need for extra middleware. Is that right?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.




Re: Every view requires authentication

2013-07-17 Thread Jon Dufresne
On Wed, Jul 17, 2013 at 1:25 PM, Arnold Krille <arn...@arnoldarts.de> wrote:

> On Wed, 17 Jul 2013 11:22:36 -0700 Jon Dufresne
> <jon.dufre...@gmail.com> wrote:The standard-way depende on your views:
>  - If its function-based views, use the @login_required-decorator.
>  - For class-based views we use the LoginRequiredMixin from
>django-braces.
>

Thanks.

This is a whitelist approach to the problem. That is, I must specify every
view that requires login. As nearly 99% of my views will require
authentication, I'd prefer to take a blacklist approach. That is, all views
are assumed to require login, unless I annotate the views to not require a
login. This avoids accidentally leaving views publicly accessible when
someone forgets the login_required decorator (or CBV equivalent).

I can achieve this with middleware (and maybe a decorator), but it occurred
to me that others probably already do this as well. I am curious if there
is a canonical approach or implementation that others use for this very
purpose.

Thanks,
Jon

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.




Every view requires authentication

2013-07-17 Thread Jon Dufresne
My application requires an authenticated user for every view (with very few
exceptions). Is there a standard correct way to handle this or should I
roll my own middleware? I'm probably not the first have this requirement.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.




Re: or-ing to QuerySets turns INNER into LEFT OUTER joins?

2013-07-16 Thread Jon Dufresne
I too would be interested to know if there is a solution. A LEFT OUTER JOIN
can really hurt performance when an INNER JOIN are more appropriate,.


On Mon, Jul 15, 2013 at 8:41 AM, Carsten Fuchs wrote:

> Hi all,
>
> we have two queries/QuerySets Q_a and Q_b, each of which use INNER joins
> in the generated SQL when evaluated individually.
>
> When I use a Q object to "OR" these two QuerySets, the INNER joins turn
> into LEFT OUTER joins -- which in turn cause a huge performance drop
> (several hundred times slower than with INNER joins).
>
> Why is this, and can I do anything against it at the Django ORM level?
>
>
> Details:
>
> My environment:
> Django 1.5.1 on Ubuntu 10.04 (Python 2.6.5) with Oracle database 10g
>
>
> FirstDay = date(2013, 5, 1)
> LastDay = date(2013, 5, 31)
> SomeDep = ...   # the department with ID == 1
>
>
> Q_a = Staff.objects. \
>   filter(bereiche=SomeDep).**distinct()
>
> Q_b = Staff.objects. \
>   filter(erfasst__datum__gte=**FirstDay,
>  erfasst__datum__lte=LastDay,
>  erfasst__bereich=SomeDep).**distinct()
>
> Q_a_or_b = Staff.objects. \
>   filter(Q(bereiche=SomeDep) |
>  Q(erfasst__datum__gte=**FirstDay,
>erfasst__datum__lte=LastDay,
>erfasst__bereich=SomeDep)).**distinct()
>
>
> In the following output, to improve readability I used the "*" to manually
> shorten the list of selected fields:
>
>
> print Q_a.query
>
> SELECT DISTINCT "STAFF".* FROM "STAFF"
> INNER JOIN "STAFF_BEREICHE" ON ("STAFF"."ID" = "STAFF_BEREICHE"."STAFF_ID")
> WHERE "STAFF_BEREICHE"."BEREICH_ID" = 1
>
>
> print Q_b.query
>
> SELECT DISTINCT "STAFF".* FROM "STAFF"
> INNER JOIN "ERFASST" ON ("STAFF"."KEY" = "ERFASST"."KEY")
> WHERE ("ERFASST"."DATUM" <= 2013-05-31  AND "ERFASST"."DATUM" >=
> 2013-05-01  AND "ERFASST"."BEREICH_ID" = 1 )
>
>
> print Q_a_or_b.query
>
> SELECT DISTINCT "STAFF".* FROM "STAFF"
> LEFT OUTER JOIN "STAFF_BEREICHE" ON ("STAFF"."ID" =
> "STAFF_BEREICHE"."STAFF_ID")
> LEFT OUTER JOIN "ERFASST" ON ("STAFF"."KEY" = "ERFASST"."KEY")
> WHERE ("STAFF_BEREICHE"."BEREICH_ID" = 1  OR ("ERFASST"."DATUM" <=
> 2013-05-31  AND "ERFASST"."DATUM" >= 2013-05-01  AND "ERFASST"."BEREICH_ID"
> = 1 ))
>
>
> In the last SQL statement, when I replace "LEFT OUTER JOIN" with "INNER
> JOIN" and manually run it, the result seems to be the same, but the query
> completes one hundred to several hundred times faster.
>
> So, from the above observations, I was wondering if the use of "LEFT OUTER
> JOIN" is what is supposed to happen for Q_a_or_b in the first place? Is it
> the expected behavior?
> And if so, can it be changed via the Django ORM?
>
> Many thanks for your help!
>
> Best regards,
> Carsten
>
>
>
> --
> Dipl.-Inf. Carsten Fuchs
>
> Carsten Fuchs Software
> Industriegebiet 3, c/o Rofu, 55768 Hoppstädten-Weiersbach, Germany
> Internet: http://www.cafu.de | E-Mail: i...@cafu.de
>
> Cafu - the open-source game and graphics engine for multiplayer 3D action
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to 
> django-users+unsubscribe@**googlegroups.com
> .
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at 
> http://groups.google.com/**group/django-users
> .
> For more options, visit 
> https://groups.google.com/**groups/opt_out
> .
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.




Re: [slightly offtopic] Which Python are people using on OSX?

2009-02-05 Thread Jon Dufresne

On Thu, Feb 5, 2009 at 1:07 PM, cjl  wrote:
> 1. Use the stock Python, slightly outdated 2.5.1, with weird and
> incomplete modules.
> 2. Compile Python myself from source.
> 3. Use MacPorts Python. Anyone know why the nearly all of Xorg gets
> built as a dependency?
> 4. Use the macpython .dmg
> 5. Ssh into my linux box
>

6. Install Linux on your MacBook Pro and have the environment you enjoy.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---