Re: Naive redirection programming question

2009-09-03 Thread Spajderix

I don't know if this is the best solution, but I made a 
context_preocessor which add content from session variable to every 
template i load. I then put in my layout a variable to show it. Finally, 
every time I want to show some message i add it to a given variable in 
session. Code for context processor looks like this:

def 
flash(request): 
 

#return {'flash': {'notice': 
'works'}}   


notice = request.session.get('flash_notice', 
False)  



request.session['flash_notice']=False   
 

warning = request.session.get('flash_warning', 
False)  
  


request.session['flash_warning']=False  
 

error = request.session.get('flash_error', 
False)  
  


request.session['flash_error']=False
 


 

#sprawdza czy sa w ogole jakiekolwiek 
flashe  
   

if notice or warning or 
error:  
 

messages = 
True
  


else:   
 

messages = 
False   
  


 

return {'flash': {'notice': notice, 'warning': warning, 'error': 
error, 'messages': 
messages}}  

it checks for three types of messages: notice, warning and error. It 
also add additional variable 'messages' to indicate if there is any 
message to show at all. So, the only thing you need to do is add a 
message to given session variable and reload/redirect a page. You can 
read up on context processors and how to use them in django documentation.

Rodney Topor pisze:
> Suppose you've just processed posted form data and successfully added
> a new item to the database.  Now you return HttpResponseRedirect('/
> items/') (I know, you should use a pattern name not an absolute URL
> here) to display the list of items, including the newly added item.
>
> But, suppose you want to prefix the list of items with a message,
> e.g., "Congratulations, your item was successfully added.".  If you
> were returning render_to_response('item_list.html"), you could pass in
> a dictionary {"message": message}, and the template could display the
> variable message if it is not empty.
>
> How can you do something similar when using HttpResponseRedirect()?
>
> Or am I thinking about this problem the wrong way?
>
> Thanks.
> >
>   


--~--~-~--~~~---~--~~
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: Standalone script with django's ORM and multiprocessing

2009-08-11 Thread Spajderix

prabhu S pisze:
> Solution appears like a hack to me. Why do you close the connection in
> every process? Can you not just close once in parent? Execute commits
> alone in each process.
>   
I've checked that. Unfortunately, in my case, it won't work. I have a 
loop looking like this:

for job in joblist:
p = Process(target=self.run_job)
p.start()

When i close connection before this loop i get errors like at the 
beggining. When i close connection just before creating a new process i 
get: 'lost connection while performing a query' (or something like that 
:)). Finally when i close connection just right after this loop it 
starts to behave weirdly. One time it works fine, but next time i launch 
it i get mysql has gone away errors. I suppose this happens because 
sometimes subprocesses fire up queries after parent finishes this loop, 
but sometimes they're faster and still use parent's connection.

Closing connections in subprocesses might look a bit hacky. I could try 
to somehow reinitialize db connection, but simply closing it, and 
leaving the rest to django is a lot easier :)

Thank you for your replies. I appreciate it.

Regards
Spajderix

--~--~-~--~~~---~--~~
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: Standalone script with django's ORM and multiprocessing

2009-08-11 Thread Spajderix
Malcolm Tredinnick pisze:
> On Tue, 2009-08-11 at 11:06 +0200, Spajderix wrote:
>   
>> Hi!
>>
>> I've written a standalone script, which looks throught a table in db for 
>> tasks to perform. It then starts subprocesses for each task it founds 
>> using multiprocessing.Process class from python. Everything's fine when 
>> there is one task, but when i try to start more subprocesses at once i get:
>>
>> OperationalError: (2013, 'Lost connection to MySQL server during query')
>> raise errorclass, errorvalue
>> OperationalError: (2006, 'MySQL server has gone away')
>>
>> I guess that this happens because all subprocesses share one connection, 
>> and when one of them closes this connection, rest of subprocesses raises 
>> an error. 
>> 
>
> That certainly sounds believable. We call close() explicitly, too, so
> that is why ongoing operations are interrupted in the middle.
>
>   
>> Do you know a way to go round this problem?
>> 
>
> If you close the database connection, Django will open a new one the
> next time it needs it. So I suspect you can work around this by
> explicitly closing the connection immediately after you start a new
> process (in the new process). Then the process will get its own
> connection when you try to do something.
>
> Regards,
> Malcolm
>
>   
Thank you! That solved the problem:)

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



Standalone script with django's ORM and multiprocessing

2009-08-11 Thread Spajderix

Hi!

I've written a standalone script, which looks throught a table in db for 
tasks to perform. It then starts subprocesses for each task it founds 
using multiprocessing.Process class from python. Everything's fine when 
there is one task, but when i try to start more subprocesses at once i get:

OperationalError: (2013, 'Lost connection to MySQL server during query')
raise errorclass, errorvalue
OperationalError: (2006, 'MySQL server has gone away')

I guess that this happens because all subprocesses share one connection, 
and when one of them closes this connection, rest of subprocesses raises 
an error. Do you know a way to go round this problem?

Regards
Spajderix

--~--~-~--~~~---~--~~
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: HttpResponseRedirect with POST

2009-08-06 Thread Spajderix

Hi,

wouldn't it be easier to put this data to session?

SÅ‚awek Tuleja pisze:
> HI everyone!
>
> Can I redirect to view with form passing few variables in POST method
> (request.POST) and showing them in the form? (i need this in admin
> action)
>
> for example this works:
> def add_mailing(self, request, queryset):
> return HttpResponseRedirect('%s?subject=halo' % \
> urlresolvers.reverse
> ('admin:tenders_mailing_add'))
>
> but this does not:
> def add_mailing(self, request, queryset):
> post = request.POST.copy()
> post.update({'subject': 'halo'})
> request.POST = post
> return HttpResponseRedirect(urlresolvers.reverse
> ('admin:tenders_mailing_add'))
>
> * subject is name of text field in a form
> >
>   


