Re: allow certain users to access certain views

2015-02-18 Thread James Schneider
You should be able to handle this pretty easily with Django's built-in
permission system.

Just create a group, add the users to it, and assign the requisite
permissions to the group. Our add the needed permissions individually to
each user.

https://docs.djangoproject.com/en/1.7/topics/auth/default/#permissions-and-authorization

Then all you'll need to do is wrap your views with the correct permission
checks, or do the checks within the templates if you need to, like {% if
'foo.view' in perms %} assuming that you have the default context
processors in place, or have included the contrib.auth processor.

You shouldn't need to do any permission checks for pages that are available
to everyone.

Alternatively, you can tweak the user.is_staff attribute to True for those
that need extra access, and use that as your permission check if you don't
need anything fancy.

-James
On Feb 18, 2015 9:49 PM, "sum abiut"  wrote:

> Hi,
> I have an app that i am trying to allow only certain users to access
> specific views. For example all users can view the home page but for a
> certain link. i only want to allow the HR manager to access it.
>
> Could you please point me to right direction.
>
> 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/CAPCf-y5VBRtsb2oFN5ZEPL2_57y%2BAhKqV-OnEqne2YAcACNxsw%40mail.gmail.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/CA%2Be%2BciWj2mb4a0Yx1sep8X8MSdcLfNK_L8SJ04E29gkH10gjyA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Sqlite3 to Postgres Migration?

2015-02-18 Thread talex
I have a functioning Django application that is based on sqlite3, 
but I want to change to using Postgres.
I have altered the DATABASES clause in settings.py to use Postgres.
and I am using Django version 1.7.

Am I correct that use of "python makemigrations" followed by
"python migrate", should copy the existing data into Postgres
and leave me with a working application?

Instead what is happening is that "python migrate" gives
the following error and hint:

  django.db.utils.ProgrammingError: column "date" cannot be cast 
automatically to type integer
  HINT:  Specify a USING expression to perform the conversion.

How can I apply this hint to make this work?

My models.py file is as follows:

from django.db import models
import datetime

# Create your models here.

class Location(models.Model):

   class Meta:
  unique_together = ("lat", "lng")

   lat  = models.DecimalField(max_digits=8, decimal_places=5)
   lng  = models.DecimalField(max_digits=8, decimal_places=5)
   name = models.CharField(max_length=200,  unique=True)

   def __str__(self):
  return "%s: %d %d" % (self.name, self.lat, self.lng)

class Observation(models.Model):

   date = models.DateField()
   location = models.ForeignKey(Location)
   observer = models.CharField(max_length=50)
   temperature = models.FloatField(default=0.0)
   photo= models.ImageField(default="tower.jpg", 
upload_to="uploaded_photos")

   def __str__(self):
  return self.observer
---

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/82208bed-4d02-49b5-83b2-f6302fa889b1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


allow certain users to access certain views

2015-02-18 Thread sum abiut
Hi,
I have an app that i am trying to allow only certain users to access
specific views. For example all users can view the home page but for a
certain link. i only want to allow the HR manager to access it.

Could you please point me to right direction.

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/CAPCf-y5VBRtsb2oFN5ZEPL2_57y%2BAhKqV-OnEqne2YAcACNxsw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django 1.7.4, PostgreSQL: Migrate FK to M2M?

2015-02-18 Thread Micky Hulse
Hi Andrew, many thanks for the quick reply and help! :)

On Wed, Feb 18, 2015 at 8:56 PM, Andrew Pinkham  wrote:
> When I had to migrate FK->M2M, I found that hand-hacking a migration file was 
> the way to go. I wrote a multiple operation migration class that did the 
> following:
> ..
> Hope that's helpful,

Yes, very helpful! Thanks for the tips. That gives me a great outline
and starting/ending points to shoot for.

I'll post back my code to the group if all works out and I have
something worth sharing. ;)

Thanks again Andrew!

Cheers,
Micky

-- 
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/CAKzdcNk7TEH-VX%2BMN4nxgtgmBrti%2Bq7CENvVPdKCZTbxR6aT%3DQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django 1.7.4, PostgreSQL: Migrate FK to M2M?

2015-02-18 Thread Andrew Pinkham
Hi Micky,
When I had to migrate FK->M2M, I found that hand-hacking a migration file was 
the way to go. I wrote a multiple operation migration class that did the 
following:

1. create a M2M, nullable field, and set all defaults to null
2. use RunPython to run a function which takes the data from the FK field, and 
connects the objects in the M2M relation (bonus: create a function that does 
the opposite, to make this script totally reversible!)
3. remove the FK field
4. make the M2M field non-null (if desirable)

If you want the fields to be the same name, I think the safest bet is to 
prepend a step to the list above:

0. change name on FK field to temporary name

But I can't remember why I think that's better than changing the M2M field at 
the end.

I would share the code, but I can't find it at the moment. Sorry about that.

Hope that's helpful,
Andrew

-- 
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/11CFD73D-4B02-4B36-AE3C-7A5567D3C2A0%40andrewsforge.com.
For more options, visit https://groups.google.com/d/optout.


Django 1.7.4, PostgreSQL: Migrate FK to M2M?

2015-02-18 Thread Micky Hulse
Hi,

I was just curious if anyone could give me tips on how to migrate an
FK to M2M whist retaining the FK selected data?

Long story short, I've played around with adding adding a M2M field
with a `related_name`, migrating, deleting FK, then re-naming the M2M
and removing `related_name`, but I eventually lose the data towards
the end of the hoop jumping. I think I need to get my hands dirty and
modify the schema migrations.

I've found a few (older) posts detailing the steps using South (for
example, [1]), but I'm using Django's built-in migration tools …

Questions:

Should South tutorials directly relate to Django's schema migrations?

Anyone out there have a step-by-step for Django 1.7.x (using native
schema migrations) that I could follow? :)

Thanks in advance!

Cheers,
M

[1] 


-- 
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/CAKzdcNmfMXTP4S1OYPEXFvnJE30cNhLFvZdPgv2DwHrmafrHdw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: how to handle race conditions?

2015-02-18 Thread Waitman Gobble
On Wed, Feb 18, 2015 at 4:04 PM, Mario Gudelj  wrote:
> Great reply. You're a champion, Carl!
>
> On 19 February 2015 at 05:31, Carl Meyer  wrote:
>>
>> Hi Abraham,
>>
>> On 02/17/2015 10:01 PM, Abraham Varricatt wrote:
>> > I'm trying to make an app where folks can order X quantity of an item.
>> > The condition is that the order should only be made if inventory exists.
>> > Assume that we have stock of Y items. This means that only if Y >= X
>> > should we allow the sale to go through. And once we accept an order, the
>> > inventory should be updated so that Y = Y - X.
>>
>> In general, the construct you need is called a "transaction", which
>> ensures that a series of database operations will either all be
>> committed together, or rolled back if they can't be successfully
>> completed. The Django API for that is "django.db.transaction.atomic".
>>
>> > The way I've currently implemented this is with a pair of getter/setter
>> > methods. I have a model 'ShopItem' which has two fields called
>> > 'quantity' and 'name' (this is unique). I've made a utility class
>> > (outside model source file) where I've made the following 2 functions;
>> >
>> > def get_quantity(name):
>> >   stuff_left = 0
>> >   try:
>> > item = ShopItem.objects.get(name=name)
>> > stuff_left = item.quantity
>> >   except ShopItem.DoesNotExist:
>> > pass # return zero
>> >   return stuff_left
>> >
>> > def set_quantity(name, stuff_left):
>> >   item = ShopItem.objects.get(name=name)
>> >   item.quantity = stuff_left
>> >   item.save()
>> >
>> >
>> > Elsewhere in my project, if I need to display the remaining quantity of
>> > an item in a view, I'll pass the result from get_quantity(). At sale
>> > time here is how I update things;
>> >
>> > def customer_buy(name, number):
>> >   temp = get_quantity(name)
>> >   if (temp >= number):
>> > temp = temp - number
>> > set_quantity(name, temp)
>> > // do other things
>> >   else:
>> > // sale failed, do something else
>> >
>> > I tested this on my system and things appear to work. But I'm concerned
>> > about race conditions. What if two different customers came in to buy
>> > the same item? As in; if I only have 7 items in stock, but one customer
>> > came to buy 6nos and another 5nos. There is a chance (as per my
>> > understanding) that both orders will be accepted - even worse, my
>> > inventory will not accurately reflect the updated situation.
>> >
>> > Any ideas on how this issue can be resolved? I've come across this
>> > -
>> > https://docs.djangoproject.com/en/1.6/ref/models/instances/#updating-attributes-based-on-existing-fields
>> > , but not sure how to apply it to the current scenario.
>>
>> If we're only considering the changes to your ShopItem model, you don't
>> even need an explicit transaction to avoid race conditions, because (as
>> the docs you linked show) the operation can be completed in a single
>> database query, which is inherently atomic. This is how it would look to
>> do it in a single query:
>>
>> from django.db.models import F
>>
>> def customer_buy(name, number):
>> ShopItem.objects.filter(
>> name=name).update(quantity=F('quantity')-number)
>>
>> You want this to fail if someone tries to purchase a larger quantity
>> than are available. The best way to do this is via a database-level
>> "check constraint" on the column, such that the database itself will
>> never permit a negative quantity. If you make 'quantity' a
>> PositiveIntegerField (the name is wrong, it actually allows zero too) on
>> your model, and your database is PostgreSQL (or Oracle), Django will add
>> this constraint for you automatically. Then if someone tries to purchase
>> more than are available, you'll get an IntegrityError, which you'd want
>> to catch and handle in some way:
>>
>> from django.db import IntegrityError
>> from django.db.models import F
>>
>> class InsufficientInventory(Exception):
>> pass
>>
>> def customer_buy(name, number):
>> try:
>> ShopItem.objects.filter(
>> name=name).update(quantity=F('quantity')-number)
>> except IntegrityError:
>> # signal to the calling code that the purchase failed - the
>> # calling code should catch this exception and notify the
>> # user that the purchase failed due to lack of inventory,
>> # and tell them the updated available quantity
>> raise InsufficientInventory()
>>
>> You also want this function to handle the case where the given product
>> name doesn't exist. To help with this case, the `update` method returns
>> the number of rows updated:
>>
>> from django.db import IntegrityError
>> from django.db.models import F
>>
>> class InsufficientInventory(Exception):
>> pass
>>
>> def customer_buy(name, number):
>> try:
>> updated = ShopItem.objects.filter(
>>   

Re: html response to be loaded in a div

2015-02-18 Thread Vijay Khemlani
What is being sent to the view from the browser is fine, it's just being
url encoded, the actual value is something like this then:

[[['5E', '10A', '8D'], ['8B', '11B', '12G'], ['8C', '7B'], ['12C', '11F',
'6A'],['5E', '10G', '10H'],['8A','11E'], ['7A', '12E', '12F'], ['5A',
'11C', '12B','11G'], ['7E', '11A', '6E'], ['10F', '12D'], ['9E','10D',
'12A'], ['6C', '9C','10E'], ['9D', '5C', '8E'], ['7C', '10B'], ['9B',
'6B'], ['6D', '9A'],['11D','5D'], '10C', '5B'], ['7D'], 2800], [['9E',
'11G', '12B', '11C'], ['10H', '9E', '12A'], ['10E','12C', '5D'], ['6E',
11B', '7E', '12F'], ['12E', '10B', '8E','5C'], ['11D', '5B'], ['7A', '9D',
'11E'],['6A', '11A', '7D'], ['9B', '5E','10A'], ['10C', '7C'], ['8A',
'10D'], ['6C', '9A', '12D'], ['12G', '6D'],['10F'],['9C', '10G'],
['11F','6B'], ['8B'], ['8D'], ['7B', '8C'], ['5A'], 2700]]

