build application from admin code

2011-01-04 Thread Santiago Caracol
Hello,

I just started using Django and I am still "marvelling at all the code
I didn't have to write". I wonder if this code, the admin interface,
could be used for the application itself. I would like to do something
like this:

Each user of my application gets his very own predefined objects of a
certain kind, which he can use, change, delete, and so on. He gets his
own urls and views that are but modifications of the automatically
generated admin urls and views. Like so:

example.org/juan/grammar/verbs/add/
(a modification of example.org/admin/grammar/verbs/add/)

or so:

example.org/merche/recipes/delete
(a modification of example.org/admin/recipes/delete)

Juan and Merche are users of example.org, but they can have and
administrate their own users. Juan, e.g., might have the users Maria
and EmacsGuru. Maria and EmacsGuru can only view and manipulate the
pages that Juan allows them to. And Juan can only view and manipulate
the pages that the admin of example.org allows him to.

Has anyone done something of this kind? Is there example code?

Santiago

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.



compiled methods

2011-01-16 Thread Santiago Caracol
Hello,

is there such a thing as "compiled methods" in Django i.e. methods
whose return value is calculated only once and then stored? Or is
there a canonical Djangoish way to implement this?

An example:

Suppose we have phone objects that have a canonical form, such as
"alcatel a-341 i" and a paraphrase type p and a method variants() that
calculates all variants of the canonical form, given the paraphrase
type p, such as:

alcatel a-341 i, alcatel a 341 i, alcatel a341 i, alcatel a-341-i,
alcatel a 341-i, alcatel a341-i, alcatel a-341i, alcatel a 341i,
alcatel a341i, a-341 i, a 341 i, a341 i, a-341-i, a 341-i, a341-i,
a-341i, a 341i, a341i

Obviously it is very bad for speed to calculate the variants of each
phone object every time the object is needed for some action.

Santiago


-- 
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.



Re: compiled methods

2011-01-16 Thread Santiago Caracol
Thank you very much! The term "memoize" was exactly what I was looking
for.

Santiago

-- 
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.



override save()

2011-01-17 Thread Santiago Caracol
Hello,

I have got a model called ContentClassifier which uses regular
expressions to classify the content of messages. It has a method
compile() that compiles an instance's regular expressions into
self.posrx. When the classify method calls compile, i.e. when each
ContentClassifier compiles its regular expressions every time it's
classify-method is called, then the ContentClassifiers work as
expected. (I.e. if, in the code given below snippet 2 instead of
snippet 1 is used). If, on the other hand, I want each instance to
compile its regular expressions when it is saved (i.e. if I use
snippet 1 instead of snippet 2), Django says

AttributeError: ... ContentClassifier' object has no attribute 'posrx'

Why?

#

class ContentClassifier(models.Model):

def save(self, *args,
**kwargs):  # snippet 1
 
self.compile()
# snippet 1
super(ContentClassifier, self).save(*args, **kwargs)   #
snippet 1


def compile(self):
...
self.posrx = re.compile(regex_string, re.IGNORECASE)

def classify(self, message):
# self.compile() # snippet 2

#

-- 
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.



Re: override save()

2011-01-17 Thread Santiago Caracol
Sorry the formatting of the code went wrong. Here it is again:

#

class ContentClassifier(models.Model):

# 
def save(self, *args, **kwargs):
self.compile()
super(ContentClassifier, self).save(*args, **kwargs)
# 

def compile(self):
...
self.posrx = re.compile(regex_string, re.IGNORECASE)

def classify(self, message):
# 
# self.compile()
# 

#

-- 
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.



Re: override save()

2011-01-17 Thread Santiago Caracol

> Since these attributes
> are "ordinary" attributes, they aren't saved to the database, so they
> aren't loaded from it neither.

It seems that this was the problem. Thank you.

Santiago

-- 
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.



slight model change makes app unusable

2011-01-19 Thread Santiago Caracol
Hello,

