Re: Violate DRY in my models

2008-01-06 Thread Nikolaus Schlemm

Am Sonntag, 6. Januar 2008 13:04:38 schrieb Brot:
> I have a few models which have the same two fields. Based on these
> fields there is always the same function in these models. But this
> violates DRY. Is there another possibility for my function/models
>
> [...]
>
> Model Inheritance isn't supported yet. Is there another way to avoid
> the duplicated code/function?

if you want to reduce the amount of duplicated code, simply create an abstract 
class and inherit that in your models like this:

class ValidFromTo(object):
def is_active(self):
return self.valid_from < datetime.now() < self.valid_to
is_active.short_description = 'Active'
is_active.boolean = True

class EventType(models.Model, ValidFromTo):
    
    valid_from = models.DateTimeField(default=datetime.now)
    valid_to = models.DateTimeField(default=DEF_VALID_TO)
    ..

class Event(models.Model, ValidFromTo):
    
    type = models.ForeignKey(EventType,
                             limit_choices_to =
                                    {'valid_from__lte': datetime.now,
                                     'valid_to__gte': datetime.now})
    valid_from = models.DateTimeField(default=datetime.now)
    valid_to = models.DateTimeField(default=DEF_VALID_TO)
    ..

class Partner(models.Model, ValidFromTo):
    
    valid_from = models.DateTimeField(default=datetime.now)
    valid_to = models.DateTimeField(default=DEF_VALID_TO)
    ..

not perfect yet since you still have to add the fields redundantly, but it's a 
first step ;)
-- 
MfG

Nikl

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Download counter?

2006-09-18 Thread Nikolaus Schlemm

hi,

Am Dienstag, 19. September 2006 01:41 schrieb frank:
> This will probably show my incredible lack of understanding regarding
> web
> servers vs. django once more, but...
>
> Is there a reasonably simple and transparent (to the user) way of
> counting downloads?  Sure, I could create a download button for each
> item, but that would be an extra step for the user.  'It would be nice'
> if that weren't necessary.
> BTW the downloads are static files, served by Apache.  That extra
> button
> is looking more and more likely...

that should be trivial if your webserver provides you with access logs (which 
it most likely already does) - just grep through them...
-- 
cheers,

Nikl

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: a conflict between django and php4

2006-09-18 Thread Nikolaus Schlemm

hi,

Am Dienstag, 19. September 2006 07:28 schrieb zhongke chen:
> Thanks! i solve this by removing libmhash package. but it's only a
> workaround solution. i expect to see a perfect one.

unfortunately, the problem seems to be somewhere within modpython which 
somehow doesn't use the md5-module that's used when running python in scripts 
or your interpreter... on the modpy-mailinglist I had posted an example that 
used the checksums from the md5-rfc. sadly this would only work correctly 
after the mentioned workarounds (disabling mod-php or uninstalling libmhash).

if both those two workarounds don't work for you, I have offered a third one 
on:

http://code.djangoproject.com/ticket/2249
-- 
cheers,

Nikl

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: Serialization on part of a model

2006-10-06 Thread Nikolaus Schlemm

> I submitted http://code.djangoproject.org/ticket/2701 which provides a
> small patch to limit by field for the serializers module.  Something
> like -- serializers.serialize('xml', stories, fields=('headline',
> 'name'))

excellent, definetely a "+1" from me!
-- 
cheers,

Nikl

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: Displaying ManyToMany relation in template

2006-10-31 Thread Nikolaus Schlemm

hi,

try something like the following:

> {% if latest_posts %}
> {% for post in latest_posts %}
> 
> {{ post.title }}
> 
> publié le {{
> post.pub_date|date:"l d F Y à H:i" }}, dans la catégorie

{% for category in  post.category.all %}
{{ category.title }}
{% endfor %}

> 
> {{ post.summary }}
> 

{% for tag in post.tag.all %}
{{ tag }}
{% endfor %}

>  
> {% endfor %}
> {% else %}
> Aucun article ne correspond aux critères.
> {% endif %}

-> http://www.djangoproject.com/documentation/models/many_to_many/

> Thanks in advance,
> Nicolas
-- 
you're welcome!

Nikl

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: Segmentation fault with mod_python (Ubuntu Edgy Eft)

2007-01-11 Thread Nikolaus Schlemm

hi James,

> I'm running Ubuntu Edgy Eft, with the following versions (updated to
> the most recent available from  apt-get)
>   Apache/2.0.55 (Ubuntu) mod_python/3.2.8 Python/2.4.4c1 PHP/5.1.6
> mod_ssl/2.0.55 OpenSSL/0.9.8b
> and Django 0.95.
I'm running pretty much the same combo, but with django trunk (and apache also 
serves my svn repositories) - works like a charm!