If that's exactly the values you are sending then you have two problems:

1. You are missing a bracket at the beginning (you need 4 brackets, not 3)

2. You are missing a pair of quotes in the second apparition of / 11B" /

It should be like this

"5E", "10A", "8D"], ["8B", "11B", "12G"], ["8C", "7B"], ["12C", "11F",
"6A"],["5E", "10G", "10H"],["8A","11E"], ["7A", "12E", "12F"], ["5A",
"11C", "12B","11G"], ["7E", "11A", "6E"], ["10F", "12D"], ["9E","10D",
"12A"], ["6C", "9C","10E"], ["9D", "5C", "8E"], ["7C", "10B"], ["9B",
"6B"], ["6D", "9A"],["11D","5D"], "10C", "5B"], ["7D"], 2800], [["9E",
"11G", "12B", "11C"], ["10H", "9E", "12A"], ["10E","12C", "5D"], ["6E",
"11B", "7E", "12F"], ["12E", "10B", "8E","5C"], ["11D", "5B"], ["7A", "9D",
"11E"],["6A", "11A", "7D"], ["9B", "5E","10A"], ["10C", "7C"], ["8A",
"10D"], ["6C", "9A", "12D"], ["12G", "6D"],["10F"],["9C", "10G"],
["11F","6B"], ["8B"], ["8D"], ["7B", "8C"], ["5A"], 2700]]

Or this if you are using it in the url

%5B%5B%5B%5B%225E%22%2C%20%2210A%22%2C%20%228D%22%5D%2C%20%5B%228B%22%2C%20%2211B%22%2C%20%2212G%22%5D%2C%20%5B%228C%22%2C%20%227B%22%5D%2C%20%5B%2212C%22%2C%20%2211F%22%2C%20%226A%22%5D%2C%5B%225E%22%2C%20%2210G%22%2C%20%2210H%22%5D%2C%5B%228A%22%2C%2211E%22%5D%2C%20%5B%227A%22%2C%20%2212E%22%2C%20%2212F%22%5D%2C%20%5B%225A%22%2C%20%2211C%22%2C%20%2212B%22%2C%2211G%22%5D%2C%20%5B%227E%22%2C%20%2211A%22%2C%20%226E%22%5D%2C%20%5B%2210F%22%2C%20%2212D%22%5D%2C%20%5B%229E%22%2C%2210D%22%2C%20%2212A%22%5D%2C%20%5B%226C%22%2C%20%229C%22%2C%2210E%22%5D%2C%20%5B%229D%22%2C%20%225C%22%2C%20%228E%22%5D%2C%20%5B%227C%22%2C%20%2210B%22%5D%2C%20%5B%229B%22%2C%20%226B%22%5D%2C%20%5B%226D%22%2C%20%229A%22%5D%2C%5B%2211D%22%2C%225D%22%5D%2C%20%2210C%22%2C%20%225B%22%5D%2C%20%5B%227D%22%5D%2C%202800%5D%2C%20%5B%5B%229E%22%2C%20%2211G%22%2C%20%2212B%22%2C%20%2211C%22%5D%2C%20%5B%2210H%22%2C%20%229E%22%2C%20%2212A%22%5D%2C%20%5B%2210E%22%2C%2212C%22%2C%20%225D%22%5D%2C%20%5B%226E%22%2C%20%2211B%22%2C%20%227E%22%2C%20%2212F%22%5D%2C%20%5B%2212E%22%2C%20%2210B%22%2C%20%228E%22%2C%225C%22%5D%2C%20%5B%2211D%22%2C%20%225B%22%5D%2C%20%5B%227A%22%2C%20%229D%22%2C%20%2211E%22%5D%2C%5B%226A%22%2C%20%2211A%22%2C%20%227D%22%5D%2C%20%5B%229B%22%2C%20%225E%22%2C%2210A%22%5D%2C%20%5B%2210C%22%2C%20%227C%22%5D%2C%20%5B%228A%22%2C%20%2210D%22%5D%2C%20%5B%226C%22%2C%20%229A%22%2C%20%2212D%22%5D%2C%20%5B%2212G%22%2C%20%226D%22%5D%2C%5B%2210F%22%5D%2C%5B%229C%22%2C%20%2210G%22%5D%2C%20%5B%2211F%22%2C%226B%22%5D%2C%20%5B%228B%22%5D%2C%20%5B%228D%22%5D%2C%20%5B%227B%22%2C%20%228C%22%5D%2C%20%5B%225A%22%5D%2C%202700%5D%5D

On Wed, Feb 18, 2015 at 8:58 PM, Mario Gudelj 
wrote:

> You may need to use | safe filter in your tag since your output is being
> encoded.
>
> E.g.
>
> {{output|safe}}
>
> Cheers,
>
> M
>
> On 19 February 2015 at 07:37, João Marques 
> wrote:
>
>> This is what is being sent to the view:
>>
>>
>> 

Re: Modify Installed_Apps settings

2015-02-18 Thread Andrew Pinkham
Thanks for the info, Joel.

However, I'm trying to avoid creating a separate settings file. Any info about 
how to properly use modify_settings to add to installed apps or how to force 
the test runner to apply a specific migration would still be appreciated.

Thanks,
Andrew

-- 
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/53B86B68-0B7B-4121-9F7A-8F482BB9FCE7%40andrewsforge.com.
For more options, visit https://groups.google.com/d/optout.


Re: how to handle race conditions?

2015-02-18 Thread Mario Gudelj
Great reply. You're a champion, Carl!

On 19 February 2015 at 05:31, Carl Meyer  wrote:

> Hi Abraham,
>
> On 02/17/2015 10:01 PM, Abraham Varricatt wrote:
> > I'm trying to make an app where folks can order X quantity of an item.
> > The condition is that the order should only be made if inventory exists.
> > Assume that we have stock of Y items. This means that only if Y >= X
> > should we allow the sale to go through. And once we accept an order, the
> > inventory should be updated so that Y = Y - X.
>
> In general, the construct you need is called a "transaction", which
> ensures that a series of database operations will either all be
> committed together, or rolled back if they can't be successfully
> completed. The Django API for that is "django.db.transaction.atomic".
>
> > The way I've currently implemented this is with a pair of getter/setter
> > methods. I have a model 'ShopItem' which has two fields called
> > 'quantity' and 'name' (this is unique). I've made a utility class
> > (outside model source file) where I've made the following 2 functions;
> >
> > def get_quantity(name):
> >   stuff_left = 0
> >   try:
> > item = ShopItem.objects.get(name=name)
> > stuff_left = item.quantity
> >   except ShopItem.DoesNotExist:
> > pass # return zero
> >   return stuff_left
> >
> > def set_quantity(name, stuff_left):
> >   item = ShopItem.objects.get(name=name)
> >   item.quantity = stuff_left
> >   item.save()
> >
> >
> > Elsewhere in my project, if I need to display the remaining quantity of
> > an item in a view, I'll pass the result from get_quantity(). At sale
> > time here is how I update things;
> >
> > def customer_buy(name, number):
> >   temp = get_quantity(name)
> >   if (temp >= number):
> > temp = temp - number
> > set_quantity(name, temp)
> > // do other things
> >   else:
> > // sale failed, do something else
> >
> > I tested this on my system and things appear to work. But I'm concerned
> > about race conditions. What if two different customers came in to buy
> > the same item? As in; if I only have 7 items in stock, but one customer
> > came to buy 6nos and another 5nos. There is a chance (as per my
> > understanding) that both orders will be accepted - even worse, my
> > inventory will not accurately reflect the updated situation.
> >
> > Any ideas on how this issue can be resolved? I've come across this
> > -
> https://docs.djangoproject.com/en/1.6/ref/models/instances/#updating-attributes-based-on-existing-fields
> > , but not sure how to apply it to the current scenario.
>
> If we're only considering the changes to your ShopItem model, you don't
> even need an explicit transaction to avoid race conditions, because (as
> the docs you linked show) the operation can be completed in a single
> database query, which is inherently atomic. This is how it would look to
> do it in a single query:
>
> from django.db.models import F
>
> def customer_buy(name, number):
> ShopItem.objects.filter(
> name=name).update(quantity=F('quantity')-number)
>
> You want this to fail if someone tries to purchase a larger quantity
> than are available. The best way to do this is via a database-level
> "check constraint" on the column, such that the database itself will
> never permit a negative quantity. If you make 'quantity' a
> PositiveIntegerField (the name is wrong, it actually allows zero too) on
> your model, and your database is PostgreSQL (or Oracle), Django will add
> this constraint for you automatically. Then if someone tries to purchase
> more than are available, you'll get an IntegrityError, which you'd want
> to catch and handle in some way:
>
> from django.db import IntegrityError
> from django.db.models import F
>
> class InsufficientInventory(Exception):
> pass
>
> def customer_buy(name, number):
> try:
> ShopItem.objects.filter(
> name=name).update(quantity=F('quantity')-number)
> except IntegrityError:
> # signal to the calling code that the purchase failed - the
> # calling code should catch this exception and notify the
> # user that the purchase failed due to lack of inventory,
> # and tell them the updated available quantity
> raise InsufficientInventory()
>
> You also want this function to handle the case where the given product
> name doesn't exist. To help with this case, the `update` method returns
> the number of rows updated:
>
> from django.db import IntegrityError
> from django.db.models import F
>
> class InsufficientInventory(Exception):
> pass
>
> def customer_buy(name, number):
> try:
> updated = ShopItem.objects.filter(
> name=name).update(quantity=F('quantity')-number)
> except IntegrityError:
> raise InsufficientInventory()
> if not updated:
> # Here we reuse Django's 