I have got a model MobilePhone that has a producer, such as "alcatel",
a canonical name, such as "b-500 s" and a paraphrase type, such as
"a-341 i".  Each instance of MobilePhone is supposed to calculate all
its variants (such as alcatel b-500 s, alcatel b 500 s, alcatel b-500-
s, b-500 s, b 500 s, b-500-s, and so on). This worked perfectly as
long as the producer wasn't a field of its own, but part of the
canonical name. Since I introduced producer names (in order to
simplify the paraphrase types and to allow for variants of producer
names (cf. HP, Hewlett Packard and so on), the app became unusable:
Whenever one wants to see the list of all (461) mobile phones in the
admin interface, the development server becomes very slow and
eventually breaks down with a memory error. Below is the class
MobilePhone. What can I do?

##

class MobilePhone(models.Model):

producer = models.ForeignKey(MobilePhoneProducer)
canonical_name = models.CharField(max_length=200)
paraphrase_type = models.CharField(max_length=50, choices=(
('n-127', 'n-127'),
('a-341 i', 'a-341 i'),
('neotouch', 'neotouch'),
))
lexical_variants = models.ManyToManyField(LexicalVariant,
blank=True)
n127_rx = re.compile('^(?P[a-z]+)\-(?P[0-9]+)$')
a341_i_rx = re.compile('^(?P[a-z]+)\-(?P[0-9]+) (?
P[a-z])$')
neotouch_rx = re.compile('^(?P[a-z]+)$')

def save(self, *args, **kwargs):
self.canonical_name =
whitespace_normalize(self.canonical_name)
super(MobilePhone, self).save(*args, **kwargs)

def __unicode__(self):
return self.canonical_name

def variants(self):
self.save()
paraphrases = self.paraphrase()
for paraphrase in paraphrases:
paraphrases.append(self.producer.name + ' ' + paraphrase)
for variant in self.lexical_variants.all():
paraphrases.append(variant)
paraphrases.append(self.producer.name + ' ' + variant)
if paraphrases:
return ', '.join(paraphrases)
else:
return '(The paraphrase type doesn
\'t seem to match.)'
variants.allow_tags = True

def the_lexical_variants(self):
return ', '.join(self.lexical_variants.all())

the_lexical_variants.short_description = "Lexical variants"

def paraphrase(self):
if self.paraphrase_type == 'n-127':
test = self.n127_rx.search(self.canonical_name)
if test:
chars = test.group('chars')
numb = test.group('numb')
return [
chars + '-' + numb,
chars + numb,
chars + ' ' + numb,
]
elif self.paraphrase_type == 'a-341 i':
test = self.a341_i_rx.search(self.canonical_name)
if test:
chars1 = test.group('chars1')
chars2 = test.group('chars2')
numb = test.group('numb')
return [
chars1 + '-' + numb + ' ' + chars2,
chars1 + ' ' + numb + ' ' + chars2,
chars1 + numb + ' ' + chars2,
chars1 + '-' + numb + '-' + chars2,
chars1 + ' ' + numb + '-' + chars2,
chars1 + numb + '-' + chars2,
chars1 + '-' + numb + chars2,
chars1 + ' ' + numb + chars2,
chars1 + numb + chars2,
]
elif self.paraphrase_type == 'neotouch':
test = self.neotouch_rx.search(self.canonical_name)
if test:
gmodel = test.group('model')
return [
gmodel,
]
return []

###

-- 
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.



use variable that is bound at an object's creation time at the object's creation time

2011-01-28 Thread Santiago Caracol
Hello,

my model Product has an attribute spelling_variants and a property
variants:

class Product(models.Model):
   canonical_name = models.CharField(max_length=200)
   spelling_variants = models.ManyToManyField(String,
related_name="product spellings", blank=True)
   ...

   @property
   def variants(self):
  variants = []
  for v in self.spelling_variants:
 variants += paraphrase(v)
  return variants

The code works, if product instances are first saved without a vaue
for spelling_variants and later modified so as to have some spelling
variants. When I add spelling variants at the time when I create a
product in the admin interface, I get this error:

Caught TypeError while rendering: 'NoneType' object is not iterable

I think the problem in these cases is that when Python tries to
execute self.variants() it does not yet have a value for
self.spelling_variants.

How can I make sure that self.spelling_variants has a value when
self.variants() is executed?

Santiago

-- 
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.



blank ModelMultipleChoiceField, sort a ManyToManyField that is allowed to be blank

2011-02-01 Thread Santiago Caracol
Hi,

why can't a ModelMultipleChoiceField not have the property
"blank=True"?

I need the ModelMultipleChoiceField to sort the values of a
models.ManyToManyField in a form class. Sorting works as expected, but
only if the field is obligatory. (Else, I get the error message
"TypeError: __init__() got an unexpected keyword argument 'blank'.)

Is there another way to sort a models.ManyToManyField that has the
property "blank=True"?

Santiago

-- 
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.



Re: blank ModelMultipleChoiceField, sort a ManyToManyField that is allowed to be blank

2011-02-01 Thread Santiago Caracol
> No form fields take that argument - that's for model fields.
>
> Form fields take the argument "required", which defaults to True.

Thank you! That is exactly the information I needed.

By the way, is there a special reason why "blank" of model fields
translates to "required" of form fields?

Santiago

-- 
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.



Re: blank ModelMultipleChoiceField, sort a ManyToManyField that is allowed to be blank

2011-02-01 Thread Santiago Caracol
Ah, now there is a new problem.

Before, my ManyToManyField was unsorted, but it was possible to add
new items by clicking on the "+" button. Now my items are sorted
thanks to a ModelMultipleChoiceFormField (with "order_by('...')" and
"required=False"), but the "+" button is gone.

Is there a way to get the "+" button back?

-- 
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.



compiled regex as attribute

2011-02-11 Thread Santiago Caracol
Hello,

I have got objects with very large regular expressions:

class Product(models.Model):
   # ...
canonical_name = models.CharField(max_length=200)
spelling_variants = models.CharField(max_length=1, blank=True)
lexical_variants = models.CharField(max_length=1, blank=True)
excluded = models.CharField(max_length=1, blank=True)
permutations = models.CharField(max_length=1000, blank=True)

When a product has values for all of the above attributes, it can
calculate all its possible names. Example: the product 'google android
1.1' has several hundred possible names including these:

google android-1.1, oha android-1.1, android-1.1, google android-1,1,
oha android-1,1, android-1,1, google android-1-1, oha android-1-1,
android-1-1, google android-11, oha android-11, android-11, google
android-1 1, oha android-1 1, android-1 1, google android1.1, oha
android1.1, android1.1, google android1,1, oha android1,1, android1,1,
google android1-1, oha android1-1, android1-1, google android11, oha
android11, android11, google android1 1, oha android1 1, android1 1,
google android 1.1, oha android 1.1, android 1.1, google android 1,1,
oha android 1,1,

There are products with many more names. Consider the name of the
mobile phone alcatatel one-touch-800-one-touch-chrome.

Currently every product calculates all its possible names and a
regular expression that includes the disjunction of all the possible
names and compiles this regular expression at the time when the
product is used. This is very inefficient. I would like to compile and
store the regular expression only once: at the time when the new
product is saved.

However, there doesn't seem to be a models.RegexField. There is a
forms.RegexField, but I don't understand how I could use it in the
product model.

Any tips how to store the compiled regex?

Santiago

-- 
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.



Re: compiled regex as attribute

2011-02-11 Thread Santiago Caracol
> http://pypi.python.org/pypi/django-picklefield/0.1

Thanks for the tip. This works, but it has one great disadvantage: The
regexes can't be stored in fixtures. They are stored in the database
directly. So it seems I would have to insert all my several thousand
products manually again. And if I change some models and need to do a
"manage.py syncdb" all the work will be lost ...

Or is there a way of loading fixtures that includes that save() is
called for each loaded object?

-- 
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.



Re: compiled regex as attribute

2011-02-11 Thread Santiago Caracol
> Since the pickled value is a string, it should work in fixtures.

There is no point in storing the regex strings in a pickle field. I
already have the regex strings in ordinary django fields. What I want
to store is *compiled* regular expressions in order to be able to use
them without having to compile them first.

-- 
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.



reimport module every n seconds in main process

2011-02-16 Thread Santiago Caracol
Hello,

in a Django application I compile certain data into a Python module
for efficiency reasons. Each time an admin changes or adds objects,
this module also changes. Therefore, I want my main process (the one
that is started with "manage.py runserver" or the like) to reimport
the data module every n (e.g. 10) seconds.

How could this be done?

Santiago

-- 
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.



'NoneType' object has no attribute 'set_cookie'

2011-03-31 Thread Santiago Caracol
Hello,

I didn't use a certain Django project for a few weeks. Now I wanted to
continue developing the project and, as it usually happens with
software that has been left alone for a while, it didn't work
anymore. I am not aware of having changed anything. The system
administrator may, of course, have made some changes to the
system. When I try to add a new object, regardless of which type, in
the admin interface, I get the error messages given below. (I had to
shorten the error message for Google Groups to allow me to post it.)

Any ideas what I could do?

Santiago

##


AttributeError at /admin/monitoring/message/add/

'NoneType' object has no attribute 'set_cookie'

Request Method: GET
Request URL:http://localhost:8000/admin/monitoring/message/add/
Django Version: 1.3
Exception Type: AttributeError
Exception Value:

'NoneType' object has no attribute 'set_cookie'

Exception Location: /usr/local/lib/python2.6/site-packages/django/
middleware/csrf.py in process_response, line 239
Python Executable:  /usr/bin/python
Python Version: 2.6.2
Python Path:[...]

Server time:Thu, 31 Mar 2011 10:10:30 +0200
Traceback Switch to copy-and-paste view

[...]

  response

  None

  result

  None

  kwargs

  {}

  view_func

  

* /usr/local/lib/python2.6/site-packages/django/middleware/csrf.py
in process_response
   232.

if request.META.get("CSRF_COOKIE") is None:

   233.

return response

   234.

   235.

if not request.META.get("CSRF_COOKIE_USED",
False):

   236.

return response

   237.

   238.

# Set the CSRF cookie even if it's already set, so
we renew the expiry timer.

   239.

response.set_cookie(settings.CSRF_COOKIE_NAME,

...
   240.

request.META["CSRF_COOKIE"], max_age = 60
* 60 * 24 * 7 * 52,

   241.

domain=settings.CSRF_COOKIE_DOMAIN)

   242.

# Content varies with the CSRF cookie, so set the
Vary header.

   243.

patch_vary_headers(response, ('Cookie',))

   244.

response.csrf_processing_done = True

   245.

return response

  ▶ Local vars

-- 
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.



django-admin.py startproject mysite

2011-04-04 Thread Santiago Caracol
Hello,

the Django (1.3) tutorial says:

>From the command line, cd into a directory where you’d like to store
your code, then run the command django-admin.py startproject mysite.
This will create a mysite directory in your current directory.

When I run

django-admin.py startproject mysite,

I get this error:

Unknown command: 'startproject'
Type 'django-admin.py help' for usage.


Using django-admin.py help, I get these subcommands:

Available subcommands:
  changepassword
  cleanup
  collectstatic
  compilemessages
  createcachetable
  createsuperuser
  dbshell
  diffsettings
  dumpdata
  findstatic
  flush
  inspectdb
  loaddata
  makemessages
  reset
  runfcgi
  runserver
  shell
  sql
  sqlall
  sqlclear
  sqlcustom
  sqlflush
  sqlindexes
  sqlinitialdata
  sqlreset
  sqlsequencereset
  startapp
  syncdb
  test
  testserver
  validate

Does the Django tutorial still match reality?

Santiago

-- 
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.