> If I disable mod_php then the site does work, so there is some sort
> of conflict but I am unsure how to resolve what that conflict may be.
> Django is partly working as it gives me the Page not found debug page
> for 404s.
can you get it to work completely?

> Is anyone running successfully on Edgy Eft while still using PHP? Is
jepp

> there anything anyone can suggest to diagnose further?
- have you tried to increase apache's loglevel?
- do you have any php-extensions installed? if so, you might want to check out 
if php is really the factor by loading it without any extensions. if that 
works, try adding them back one by one.
- can you also rule out other possibilities? (corrupt memory and stuff like 
that)
-- 
cheers,

Nikl

--~--~-~--~~~---~--~~
 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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Segmentation fault with mod_python (Ubuntu Edgy Eft)

2007-01-11 Thread Nikolaus Schlemm

> Aha! Not sure why I didn't think of that, but a back and forth
> switching php modules on and off eventually led me to mhash.so -
> switching off that module solved all my woes.
ouch mhash again ... a couple of months ago that interfered with md5 for some 
reason resulting in wrong digests, but segfaults? might be useful if you 
report this to modpython (and possibly ubuntu) as well.
-- 
cheers,

Nikl

--~--~-~--~~~---~--~~
 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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



generating md5-hashes in modpython fails

2006-06-27 Thread Nikolaus Schlemm
hi all,

for some (up to now) unknown reasons the generation of md5-hashes fails in 
modpython (for me and at least one other user on irc). I discovered this bug 
while switching a django-setup from the builtin dev-server to modpython: it 
complained that I had tampered with the session cookies - which I didn't ;)

the session ids looked a little unusual: all hashes started with 16 leading 
zeros. So next, I tried generating md5-hashes in django and they also had the 
leading zeros PLUS when generating a couple of hashes for different strings 
in one request the resulting hashes were all the same. After that I tested 
md5 hash generation in "plain" modpy - which had the same problems. the odd 
thing is: using the md5-module in "regular" python works totally fine! since 
this clearly seems to be a modpy issue, there's a thread on the modpy 
mailinglist:
http://modpython.org/pipermail/mod_python/2006-June/021482.html
note that, there already have been confirmations that this is not a general 
modpython issue.

the first workaround was to replace md5 with sha - but since that can hardly 
be called a solution, I propose that the use of hashing algorithms should 
be "uncoupled" from the actual implementation used. the appended diff might 
be a start of into that direction, but certainly might need some clean up ;)
-- 
cheers,

Nikl


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---
Index: conf/global_settings.py
===
--- conf/global_settings.py	(revision 3212)
+++ conf/global_settings.py	(working copy)
@@ -221,6 +221,9 @@
 # Hint: you really don't!
 TRANSACTIONS_MANAGED = False
 
+# Which hashing algorithm do you prefer? (see django.util.hashes for available algorithms)
+FAVORITE_HASH_ALGO = "md5"
+
 ##
 # MIDDLEWARE #
 ##
Index: contrib/sessions/models.py
===
--- contrib/sessions/models.py	(revision 3212)
+++ contrib/sessions/models.py	(working copy)
@@ -1,6 +1,7 @@
-import base64, md5, random, sys
+import base64, random, sys
 import cPickle as pickle
 from django.db import models
+from django.utils.hashes import hash
 from django.utils.translation import gettext_lazy as _
 from django.conf import settings
 
@@ -8,15 +9,15 @@
 def encode(self, session_dict):
 "Returns the given session dictionary pickled and encoded as a string."
 pickled = pickle.dumps(session_dict)
-pickled_md5 = md5.new(pickled + settings.SECRET_KEY).hexdigest()
-return base64.encodestring(pickled + pickled_md5)
+pickled_hash = hash.new(pickled + settings.SECRET_KEY).hexdigest()
+return base64.encodestring(pickled + pickled_hash)
 
 def get_new_session_key(self):
 "Returns session key that isn't being used."
 # The random module is seeded when this Apache child is created.
 # Use person_id and SECRET_KEY as added salt.
 while 1:
-session_key = md5.new(str(random.randint(0, sys.maxint - 1)) + str(random.randint(0, sys.maxint - 1)) + settings.SECRET_KEY).hexdigest()
+session_key = hash.new(str(random.randint(0, sys.maxint - 1)) + str(random.randint(0, sys.maxint - 1)) + settings.SECRET_KEY).hexdigest()
 try:
 self.get(session_key=session_key)
 except self.model.DoesNotExist:
@@ -49,8 +50,8 @@
 
 def get_decoded(self):
 encoded_data = base64.decodestring(self.session_data)