Re: html response to be loaded in a div

2015-02-18 Thread Mario Gudelj
You may need to use | safe filter in your tag since your output is being
encoded.

E.g.

{{output|safe}}

Cheers,

M

On 19 February 2015 at 07:37, João Marques 
wrote:

> This is what is being sent to the view:
>
>
> %5B%5B%5B%275E%27,%20%2710A%27,%20%278D%27%5D,%20%5B%278B%27,%20%2711B%27,%20%2712G%27%5D,%20%5B%278C%27,%20%277B%27%5D,%20%5B%2712C%27,%20%2711F%27,%20%276A%27%5D,%5B%275E%27,%20%2710G%27,%20%2710H%27%5D,%5B%278A%27,%2711E%27%5D,%20%5B%277A%27,%20%2712E%27,%20%2712F%27%5D,%20%5B%275A%27,%20%2711C%27,%20%2712B%27,%2711G%27%5D,%20%5B%277E%27,%20%2711A%27,%20%276E%27%5D,%20%5B%2710F%27,%20%2712D%27%5D,%20%5B%279E%27,%2710D%27,%20%2712A%27%5D,%20%5B%276C%27,%20%279C%27,%2710E%27%5D,%20%5B%279D%27,%20%275C%27,%20%278E%27%5D,%20%5B%277C%27,%20%2710B%27%5D,%20%5B%279B%27,%20%276B%27%5D,%20%5B%276D%27,%20%279A%27%5D,%5B%2711D%27,%275D%27%5D,%20%2710C%27,%20%275B%27%5D,%20%5B%277D%27%5D,%202800%5D,%20%5B%5B%279E%27,%20%2711G%27,%20%2712B%27,%20%2711C%27%5D,%20%5B%2710H%27,%20%279E%27,%20%2712A%27%5D,%20%5B%2710E%27,%2712C%27,%20%275D%27%5D,%20%5B%276E%27,%2011B%27,%20%277E%27,%20%2712F%27%5D,%20%5B%2712E%27,%20%2710B%27,%20%278E%27,%275C%27%5D,%20%5B%2711D%27,%20%275B%27%5D,%20%5B%277A%27,%20%279D%27,%20%2711E%27%5D,%5B%276A%27,%20%2711A%27,%20%277D%27%5D,%20%5B%279B%27,%20%275E%27,%2710A%27%5D,%20%5B%2710C%27,%20%277C%27%5D,%20%5B%278A%27,%20%2710D%27%5D,%20%5B%276C%27,%20%279A%27,%20%2712D%27%5D,%20%5B%2712G%27,%20%276D%27%5D,%5B%2710F%27%5D,%5B%279C%27,%20%2710G%27%5D,%20%5B%2711F%27,%276B%27%5D,%20%5B%278B%27%5D,%20%5B%278D%27%5D,%20%5B%277B%27,%20%278C%27%5D,%20%5B%275A%27%5D,%202700%5D%5D
>
> instead of:
>
> [[['5E', '10A', '8D'], ['8B', '11B', '12G'], ['8C', '7B'], ['12C', '11F',
> '6A'],['5E', '10G', '10H'],['8A','11E'], ['7A', '12E', '12F'], ['5A',
> '11C', '12B','11G'], ['7E', '11A', '6E'], ['10F', '12D'], ['9E','10D',
> '12A'], ['6C', '9C','10E'], ['9D', '5C', '8E'], ['7C', '10B'], ['9B',
> '6B'], ['6D', '9A'],['11D','5D'], '10C', '5B'], ['7D'], 2800], [['9E',
> '11G', '12B', '11C'], ['10H', '9E', '12A'], ['10E','12C', '5D'], ['6E',
> 11B', '7E', '12F'], ['12E', '10B', '8E','5C'], ['11D', '5B'], ['7A', '9D',
> '11E'],['6A', '11A', '7D'], ['9B', '5E','10A'], ['10C', '7C'], ['8A',
> '10D'], ['6C', '9A', '12D'], ['12G', '6D'],['10F'],['9C', '10G'],
> ['11F','6B'], ['8B'], ['8D'], ['7B', '8C'], ['5A'], 2700]]
>
>
> Altough when I reach the view, the content that is loaded to the db is the
> right one
>
>  --
> 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/158965f3-09d9-4bfe-a66a-1f353698bffd%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/CAHqTbj%3DNUz%2Bzr_LGs5kG4KrRRWyFfotWJ%3D5r2m3_WmHkxM8tOw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Modify Installed_Apps settings

2015-02-18 Thread Joel Goldstick
On Wed, Feb 18, 2015 at 2:03 PM, Andrew Pinkham  wrote:
> I have an app in Django 1.7.4 that I would like to use purely for testing.
>
> $ ./manage.py startapp override
>
> The app contains its own model, view, and URL pattern (the model overrides a 
> model in another app, allowing me to test behavior in a specific case).
>
> For the moment, I have the app in INSTALLED_APPS, and the tests run fine. 
> However, I would like to remove the app from INSTALLED_APPS, and override the 
> setting just for the tests. In theory:
>
> @modify_settings(INSTALLED_APPS={'append': 'override'})
>
> However, if I then remove the string from project_name/settings.py, I am 
> given a Python Traceback which ends in:
>
> django.db.utils.OperationalError: no such table: override_modelname
>
> How may I force the TestCase class to apply the migration file 
> override/migrations/0001_initial.py to the test database?
>
> Thanks,
> Andrew
>
> --
In the book "Two Scoops of Django" there is a section about setting up
different settings files.  That may help you out

http://twoscoopspress.org/pages/two-scoops-of-django-1-6-faq
-- 
Joel Goldstick
http://joelgoldstick.com

-- 
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/CAPM-O%2ByxF1%2BxJe%3D9eeCY-CJAKBjZH9vKzfBsYxrCfhyGPktWVQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: html response to be loaded in a div

2015-02-18 Thread Thomas Rega
Hi,

may be that this example(s) help:
https://godjango.com/blog/working-with-json-and-django/

good luck

2015-02-18 21:37 GMT+01:00 João Marques :
> This is what is being sent to the view:
>
> %5B%5B%5B%275E%27,%20%2710A%27,%20%278D%27%5D,%20%5B%278B%27,%20%2711B%27,%20%2712G%27%5D,%20%5B%278C%27,%20%277B%27%5D,%20%5B%2712C%27,%20%2711F%27,%20%276A%27%5D,%5B%275E%27,%20%2710G%27,%20%2710H%27%5D,%5B%278A%27,%2711E%27%5D,%20%5B%277A%27,%20%2712E%27,%20%2712F%27%5D,%20%5B%275A%27,%20%2711C%27,%20%2712B%27,%2711G%27%5D,%20%5B%277E%27,%20%2711A%27,%20%276E%27%5D,%20%5B%2710F%27,%20%2712D%27%5D,%20%5B%279E%27,%2710D%27,%20%2712A%27%5D,%20%5B%276C%27,%20%279C%27,%2710E%27%5D,%20%5B%279D%27,%20%275C%27,%20%278E%27%5D,%20%5B%277C%27,%20%2710B%27%5D,%20%5B%279B%27,%20%276B%27%5D,%20%5B%276D%27,%20%279A%27%5D,%5B%2711D%27,%275D%27%5D,%20%2710C%27,%20%275B%27%5D,%20%5B%277D%27%5D,%202800%5D,%20%5B%5B%279E%27,%20%2711G%27,%20%2712B%27,%20%2711C%27%5D,%20%5B%2710H%27,%20%279E%27,%20%2712A%27%5D,%20%5B%2710E%27,%2712C%27,%20%275D%27%5D,%20%5B%276E%27,%2011B%27,%20%277E%27,%20%2712F%27%5D,%20%5B%2712E%27,%20%2710B%27,%20%278E%27,%275C%27%5D,%20%5B%2711D%27,%20%275B%27%5D,%20%5B%277A%27,%20%279D%27,%20%2711E%27%5D,%5B%276A%27,%20%2711A%27,%20%277D%27%5D,%20%5B%279B%27,%20%275E%27,%2710A%27%5D,%20%5B%2710C%27,%20%277C%27%5D,%20%5B%278A%27,%20%2710D%27%5D,%20%5B%276C%27,%20%279A%27,%20%2712D%27%5D,%20%5B%2712G%27,%20%276D%27%5D,%5B%2710F%27%5D,%5B%279C%27,%20%2710G%27%5D,%20%5B%2711F%27,%276B%27%5D,%20%5B%278B%27%5D,%20%5B%278D%27%5D,%20%5B%277B%27,%20%278C%27%5D,%20%5B%275A%27%5D,%202700%5D%5D
>
> instead of:
>
> [[['5E', '10A', '8D'], ['8B', '11B', '12G'], ['8C', '7B'], ['12C', '11F',
> '6A'],['5E', '10G', '10H'],['8A','11E'], ['7A', '12E', '12F'], ['5A', '11C',
> '12B','11G'], ['7E', '11A', '6E'], ['10F', '12D'], ['9E','10D', '12A'],
> ['6C', '9C','10E'], ['9D', '5C', '8E'], ['7C', '10B'], ['9B', '6B'], ['6D',
> '9A'],['11D','5D'], '10C', '5B'], ['7D'], 2800], [['9E', '11G', '12B',
> '11C'], ['10H', '9E', '12A'], ['10E','12C', '5D'], ['6E', 11B', '7E',
> '12F'], ['12E', '10B', '8E','5C'], ['11D', '5B'], ['7A', '9D', '11E'],['6A',
> '11A', '7D'], ['9B', '5E','10A'], ['10C', '7C'], ['8A', '10D'], ['6C', '9A',
> '12D'], ['12G', '6D'],['10F'],['9C', '10G'], ['11F','6B'], ['8B'], ['8D'],
> ['7B', '8C'], ['5A'], 2700]]
>
>
> Altough when I reach the view, the content that is loaded to the db is the
> right one
>
> --
> 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/158965f3-09d9-4bfe-a66a-1f353698bffd%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/CAFgu0xiT8kBQvhRiChnEoxwT3wb%2B2NJnCBW%2BFNEnCB31zxjWmg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: html response to be loaded in a div