--~--~-~--~~~---~--~~
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: Calculating a date

2009-08-05 Thread Spajderix

 From what i know, you can operate on datetime.datetime objects using 
datetime.timedelta, eg:

datetime.datetime.now() - datetime.timedelta(days=84)

will give you datetime.datetime object with date of today - 12 weeks

Link to manual: 
http://docs.python.org/library/datetime.html#datetime.timedelta

adelaide_mike pisze:
> This should really be a Python enquiry, but I am sure someone will
> know:
>
> I need to calculate the date 12 weeks before today.  What is the best
> way?  TIA
>
> Mike
> >
>   


--~--~-~--~~~---~--~~
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: buildung query with ORM

2009-08-04 Thread Spajderix

tom pisze:
> Hi group,
>
> i build a model to save measurement data. a measurement entry has
> always a channel (that is the source where the data comes from), a
> value (for example 1.04), a unit (for example m/s or Hz) and a
> timestamp.
> The timestamp and the channel are together unique. every channel can
> only have one data entry for one timestamp.
>
> i have the following model:
>
> class Data(models.Model):
> datetime = models.DateTimeField(help_text='the date and time of
> the measurement value', db_index=True)
> channel = models.CharField(help_text='the channelname',
> max_length=20, db_index=True)
> value = models.FloatField(help_text='the measurement value',
> db_index=True)
> unit = models.CharField(help_text='the unit of the measurement
> value', max_length=20, db_index=True)
> class Meta:
> unique_together = [('datetime', 'channel')]
>
> now, i want to have a query to get the following data-table:
>
> datetimechannel1 unit1channel2
> unit2
> 2009-01-01 00:00:00   10.0m/s 1000  Hz
> 2009-01-01 00:00:10   15.0m/s 1040  Hz
> 2009-01-01 00:00:20   16.0m/s 1563  Hz
>
>
> Is something like this possible with django?
> i know that i can get all data ordered by date and the use python to
> get a datastructure like the described table. but i want to do it with
> the database because i think it's much faster.
>
> any ideas how to do this?
>
>
> Best regards,
>
> tom
>
> >
>   
I would recommend a bit different solution. Instead of one, use two 
tables like this:

class Entry(models.Model):
datetime = models.DateTimeField(help_text='the date and time of the 
measurement value', db_index=True)

class Data(models.Model):
entry = ForeignKey(Entry)
channel = models.CharField(help_text='the channelname', max_length=20, 
db_index=True)
value = models.FloatField(help_text='the measurement value', db_index=True)
unit = models.CharField(help_text='the unit of the measurement value', 
max_length=20, db_index=True)

Then you have one entry element with as many data elements as you like. You 
access it by taking one element object and going through relation, eg:
e = Entry(pk=1)
e.data_set.all()

I don't know if "unique_together" will work with this though


--~--~-~--~~~---~--~~
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: apache

2009-08-04 Thread Spajderix

Hi,

replace

PythonPath "['/home/testpec/public_html/pecwizard/'] + sys.path"

with

PythonPath "['/home/testpec/public_html/'] + sys.path"

and should then work


Salvatore Leone pisze:
> Hi,
>
> I'm trying to move my site from the developemente server to Apache but I 
> always obtain this error:
>
> ImportError: Could not import settings 'pecwizard.settings' (Is it on 
> sys.path? Does it have syntax errors?): No module named pecwizard.settings
>
>
>
> and here is my apache configuration:
>
>
> 
>
> SetHandler python-program
>
> PythonHandler django.core.handlers.modpython
>
> SetEnv DJANGO_SETTINGS_MODULE pecwizard.settings
>
> PythonPath "['/home/testpec/public_html/pecwizard/'] + sys.path"
>
> PythonOption django.root /pecwizard
>
> PythonDebug On
>
> 
>
>
>
> any idea of what could be wrong?
>
> I tryend to put:
>
> SetEnv DJANGO_SETTINGS_MODULE /home/testpec/public_html/pecwizard/settings.py
>
>
> but it says something about I can't set the variable with a full path to 
> the settings.py file
>
> -Salvatore
>
> >
>   


--~--~-~--~~~---~--~~
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: Create a new user profile

2009-08-04 Thread Spajderix

Hi,

you have to create new "User" object, assign values to it and then 
assign this object to profile_obj. Example:

from django.contrib.auth.models import User

new_user = User()
profile_obj = UserProfile()
new_user.username = 'someusername'
new_user.password = 'somepassword'
new_user.save()
profile_obj.user=new_user
profile_obj.save()

Steven Nien pisze:
> Hi all,
> I have built a model like this:
>
> class UserProfile(models.Model):
> address = models.CharField(max_length=60)
> birthday = models.DateField()
> user = models.ForeignKey(User)
>
> I render the model by the ModelForm:
>
> class UserProfileForm(ModelForm):
> class Meta:
> model = UserProfile
> exclude = ('user', )
>
> I am trying to create a "blank" profile for the current user, if the
> profile does not already exist.
> I have tried the following code but it didn't work.
>
> if request.user.is_authenticated():
> username = request.user.username
> try:
> profile = request.user.get_profile()
> except ObjectDoesNotExist:
> profile_obj=UserProfile(user=username)
> profile_obj.save()
>
> which I got a error message:
> Cannot assign "u'someone": "UserProfile.user" must be a "User"
> instance.
>
> Can someone help me?
> Many thanks.
>
> >
>   


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