-pickled, tamper_check = encoded_data[:-32], encoded_data[-32:]
-if md5.new(pickled + settings.SECRET_KEY).hexdigest() != tamper_check:
+pickled, tamper_check = encoded_data[:hash.digest_size*-2], encoded_data[hash.digest_size*-2:]
+if hash.new(pickled + settings.SECRET_KEY).hexdigest() != tamper_check:
 from django.core.exceptions import SuspiciousOperation
 raise SuspiciousOperation, "User tampered with session cookie."
 try:
Index: contrib/admin/views/decorators.py
===
--- contrib/admin/views/decorators.py	(revision 3212)
+++ contrib/admin/views/decorators.py	(working copy)
@@ -2,8 +2,9 @@
 from django.conf import settings
 from django.contrib.auth.models import User, SESSION_KEY
 from django.shortcuts import render_to_response
+from django.utils.hashes import hash
 from django.utils.translation import gettext_lazy
-import base64, datetime, md5
+import base64, datetime
 import cPickle as pickle
 
 ERROR_MESSAGE = gettext_lazy("Please enter a correct username 

Re: generating md5-hashes in modpython fails

2006-06-27 Thread Nikolaus Schlemm
I forgot to append this file...
-- 
cheers,

Nikl


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---


hashes.py
Description: application/python


Re: SuspiciousOperation: User tampered with session cookie

2006-06-27 Thread Nikolaus Schlemm

> I've heard that the problem might be related to md5 hashing (someone
> just recently posted it here in the groups with the same error
> message). I looked at the django code and that where that error message
> is thrown and it seemed to confirm that, but I don't know why this
> happens.
simply try generating the reference hashes provided at the end of the md5 
rfc[1] within a django-view - if they are not calculated correctly, you might 
want to switch from md5 to sha for a quick workaround and possibly follow the 
thread on the modpy mailinglist[2].

another solution might be something along the lines of the patch I posted 
earlier[3] - but of course, I don't know whether or when this will make it in 
into django ;)

[1] http://www.ietf.org/rfc/rfc1321.txt
[2] http://modpython.org/pipermail/mod_python/2006-June/021482.html
[3] 
http://groups.google.com/group/django-users/browse_thread/thread/eeb44c894342d6f7/4b951b6d3a8644dd
-- 
cheers,

Nikl

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: SuspiciousOperation: User tampered with session cookie

2006-06-27 Thread Nikolaus Schlemm

Am Dienstag, 27. Juni 2006 19:25 schrieb [EMAIL PROTECTED]:
> Clear the cookies in your browser and the problem will go away.
unfortunately that won't solve this problem - possibly another one ;)
-- 
cheers,

Nikl

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: generating md5-hashes in modpython fails

2006-07-05 Thread Nikolaus Schlemm

for anybody who's also experiencing this problem, Alain Tesio mentioned 
another odd factor and its workaround on the modpy-mailinglist:

http://modpython.org/pipermail/mod_python/2006-July/021544.html
-- 
cheers,

Nikl

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: How to ?

2006-07-15 Thread Nikolaus Schlemm

hi,

> but how can I find a user for a particular photo?

wanted_user = particular_photo.gallery.created
-- 
cheers,

Nikl

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: How to ?

2006-07-15 Thread Nikolaus Schlemm

> >>> p.gallery_id.created_id
>
> Traceback (most recent call last):
>   File "", line 1, in ?
> AttributeError: 'long' object has no attribute 'created_id'
>
> Where is a problem?
your photo object "p" has an attribute "gallery" which references a gallery 
object (the one with id=p.gallery_id) and this gallery object in turn has an 
attribute "created" which references a user object (the one with 
id=p.gallery.created_id).

therefore I wrote

wanted_user = particular_photo.gallery.created

does that not work?
-- 
cheers,

Nikl

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: How to ?

2006-07-15 Thread Nikolaus Schlemm

Am Samstag, 15. Juli 2006 15:12 schrieb PythonistL:
> Not sure I fully understand.
> Can you please explain a little more?
>
> Do you mean that
> p.gallery.created
> should work(  where p is my photo object)?
exactely - you might want to take a look at

http://www.djangoproject.com/documentation/models/many_to_one/

and propably study the docs a little more:

http://www.djangoproject.com/documentation/model_api/#relationships

;)
-- 
cheers,

Nikl

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: Linked select pull down menu question

2006-08-10 Thread Nikolaus Schlemm

hi Gwilym,

> I am a newbie to Django, I am looking to move an application I have
> written in PHP a couple of years ago to Django and need to know if
> there is an easy way to generate a linked pulldown menu in a form.
>
> Previously I have used Javascript so if you select a category in one
> pulldown the second pulldown loads the array of sub-categories
> associated to the category without re-loading the page.
>
> Does that make sense?
>
> Gwilym

up to now it's quite difficult to replace javascript functionality with 
python ;)

but you could replace the php stuff with python though. and then mostly just 
keep the javascript, possibly adjusting a url or two.
-- 
cheers,

Nikl

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---