2015-02-18 Thread João Marques
This is what is being sent to the view:

%5B%5B%5B%275E%27,%20%2710A%27,%20%278D%27%5D,%20%5B%278B%27,%20%2711B%27,%20%2712G%27%5D,%20%5B%278C%27,%20%277B%27%5D,%20%5B%2712C%27,%20%2711F%27,%20%276A%27%5D,%5B%275E%27,%20%2710G%27,%20%2710H%27%5D,%5B%278A%27,%2711E%27%5D,%20%5B%277A%27,%20%2712E%27,%20%2712F%27%5D,%20%5B%275A%27,%20%2711C%27,%20%2712B%27,%2711G%27%5D,%20%5B%277E%27,%20%2711A%27,%20%276E%27%5D,%20%5B%2710F%27,%20%2712D%27%5D,%20%5B%279E%27,%2710D%27,%20%2712A%27%5D,%20%5B%276C%27,%20%279C%27,%2710E%27%5D,%20%5B%279D%27,%20%275C%27,%20%278E%27%5D,%20%5B%277C%27,%20%2710B%27%5D,%20%5B%279B%27,%20%276B%27%5D,%20%5B%276D%27,%20%279A%27%5D,%5B%2711D%27,%275D%27%5D,%20%2710C%27,%20%275B%27%5D,%20%5B%277D%27%5D,%202800%5D,%20%5B%5B%279E%27,%20%2711G%27,%20%2712B%27,%20%2711C%27%5D,%20%5B%2710H%27,%20%279E%27,%20%2712A%27%5D,%20%5B%2710E%27,%2712C%27,%20%275D%27%5D,%20%5B%276E%27,%2011B%27,%20%277E%27,%20%2712F%27%5D,%20%5B%2712E%27,%20%2710B%27,%20%278E%27,%275C%27%5D,%20%5B%2711D%27,%20%275B%27%5D,%20%5B%277A%27,%20%279D%27,%20%2711E%27%5D,%5B%276A%27,%20%2711A%27,%20%277D%27%5D,%20%5B%279B%27,%20%275E%27,%2710A%27%5D,%20%5B%2710C%27,%20%277C%27%5D,%20%5B%278A%27,%20%2710D%27%5D,%20%5B%276C%27,%20%279A%27,%20%2712D%27%5D,%20%5B%2712G%27,%20%276D%27%5D,%5B%2710F%27%5D,%5B%279C%27,%20%2710G%27%5D,%20%5B%2711F%27,%276B%27%5D,%20%5B%278B%27%5D,%20%5B%278D%27%5D,%20%5B%277B%27,%20%278C%27%5D,%20%5B%275A%27%5D,%202700%5D%5D

instead of:

[[['5E', '10A', '8D'], ['8B', '11B', '12G'], ['8C', '7B'], ['12C', '11F', 
'6A'],['5E', '10G', '10H'],['8A','11E'], ['7A', '12E', '12F'], ['5A', 
'11C', '12B','11G'], ['7E', '11A', '6E'], ['10F', '12D'], ['9E','10D', 
'12A'], ['6C', '9C','10E'], ['9D', '5C', '8E'], ['7C', '10B'], ['9B', 
'6B'], ['6D', '9A'],['11D','5D'], '10C', '5B'], ['7D'], 2800], [['9E', 
'11G', '12B', '11C'], ['10H', '9E', '12A'], ['10E','12C', '5D'], ['6E', 
11B', '7E', '12F'], ['12E', '10B', '8E','5C'], ['11D', '5B'], ['7A', '9D', 
'11E'],['6A', '11A', '7D'], ['9B', '5E','10A'], ['10C', '7C'], ['8A', 
'10D'], ['6C', '9A', '12D'], ['12G', '6D'],['10F'],['9C', '10G'], 
['11F','6B'], ['8B'], ['8D'], ['7B', '8C'], ['5A'], 2700]]


Altough when I reach the view, the content that is loaded to the db is the 
right one

-- 
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/158965f3-09d9-4bfe-a66a-1f353698bffd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: html response to be loaded in a div

2015-02-18 Thread João Marques
Thank you so much for all the answers so far. I must say that I totally 
failed not checking the method that both of you guys presented (just 
acessing the GET url on the browser). 

I actually gives me an *error*:

ValueError(errmsg("Expecting value", s, err.value)) from None

I've searched and it seems to be my json that is not well built, altough 
the content is being loaded sucessfully to the database.


quarta-feira, 18 de Fevereiro de 2015 às 18:07:01 UTC, Tom Evans escreveu:
>
> On Wed, Feb 18, 2015 at 1:27 PM, João Marques  > wrote: 
> > Hey guys, so basicly I want to send a GET request with ajax to one of my 
> > views and the specific view returns an html response that will be loaded 
> on 
> > a div. 
> > The problem is that is doesn't seems to work at all. Nothing happers on 
> the 
> > div. 
> > 
> > Please help me community! Thank you! 
>
> How have you debugged it so far? 
>
> > 
> > 
> > HTML to be filled with the response: 
> > 
> >  
> > 
> > 
> > GET REQUEST: 
> > 
> >  $.ajax({ 
> > type: 'GET', 
> > url: 'saveToDbAndReturn', 
> > data: {sols: holder} 
> > }).done(function (response) { 
> >document.getElementById("content").innerHTML = 
> response; 
> > 
> > }); 
>
> This is a GET request. It should be trivial to open the URL in a 
> browser, no AJAX. Does it render the correct content? If it doesn't, 
> fix that first. 
>
> Next, what jumps out is the URL. You've specified 'saveToDbAndReturn', 
> which is a relative URL. Javascript will evaluate that relative to the 
> current page, so if the current page is "/foo/bar/", it will attempt 
> to open "/foo/bar/saveToDbAndReturn'. 
>
> Is that the right URL? 
> What response status code do you get from the webserver for this AJAX 
> request? Use Web Inspector/firebug to determine. 
>
> PS: this is jquery? 
>
> $('#content').load('saveToDbAndReturn', {sols: holder}) 
>
> PPS: 
>
> Views which modify data should not be GET requests. RFC2616 (HTTP) and 
> django's own docs: 
>
> In particular, the convention has been established that the GET and 
> HEAD methods SHOULD NOT have the significance of taking an action 
> other than retrieval. These methods ought to be considered "safe". 
> This allows user agents to represent other methods, such as POST, PUT 
> and DELETE, in a special way, so that the user is made aware of the 
> fact that a possibly unsafe action is being requested. 
>
> Ideally, views that modify data should not present data either. If 
> that behaviour is desired, on a successful data modification the view 
> can redirect to another url to render the newly stored information. 
>
> > 
> > 
> > 
> > VIEW: 
> > 
> > def saveToDbAndReturn(request): 
> > 
> > if(request.GET.get('sols', ''))=='': 
> > return HttpResponseRedirect("/") 
> > else: 
> > sols = json.loads(request.GET.get('sols', '')) 
>
> Arbitrarily loading user input as JSON might not be wise... 
>
> > 
> > 
> > for i in range(0, len(sols)): 
> > 
> > new_entry = Solution(fullArray=sols[i]) 
> > new_entry.save() 
>
> Ugly and inefficient. No need to count things, no need for multiple 
> queries: 
>
>   Solution.objects.bulk_create([ 
>   Solution(fullArray=datum) 
>   for datum in sols 
>   ]) 
>
> > 
> > return render_to_response('saveToDbAndReturn.html', {'sols': sols}) 
> > 
> > 
> > saveToDbAndReturn.html 
> > 
> >  
> > 
> > {% for i in range(sols) %} 
>
> PPPS: 
>
> You can't do that in templates. However, within any {% for %} loop, 
> django is already counting. Do this instead: 
>
> {% for elem in sols %} 
>  
>
> > 
> >  
> >
> >  > href="#collapse{{i}}" aria-expanded="true" 
> aria-controls="collapse{{i}}"> 
> > {{i}} 
> >  
> >
> >  
> >  > role="tabpanel" aria-labelledby="heading{{i}}"> 
> >
> > {{sols[i]}} 
>
> S: 
>
> This is not how to access the i-th member of an array in django 
> templates. In fact, there is not a way to do that without using a 
> custom tag. However, if you rewrite your loop logic as suggested in 
> the PPPS, you don't need to do that, just "{{ elem }}" 
>
> Cheers 
>
> Tom 
>

-- 
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/991795c8-991d-4344-bd3d-b8f43d1d2293%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Release announcements?

2015-02-18 Thread Markus Holtermann
Hey Anton,

You can find the release roadmap 
at https://code.djangoproject.com/wiki/Version1.8Roadmap

The current plan is to issue the beta on Friday, given that all current 
release blockers are fixed. There is a topic about the 1.8 release blockers 
on the django-developers mailing 
list: https://groups.google.com/forum/#!topic/django-developers/74_MZK27TAE

There is also the django-announce mailing list that just mentions all 
releases: https://groups.google.com/forum/#!forum/django-announce . The 
link to the RSS of our blog is https://www.djangoproject.com/rss/weblog/ 
which will also mention the beta release.

/Markus

On Wednesday, February 18, 2015 at 7:00:50 PM UTC+1, Anton Melser wrote:
>
> Hi,
>
> I have started a new project on 1.8 and am looking for an RSS or mailing 
> list that has release announcements for Django proper - is there anything? 
> My understanding was that the first Beta was supposed to come out on the 16 
> Feb, is that right?
>
> Thanks!
> Anton
>

-- 
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/d58b2d51-a5ca-4368-a3fe-50df55c46048%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Release announcements?

2015-02-18 Thread Anton Melser
Hi,

I have started a new project on 1.8 and am looking for an RSS or mailing 
list that has release announcements for Django proper - is there anything? 
My understanding was that the first Beta was supposed to come out on the 16 
Feb, is that right?

Thanks!
Anton

-- 
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/1fd9766f-56f4-4661-af3f-89f3f27a1cc4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Modify Installed_Apps settings

2015-02-18 Thread Andrew Pinkham
I have an app in Django 1.7.4 that I would like to use purely for testing.

$ ./manage.py startapp override

The app contains its own model, view, and URL pattern (the model overrides a 
model in another app, allowing me to test behavior in a specific case).

For the moment, I have the app in INSTALLED_APPS, and the tests run fine. 
However, I would like to remove the app from INSTALLED_APPS, and override the 
setting just for the tests. In theory:

@modify_settings(INSTALLED_APPS={'append': 'override'})

However, if I then remove the string from project_name/settings.py, I am given 
a Python Traceback which ends in:

django.db.utils.OperationalError: no such table: override_modelname

How may I force the TestCase class to apply the migration file 
override/migrations/0001_initial.py to the test database? 

Thanks,
Andrew

-- 
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/3A0C68F2-C408-4ECA-B2B3-CD309C680C10%40andrewsforge.com.
For more options, visit https://groups.google.com/d/optout.


Re: how to handle race conditions?

2015-02-18 Thread Carl Meyer
Hi Abraham,

On 02/17/2015 10:01 PM, Abraham Varricatt wrote:
> I'm trying to make an app where folks can order X quantity of an item.
> The condition is that the order should only be made if inventory exists.
> Assume that we have stock of Y items. This means that only if Y >= X
> should we allow the sale to go through. And once we accept an order, the
> inventory should be updated so that Y = Y - X. 

In general, the construct you need is called a "transaction", which
ensures that a series of database operations will either all be
committed together, or rolled back if they can't be successfully
completed. The Django API for that is "django.db.transaction.atomic".

> The way I've currently implemented this is with a pair of getter/setter
> methods. I have a model 'ShopItem' which has two fields called
> 'quantity' and 'name' (this is unique). I've made a utility class
> (outside model source file) where I've made the following 2 functions;
> 
> def get_quantity(name):
>   stuff_left = 0
>   try:
> item = ShopItem.objects.get(name=name)
> stuff_left = item.quantity
>   except ShopItem.DoesNotExist:
> pass # return zero
>   return stuff_left
> 
> def set_quantity(name, stuff_left):
>   item = ShopItem.objects.get(name=name)
>   item.quantity = stuff_left
>   item.save()
> 
> 
> Elsewhere in my project, if I need to display the remaining quantity of
> an item in a view, I'll pass the result from get_quantity(). At sale
> time here is how I update things;
> 
> def customer_buy(name, number):
>   temp = get_quantity(name)
>   if (temp >= number):
> temp = temp - number
> set_quantity(name, temp)
> // do other things
>   else:
> // sale failed, do something else
> 
> I tested this on my system and things appear to work. But I'm concerned
> about race conditions. What if two different customers came in to buy
> the same item? As in; if I only have 7 items in stock, but one customer
> came to buy 6nos and another 5nos. There is a chance (as per my
> understanding) that both orders will be accepted - even worse, my
> inventory will not accurately reflect the updated situation. 
> 
> Any ideas on how this issue can be resolved? I've come across this
> - 
> https://docs.djangoproject.com/en/1.6/ref/models/instances/#updating-attributes-based-on-existing-fields
> , but not sure how to apply it to the current scenario. 

If we're only considering the changes to your ShopItem model, you don't
even need an explicit transaction to avoid race conditions, because (as
the docs you linked show) the operation can be completed in a single
database query, which is inherently atomic. This is how it would look to
do it in a single query:

from django.db.models import F

def customer_buy(name, number):
ShopItem.objects.filter(
name=name).update(quantity=F('quantity')-number)

You want this to fail if someone tries to purchase a larger quantity
than are available. The best way to do this is via a database-level
"check constraint" on the column, such that the database itself will
never permit a negative quantity. If you make 'quantity' a
PositiveIntegerField (the name is wrong, it actually allows zero too) on
your model, and your database is PostgreSQL (or Oracle), Django will add
this constraint for you automatically. Then if someone tries to purchase
more than are available, you'll get an IntegrityError, which you'd want
to catch and handle in some way:

from django.db import IntegrityError
from django.db.models import F

class InsufficientInventory(Exception):
pass

def customer_buy(name, number):
try:
ShopItem.objects.filter(
name=name).update(quantity=F('quantity')-number)
except IntegrityError:
# signal to the calling code that the purchase failed - the
# calling code should catch this exception and notify the
# user that the purchase failed due to lack of inventory,
# and tell them the updated available quantity
raise InsufficientInventory()

You also want this function to handle the case where the given product
name doesn't exist. To help with this case, the `update` method returns
the number of rows updated:

from django.db import IntegrityError
from django.db.models import F

class InsufficientInventory(Exception):
pass

def customer_buy(name, number):
try:
updated = ShopItem.objects.filter(
name=name).update(quantity=F('quantity')-number)
except IntegrityError:
raise InsufficientInventory()
if not updated:
# Here we reuse Django's built-in DoesNotExist exception;
# you could define your own exception class instead.
raise ShopItem.DoesNotExist()

With this code, you've solved the bad race conditions -- quantity will
never go negative, and a sale will never appear to succeed when it
should have failed, because of two 

Re: html response to be loaded in a div

2015-02-18 Thread Tom Evans
On Wed, Feb 18, 2015 at 1:27 PM, João Marques  wrote:
> Hey guys, so basicly I want to send a GET request with ajax to one of my
> views and the specific view returns an html response that will be loaded on
> a div.
> The problem is that is doesn't seems to work at all. Nothing happers on the
> div.
>
> Please help me community! Thank you!

How have you debugged it so far?

>
>
> HTML to be filled with the response:
>
> 
>
>
> GET REQUEST:
>
>  $.ajax({
> type: 'GET',
> url: 'saveToDbAndReturn',
> data: {sols: holder}
> }).done(function (response) {
>document.getElementById("content").innerHTML = response;
>
> });

This is a GET request. It should be trivial to open the URL in a
browser, no AJAX. Does it render the correct content? If it doesn't,
fix that first.

Next, what jumps out is the URL. You've specified 'saveToDbAndReturn',
which is a relative URL. Javascript will evaluate that relative to the
current page, so if the current page is "/foo/bar/", it will attempt
to open "/foo/bar/saveToDbAndReturn'.

Is that the right URL?
What response status code do you get from the webserver for this AJAX
request? Use Web Inspector/firebug to determine.

PS: this is jquery?

$('#content').load('saveToDbAndReturn', {sols: holder})

PPS:

Views which modify data should not be GET requests. RFC2616 (HTTP) and
django's own docs:

In particular, the convention has been established that the GET and
HEAD methods SHOULD NOT have the significance of taking an action
other than retrieval. These methods ought to be considered "safe".
This allows user agents to represent other methods, such as POST, PUT
and DELETE, in a special way, so that the user is made aware of the
fact that a possibly unsafe action is being requested.

Ideally, views that modify data should not present data either. If
that behaviour is desired, on a successful data modification the view
can redirect to another url to render the newly stored information.

>
>
>
> VIEW:
>
> def saveToDbAndReturn(request):
>
> if(request.GET.get('sols', ''))=='':
> return HttpResponseRedirect("/")
> else:
> sols = json.loads(request.GET.get('sols', ''))

Arbitrarily loading user input as JSON might not be wise...

>
>
> for i in range(0, len(sols)):
>
> new_entry = Solution(fullArray=sols[i])
> new_entry.save()

Ugly and inefficient. No need to count things, no need for multiple queries:

  Solution.objects.bulk_create([
  Solution(fullArray=datum)
  for datum in sols
  ])

>
> return render_to_response('saveToDbAndReturn.html', {'sols': sols})
>
>
> saveToDbAndReturn.html
>
> 
>
> {% for i in range(sols) %}

PPPS:

You can't do that in templates. However, within any {% for %} loop,
django is already counting. Do this instead:

{% for elem in sols %}


>
> 
>   
>  href="#collapse{{i}}" aria-expanded="true" aria-controls="collapse{{i}}">
> {{i}}
> 
>   
> 
>  role="tabpanel" aria-labelledby="heading{{i}}">
>   
> {{sols[i]}}

S:

This is not how to access the i-th member of an array in django
templates. In fact, there is not a way to do that without using a
custom tag. However, if you rewrite your loop logic as suggested in
the PPPS, you don't need to do that, just "{{ elem }}"

Cheers

Tom

-- 
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/CAFHbX1Jvp%2BepEhhgyZHkcVuWffkwJ5vur3KDMxrm-ZdYfWCCFg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: html response to be loaded in a div

2015-02-18 Thread Vijay Khemlani
When you access the AJAX resource directly from your browser (
http://127.0.0.1:8000//saveToDbAndReturn?sols=) are you also
getting a blank page?

Also try putting a print statement or debugging poing (using pdb) at the
beginning of the controller and check if it is actually being called.

On Wed, Feb 18, 2015 at 1:47 PM, João Marques 
wrote:

> Im getting nothing really... I just do alert(response); and nothing shows
> up.
>
> quarta-feira, 18 de Fevereiro de 2015 às 14:48:48 UTC, Vijay Khemlani
> escreveu:
>>
>> What is the actual content of the response you are getting from the AJAX
>> request?
>>
>> On Wed, Feb 18, 2015 at 10:27 AM, João Marques 
>> wrote:
>>
>>> Hey guys, so basicly I want to send a GET request with ajax to one of my
>>> views and the specific view returns an html response that will be loaded on
>>> a div.
>>> The problem is that is doesn't seems to work at all. Nothing happers on
>>> the div.
>>>
>>> Please help me community! Thank you!
>>>
>>>
>>> *HTML to be filled with the response:*
>>>
>>> 
>>>
>>>
>>> *GET REQUEST:*
>>>
>>>  $.ajax({
>>> type: 'GET',
>>> url: 'saveToDbAndReturn',
>>> data: {sols: holder}
>>> }).done(function (response) {
>>>document.getElementById("content").innerHTML =
>>> response;
>>>
>>> });
>>>
>>>
>>>
>>> *VIEW:*
>>>
>>> def saveToDbAndReturn(request):
>>>
>>> if(request.GET.get('sols', ''))=='':
>>> return HttpResponseRedirect("/")
>>> else:
>>> sols = json.loads(request.GET.get('sols', ''))
>>>
>>>
>>> for i in range(0, len(sols)):
>>>
>>> new_entry = Solution(fullArray=sols[i])
>>> new_entry.save()
>>>
>>> return render_to_response('saveToDbAndReturn.html', {'sols': sols})
>>>
>>>
>>> *saveToDbAndReturn.html*
>>>
>>> 
>>>
>>> {% for i in range(sols) %}
>>>
>>> 
>>>   
>>> >> href="#collapse{{i}}" aria-expanded="true" aria-controls="collapse{{i}}">
>>> {{i}}
>>> 
>>>   
>>> 
>>> >> role="tabpanel" aria-labelledby="heading{{i}}">
>>>   
>>> {{sols[i]}}
>>>   
>>> 
>>> {% endfor %}
>>>
>>> 
>>>
>>>
>>> *Variable sol*:
>>>
>>> Just so you get an idea, the array sols is someting like this [ [[1, 2]
>>> , [3,4]] , [[4,5,6],[2,21,9]] ]
>>>
>>> --
>>> 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...@googlegroups.com.
>>> To post to this group, send email to django...@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/a4a2f0f7-0f6c-4918-8c01-669ff9a0f563%
>>> 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/0d917e98-68f5-4c1e-b1ff-9065e9b8e89e%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/CALn3ei0tYS5uMEq1vsSf2vuMKO-upHER8%2BffMvB_jGqd2msV%3Dg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: html response to be loaded in a div

2015-02-18 Thread João Marques
Im getting nothing really... I just do alert(response); and nothing shows 
up.

quarta-feira, 18 de Fevereiro de 2015 às 14:48:48 UTC, Vijay Khemlani 
escreveu:
>
> What is the actual content of the response you are getting from the AJAX 
> request?
>
> On Wed, Feb 18, 2015 at 10:27 AM, João Marques  > wrote:
>
>> Hey guys, so basicly I want to send a GET request with ajax to one of my 
>> views and the specific view returns an html response that will be loaded on 
>> a div.
>> The problem is that is doesn't seems to work at all. Nothing happers on 
>> the div.
>>
>> Please help me community! Thank you!
>>
>>
>> *HTML to be filled with the response:*
>>
>> 
>>
>>
>> *GET REQUEST:*
>>
>>  $.ajax({
>> type: 'GET',
>> url: 'saveToDbAndReturn',
>> data: {sols: holder}
>> }).done(function (response) {
>>document.getElementById("content").innerHTML = 
>> response;
>>
>> });
>>
>>
>>
>> *VIEW:*
>>
>> def saveToDbAndReturn(request):
>>
>> if(request.GET.get('sols', ''))=='':
>> return HttpResponseRedirect("/")
>> else:
>> sols = json.loads(request.GET.get('sols', ''))
>>
>>
>> for i in range(0, len(sols)):
>>
>> new_entry = Solution(fullArray=sols[i])
>> new_entry.save()
>>
>> return render_to_response('saveToDbAndReturn.html', {'sols': sols})
>>
>>
>> *saveToDbAndReturn.html*
>>
>> 
>>
>> {% for i in range(sols) %}
>>
>> 
>>   
>> > href="#collapse{{i}}" aria-expanded="true" aria-controls="collapse{{i}}">
>> {{i}}
>> 
>>   
>> 
>> > role="tabpanel" aria-labelledby="heading{{i}}">
>>   
>> {{sols[i]}}
>>   
>> 
>> {% endfor %}
>>
>> 
>>
>>
>> *Variable sol*: 
>>
>> Just so you get an idea, the array sols is someting like this [ [[1, 2] , 
>> [3,4]] , [[4,5,6],[2,21,9]] ]
>>
>> -- 
>> 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...@googlegroups.com .
>> To post to this group, send email to django...@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/a4a2f0f7-0f6c-4918-8c01-669ff9a0f563%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/0d917e98-68f5-4c1e-b1ff-9065e9b8e89e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: confirm_login_allowed in subclassed authentication returns wrong error message

2015-02-18 Thread Tomáš Sekanina
Thank you for your answer but I am not sure I understand your answer, can 
you explain why using the method described in docs does not work for this 
case? 

Dne úterý 17. února 2015 22:33:00 UTC+1 James Schneider napsal(a):
>
> You should raise the error in your custom user login form when attempting 
> to authenticate the user, not directly from the custom user model or 
> authentication backend.
>
> -James
> On Feb 17, 2015 12:20 PM, "Tomáš Sekanina"  > wrote:
>
>> Hello, 
>>
>> I have a custom user model and I need to extend the user authentication 
>> during login and found this solution: http://bit.ly/1u705o9  It would 
>> work great for me and I ended up implementing this 
>>  . It works in a manner that it does not 
>> let such user log in, but raises a different error message which is: 'Sorry, 
>> that's not a valid username or password.'.  I tried posting this 
>> question on different forums but got no answer :(. It seems really simple 
>> so I do not see there a place for an error unless there is some problem 
>> with the custom user model. I will be glad for your opinion at least :).
>>
>> Thank you in advance for any effort to solve this. 
>>
>>  -- 
>> 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...@googlegroups.com .
>> To post to this group, send email to django...@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/49e6d5be-4a4c-411d-8476-e851ea8f3f2a%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/66d88589-320b-46c5-aaeb-47c1ad248883%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Seems like a geodjango bug with multiple databases

2015-02-18 Thread Luan Nguyen
I'm using geodjango and multiple databases, and experiencing a quite weird 
situation, I guess it's a kind of bug but not absolutely sure since I'm 
pretty new to Django.

Here is how to reproduce the problem:
- Set up another database besides default:
DATABASES = {
'default': {
'ENGINE' : 'django.contrib.gis.db.backends.postgis' 
,
'NAME' : 'issue1',
'USER' : 'user',
'PASSWORD' : 'password',
'HOST' : '127.0.0.1',
'OPTIONS' : {
'autocommit' : True,
}
},
'another': {
'ENGINE' : 'django.contrib.gis.db.backends.postgis' 
,
'NAME' : 'issue2',
'USER' : 'user',
'PASSWORD' : 'password',
'HOST' : '127.0.0.1',
'OPTIONS' : {
'autocommit' : True,
}
},
}

And two models:
from django.db import models as default_models
from django.contrib.gis.db import models
# Create your models here.
class Hotel(models.Model):
hotel_name = models.CharField(max_length=255)
objects = models.GeoManager()

class Room(models.Model):
room_num = models.IntegerField()
hotel = models.ForeignKey(Hotel, null=True, blank=True)

Add data into issue2 database (leave issue1 blank), then go into shell:
>>>h = Hotel.objects.using('another').all()[0]
>>> h.id
9
>>>h.room_set.all()[0].id  #=> room id 
10 links to hotel id 9
10
>>>r = Room.objects.using('another').get(pk=10)
>>>r.hotel
Traceback (most recent call last):
File "", line 1, in 
File "/
Applications/MAMP/htdocs/Rainmaker/vir341/lib/python3.4/site-packages/django/db/models/fields/related.py
 
",
 
line 572, in __get__
rel_obj = qs.get()
File "/
Applications/MAMP/htdocs/Rainmaker/vir341/lib/python3.4/site-packages/django/db/models/query.py
 
",
 
line 357, in get
self.model._meta.object_name)
multi.models.DoesNotExist: Hotel matching query does not exist.

The thing is, if I create a hotel record on database issue1 with id of 9 
then the last command works, so I guess it tried to look up in default 
database. This doesn't have any problems if I use default manager for 
Hotel, so I guess it's a bug?

-- 
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/daa291e8-bf87-422a-a8fc-b23038e94268%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: html response to be loaded in a div

2015-02-18 Thread Vijay Khemlani
What is the actual content of the response you are getting from the AJAX
request?

On Wed, Feb 18, 2015 at 10:27 AM, João Marques 
wrote:

> Hey guys, so basicly I want to send a GET request with ajax to one of my
> views and the specific view returns an html response that will be loaded on
> a div.
> The problem is that is doesn't seems to work at all. Nothing happers on
> the div.
>
> Please help me community! Thank you!
>
>
> *HTML to be filled with the response:*
>
> 
>
>
> *GET REQUEST:*
>
>  $.ajax({
> type: 'GET',
> url: 'saveToDbAndReturn',
> data: {sols: holder}
> }).done(function (response) {
>document.getElementById("content").innerHTML = response;
>
> });
>
>
>
> *VIEW:*
>
> def saveToDbAndReturn(request):
>
> if(request.GET.get('sols', ''))=='':
> return HttpResponseRedirect("/")
> else:
> sols = json.loads(request.GET.get('sols', ''))
>
>
> for i in range(0, len(sols)):
>
> new_entry = Solution(fullArray=sols[i])
> new_entry.save()
>
> return render_to_response('saveToDbAndReturn.html', {'sols': sols})
>
>
> *saveToDbAndReturn.html*
>
> 
>
> {% for i in range(sols) %}
>
> 
>   
>  href="#collapse{{i}}" aria-expanded="true" aria-controls="collapse{{i}}">
> {{i}}
> 
>   
> 
>  role="tabpanel" aria-labelledby="heading{{i}}">
>   
> {{sols[i]}}
>   
> 
> {% endfor %}
>
> 
>
>
> *Variable sol*:
>
> Just so you get an idea, the array sols is someting like this [ [[1, 2] ,
> [3,4]] , [[4,5,6],[2,21,9]] ]
>
> --
> 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/a4a2f0f7-0f6c-4918-8c01-669ff9a0f563%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/CALn3ei2DDwJFU%3Dn-URU4kxT%2BeMo36%3DBgi6OJsTTFRBEFK24xjQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: drop-in replacement for admin module foreign key relation UI

2015-02-18 Thread Derek
I think https://pypi.python.org/pypi/django-autocomplete-light is good, 
quick and stable.  It has "heritage" (been around and battle-tested) and 
provides the features that are key.  (And supports Django 1.7, Python3).

On Tuesday, 17 February 2015 01:03:05 UTC+2, Jani Kajala wrote:
>
> Hi all,
>
> I have tons of objects referenced by ForeignKey in Django Admin module UI, 
> and I would like to replace it with something more optimal, e.g. auto-fill 
> input box or something.
>
> My model looks something like this:
>
> class SoapCall(models.Model):
> customer = models.ForeignKey(Customer, null=True, blank=True)
> created = models.DateTimeField(default=now)
> wsdl = models.CharField(max_length=255)
> func = models.CharField(max_length=63)
> request = models.TextField()
> response = models.TextField()
>
> Now, this works great, but if I have 10 Customer objects it's not very 
> nice to generate one huge ... element for every page load.
>
> There seems to be many possible plugins/implementations for this, but I 
> was just wondering if anyone would have up-to-date recommendations in this 
> mailing list. I'm on Python 3.4.2 and Django 1.7.4.
>
> Thanks beforehand. :) 
>
>
> Br,
> Jani
>
> P.S. <3 Django
>
>

-- 
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/d02362d4-0176-44b0-85c5-a24937f91820%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


html response to be loaded in a div

2015-02-18 Thread João Marques
Hey guys, so basicly I want to send a GET request with ajax to one of my 
views and the specific view returns an html response that will be loaded on 
a div.
The problem is that is doesn't seems to work at all. Nothing happers on the 
div.

Please help me community! Thank you!


*HTML to be filled with the response:*




*GET REQUEST:*

 $.ajax({
type: 'GET',
url: 'saveToDbAndReturn',
data: {sols: holder}
}).done(function (response) {
   document.getElementById("content").innerHTML = response;

});



*VIEW:*

def saveToDbAndReturn(request):

if(request.GET.get('sols', ''))=='':
return HttpResponseRedirect("/")
else:
sols = json.loads(request.GET.get('sols', ''))


for i in range(0, len(sols)):

new_entry = Solution(fullArray=sols[i])
new_entry.save()

return render_to_response('saveToDbAndReturn.html', {'sols': sols})


*saveToDbAndReturn.html*



{% for i in range(sols) %}


  

{{i}}

  


  
{{sols[i]}}
  

{% endfor %}




*Variable sol*: 

Just so you get an idea, the array sols is someting like this [ [[1, 2] , 
[3,4]] , [[4,5,6],[2,21,9]] ]

-- 
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/a4a2f0f7-0f6c-4918-8c01-669ff9a0f563%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Beginning Djangi

2015-02-18 Thread Jorge Andrés Vergara Ebratt
I think for this kind of questions you could benefit from checking
https://www.djangosites.org/

There you can see a list of sites built with Django and get and idea of the
kind of things you could do with it

On Wed Feb 18 2015 at 7:49:02 AM Mukul Sharma 
wrote:

> I am new to Django. I want to build a news website with real time
> refreshing. So my question is, "is django enough to create this type of
> website?, or I do need something else also". I am familiar with JavaScript,
> HTML and CSS.
>
> --
> 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/7ef4fb8f-3bb1-4723-8794-570fa6c9b200%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/CAAeX05GC40ELiZWOhKEH5p6K08NSvwdeERxkZn9hC5cSdFirXA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Beginning Djangi

2015-02-18 Thread Mukul Sharma
I am new to Django. I want to build a news website with real time refreshing. 
So my question is, "is django enough to create this type of website?, or I do 
need something else also". I am familiar with JavaScript, HTML and CSS.

-- 
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/7ef4fb8f-3bb1-4723-8794-570fa6c9b200%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: ImportError

2015-02-18 Thread Petar Pilipovic
Ok, I will work on it as soon as posible, tnx for your help.

On Wednesday, February 18, 2015, aRkadeFR  wrote:
>
> On 02/18/2015 07:17 AM, Petar Pilipovic wrote:
>
> aRkadeFR I have one other question regarding urls.py. I got this error
when I am trying to go from contact or about to home page.
>
> Using the URLconf defined in tryDjango.urls, Django tried these URL
patterns, in this order:
>
> ^$ [name='home']
> ^about/$ [name='about']
> ^contact/$ [name='contact']
> ^admin/
> ^static\/(?P.*)$
> ^media\/(?P.*)$
>
> The current URL, contact/{% url 'home' %}, didn't match any of these.
>
> the first part of your list are regexes. The "$" means the end of the
line.
> So there's nothing after "contact/" ; you can't do "contact/".
>
> A link in a template is as simple as:
> Link
> For your home page:
> Link
>
> Now mine root/urls.py, and this is mine navbar.html, every other thing is
connected properly, and I can go from home page to contact, about, but I
can not go reverse.
> Can you explain to me where is the error?
> Tank you.
>
> On Wednesday, 18 February 2015 06:02:54 UTC+1, Petar Pilipovic wrote:
>>
>> Hello aRkadeFR.
>> I have solved mine problem, I was working whit Python 2.X here, and like
you recommended I have done some  absolute import.
>> from forms import contactForm ,  and that solved it.
>> There was one other error, mine base.html did not render mine block
content, because I did not defined it, I did that and now mine form is
working.
>> Tank you.
>> Best.
>> Petar...;-)
>>
>>
>> On Tuesday, 17 February 2015 14:57:46 UTC+1, aRkadeFR wrote:
>>>
>>> Two different problems here.
>>>
>>> For your import of contactForm, please try the relative import like:
>>> from contact.forms import contactForm
>>> > contact is your app name
>>>
>>> try the (absolute_import)[1] too if you're python2.
>>>
>>> [1] https://docs.python.org/2/library/__future__.html
>>>
>>> Let us know if this works
>>>
>>> On 02/17/2015 01:30 PM, Petar Pilipovic wrote:
>>>
>>> Maybea this can help you, a bref info :
>>> I made a contact app, I have defined app url in mine url root project,
then i made a forms.py file, i have defined form in it, then i have made
contact.html where I have defined contact.html form which mine app will
render, I have then go to views.py an imported contactForm class.
>>> Afther that I have defined connection between  contact.html page and
navbar.html, collected static, run server and bam error occur.
>>> I just can not explain to mine self how come he can not import
contactForm, and how com he can not render havbar.html, error pop's out
line 15.
>>> On Tuesday, February 17, 2015, Petar Pilipovic  wrote:
>>> > Hello, I can not do it wright now I am not at home.
>>> > As for mine forms.py, it is in mine contact app. I have two apps in
mine root project, one off them is contact.
>>> > I will send you more info as soon as posible.
>>> > Tank you.
>>> >
>>> >
>>> > On Tuesday, February 17, 2015, aRkadeFR  wrote:
>>> >> Hello :)
>>> >>
>>> >> Can you dump us the layout of your files?
>>> >> Where is your forms.py?
>>> >>
>>> >> Thanks
>>> >>
>>> >> On 02/17/2015 06:36 AM, Petar Pilipovic wrote:
>>> >>
>>> >> Hello all, I am trying to build and e_commerc django app, but I have
encounter an confusing error.
>>> >> Mine Error is:
>>> >> Environment:
>>> >>
>>> >> Request Method: GET
>>> >> Request URL: http://127.0.0.1:8000/
>>> >> Django Version: 1.7.4
>>> >> Python Version: 2.7.6
>>> >> Installed Applications:
>>> >> ('django.contrib.admin',
>>> >>  'django.contrib.auth',
>>> >>  'django.contrib.contenttypes',
>>> >>  'django.contrib.sessions',
>>> >>  'django.contrib.messages',
>>> >>  'django.contrib.staticfiles',
>>> >>  'profiles')
>>> >> Installed Middleware:
>>> >> ('django.contrib.sessions.middleware.SessionMiddleware',
>>> >>  'django.middleware.common.CommonMiddleware',
>>> >>  'django.middleware.csrf.CsrfViewMiddleware',
>>> >>  'django.contrib.auth.middleware.AuthenticationMiddleware',
>>> >>  'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
>>> >>  'django.contrib.messages.middleware.MessageMiddleware',
>>> >>  'django.middleware.clickjacking.XFrameOptionsMiddleware')
>>> >>
>>> >> Template error:
>>> >> In template
/home/petarp/Documents/Udemy/Learn_Django/static/templates/navbar.html,
error at line 15
>>> >>cannot import name contactForm
>>> >>5 :   
>>> >>
>>> >>6 : Toggle navigation
>>> >>
>>> >>7 : 
>>> >>
>>> >>8 : 
>>> >>
>>> >>9 : 
>>> >>
>>> >>10 :   
>>> >>
>>> >>11 :   Bootstrap
theme
>>> >>
>>> >>12 : 
>>> >>
>>> >>13 : 
>>> >>
>>> >>14 :   
>>> >>
>>> >>15 : {% url 'home' as home %}
>>> >>
>>> >>16 :{% url 'about' as about %}
>>> >>
>>> >>17 :{% url 'contact' as contact %}
>>> >>
>>> >>18 : Home
>>> >>
>>> 

Re: ImportError

2015-02-18 Thread aRkadeFR


On 02/18/2015 07:17 AM, Petar Pilipovic wrote:
aRkadeFR I have one other question regarding urls.py. I got this error 
when I am trying to go from contact or about to home page.



Using the URLconf defined in |tryDjango.urls|, Django tried these URL 
patterns, in this order:


 1. ^$ [name='home']
 2. ^about/$ [name='about']
 3. ^contact/$ [name='contact']
 4. ^admin/
 5. ^static\/(?P.*)$
 6. ^media\/(?P.*)$

The current URL, |contact/{% url 'home' %}|, didn't match any of these.



the first part of your list are regexes. The "$" means the end of the line.
So there's nothing after "contact/" ; you can't do "contact/".

A link in a template is as simple as:
Link
For your home page:
Link

Now mine root/urls.py , and this is mine 
navbar.html , every other thing is connected 
properly, and I can go from home page to contact, about, but I can not 
go reverse.

Can you explain to me where is the error?
Tank you.

On Wednesday, 18 February 2015 06:02:54 UTC+1, Petar Pilipovic wrote:

Hello aRkadeFR.
I have solved mine problem, I was working whit Python 2.X here,
and like you recommended I have done some  absolute import.
from forms import contactForm ,  and that solved it.
There was one other error, mine base.html did not render mine
block content, because I did not defined it, I did that and now
mine form is working.
Tank you.
Best.
Petar...;-)



On Tuesday, 17 February 2015 14:57:46 UTC+1, aRkadeFR wrote:

Two different problems here.

For your import of contactForm, please try the relative import
like:
from contact.forms import contactForm
> contact is your app name

try the (absolute_import)[1] too if you're python2.

[1] https://docs.python.org/2/library/__future__.html


Let us know if this works

On 02/17/2015 01:30 PM, Petar Pilipovic wrote:

Maybea this can help you, a bref info :
I made a contact app, I have defined app url in mine url root
project, then i made a forms.py file, i have defined form in
it, then i have made contact.html where I have defined
contact.html form which mine app will render, I have then go
to views.py an imported contactForm class.
Afther that I have defined connection between  contact.html
page and navbar.html, collected static, run server and bam
error occur.
I just can not explain to mine self how come he can not
import contactForm, and how com he can not render
havbar.html, error pop's out line 15.
On Tuesday, February 17, 2015, Petar Pilipovic
 wrote:
> Hello, I can not do it wright now I am not at home.
> As for mine forms.py, it is in mine contact app. I have two
apps in mine root project, one off them is contact.
> I will send you more info as soon as posible.
> Tank you.
>
>
> On Tuesday, February 17, 2015, aRkadeFR
 wrote:
>> Hello :)
>>
>> Can you dump us the layout of your files?
>> Where is your forms.py?
>>
>> Thanks
>>
>> On 02/17/2015 06:36 AM, Petar Pilipovic wrote:
>>
>> Hello all, I am trying to build and e_commerc django app,
but I have encounter an confusing error.
>> Mine Error is:
>> Environment:
>>
>> Request Method: GET
>> Request URL: http://127.0.0.1:8000/
>> Django Version: 1.7.4
>> Python Version: 2.7.6
>> Installed Applications:
>> ('django.contrib.admin',
>>  'django.contrib.auth',
>>  'django.contrib.contenttypes',
>>  'django.contrib.sessions',
>>  'django.contrib.messages',
>>  'django.contrib.staticfiles',
>>  'profiles')
>> Installed Middleware:
>> ('django.contrib.sessions.middleware.SessionMiddleware',
>>  'django.middleware.common.CommonMiddleware',
>>  'django.middleware.csrf.CsrfViewMiddleware',
>>  'django.contrib.auth.middleware.AuthenticationMiddleware',
>>
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
>>  'django.contrib.messages.middleware.MessageMiddleware',
>>  'django.middleware.clickjacking.XFrameOptionsMiddleware')
>>
>> Template error:
>> In template
/home/petarp/Documents/Udemy/Learn_Django/static/templates/navbar.html,
error at line 15
>>cannot import name contactForm
>>5 :   
>>
>>6 : Toggle
navigation
>>
>>7 : 
>>
>>8 : 
>>
>>9 : 
>>
>>10 :   
>>
  

Odesk , Freelancer Clone, Auction site in django

2015-02-18 Thread Michal Tomczakowski
Where can I find clones in django? something simmilar i can change in to 
Auction site? 


-- 
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/c960b8b9-9b13-44e7-8053-06c5bf86e4f3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: How to make a list-view sortable in frontend?

2015-02-18 Thread ThomasTheDjangoFan
Hi James,

yeah - the data is displayed as a table.. 

django-tables2 looks awesome... I'm gonna give it a try.

Are there any other solutions out there that you can recon?

Am Dienstag, 17. Februar 2015 23:51:25 UTC+1 schrieb James Schneider:
>
> Are you displaying the data in a table? If so, I've always used 
> django-tables2, which includes the ability to have sorting in each of the 
> columns via a generated link in the header row of any or all columns 
> (up/down arrow, etc.).
>
> -James
> On Feb 17, 2015 2:43 PM, "ThomasTheDjangoFan" <
> stefan.eich...@googlemail.com > wrote:
>
>> Hi 'ya,
>>
>> I am new to Django and would like to create a *sortable ListView* (just 
>> like the Lists in Django Admin) for an array of object-instances. The 
>> objects themself are *non Django-Models* and are pre-populated in an 
>> array before showing the view.
>>
>> I've got a basic ListView working (
>> https://docs.djangoproject.com/en/1.7/topics/class-based-views/), 
>> but the important question remains: *How do I make it sortable? *
>>
>> Can you give me to some examples to check out how to implement a sortable 
>> ListView?
>>
>> Kind regards and thanks a lot for your support
>> Thomas
>>
>> -- 
>> 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...@googlegroups.com .
>> To post to this group, send email to django...@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/9676838b-9df9-46c0-8b57-b1e4aa43ecce%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/20de2a3a-b3bf-47a1-99a1-8816993ce904%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: how to handle race conditions?

2015-02-18 Thread aRkadeFR

Hello,

My opinion on the problem:
A database is ACID, so there will be no problem if every information
are stored in the database.
There should be a "available_quantity" property on a item.
This will allow you to have more control of the stock too.

If a customer put an item in his basket this checks the
available quantity of the item and update it. So you
succeed to sell only available item.

Then the release item problem:
If the user didnt buy the item before X minutes, you need
to forbid his basket and you put again his items inside
the available_quantity.

Don't know if it helped you?

Thanks,

PS:
May I suppose to you that the get_quantity function and set_quantity
should be in the manager of the model?
something like:
```
class ShopItemManager(Manager):
def get_quantity(self, name):
return self.get_queryset().get(name=name).quantity

class ShopItem(Model):
...
objects = ShopItemManager()
```
This allow you to tied up your function to the model.


On 02/18/2015 06:01 AM, Abraham Varricatt wrote:

Hello,

I'm trying to make an app where folks can order X quantity of an item. 
The condition is that the order should only be made if inventory 
exists. Assume that we have stock of Y items. This means that only if 
Y >= X should we allow the sale to go through. And once we accept an 
order, the inventory should be updated so that Y = Y - X.


The way I've currently implemented this is with a pair of 
getter/setter methods. I have a model 'ShopItem' which has two fields 
called 'quantity' and 'name' (this is unique). I've made a utility 
class (outside model source file) where I've made the following 2 
functions;


def get_quantity(name):
  stuff_left = 0
  try:
item = ShopItem.objects.get(name=name)
stuff_left = item.quantity
  except ShopItem.DoesNotExist:
pass # return zero
  return stuff_left

def set_quantity(name, stuff_left):
  item = ShopItem.objects.get(name=name)
  item.quantity = stuff_left
  item.save()


Elsewhere in my project, if I need to display the remaining quantity 
of an item in a view, I'll pass the result from get_quantity(). At 
sale time here is how I update things;


def customer_buy(name, number):
  temp = get_quantity(name)
  if (temp >= number):
temp = temp - number
set_quantity(name, temp)
// do other things
  else:
// sale failed, do something else

I tested this on my system and things appear to work. But I'm 
concerned about race conditions. What if two different customers came 
in to buy the same item? As in; if I only have 7 items in stock, but 
one customer came to buy 6nos and another 5nos. There is a chance (as 
per my understanding) that both orders will be accepted - even worse, 
my inventory will not accurately reflect the updated situation.


Any ideas on how this issue can be resolved? I've come across this 
- https://docs.djangoproject.com/en/1.6/ref/models/instances/#updating-attributes-based-on-existing-fields 
, but not sure how to apply it to the current scenario.


Learning django,
Abraham V.

--
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/7552759e-81d6-4cfb-a0ce-4fed95eef5bc%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/54E453B0.4030509%40arkade.info.
For more options, visit https://groups.google.com/d/optout.