Re: Updating reCaptcha (widget) in django-based OSQA

2018-04-20 Thread Jani Tiainen
I recently used similar stuff and IIRC recaptcha2 doesn't use one of those
fields anymore.

pe 20. huhtikuuta 2018 klo 17.40 DougN  kirjoitti:

> I'm new to django and trying to figure out how some of the form processing
> works.
>
> For example, I have this field:
>
> class ReCaptchaField(forms.Field):
> def __init__(self, *args, **kwargs):
> super(ReCaptchaField, self).__init__(widget=ReCaptchaWidget)
>
> The widget renders some HTML (reCaptcha2 stuff).
>
> I understand the concept of Field.clean, but the existing code is cleaning
> an array, and I haven't been able to work out where those values come from
> or what they are:
>
> def clean(self, values):
> super(ReCaptchaField, self).clean(values[1])
> recaptcha_challenge_value = smart_unicode(values[0])
> recaptcha_response_value = smart_unicode(values[1])
> ... code to do stuff with the values
>
> How can I figure out what those incoming values are?   The widget has a
> value_from_datadict, but I can't see where those values get populated
> either.
>
> class ReCaptchaWidget(forms.Widget):
> def render(self, name, value, attrs=None):
> return
> mark_safe(force_unicode(captcha.displayhtml(settings.RECAPTCHA_PUB_KEY)))
>
> def value_from_datadict(self, data, files, name):
> return data.get('recaptcha_challenge_field', None),
> data.get('recaptcha_response_field', None)
>
> For example, there are variables named recaptcha_challenge_field, but that
> string literal isn't used anywhere else.  Seems like magic going on here...
> ;(
>
> Thanks for any pointers.
>
> Doug
>
> --
> 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 https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/2630b767-678b-4f29-879a-4c2d01a3e252%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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHn91ofxUbr0k7dzL0C3WoQot13vDzNLcQ6SUpmaxa16opNSBA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Updating reCaptcha (widget) in django-based OSQA

2018-04-20 Thread DougN
I'm new to django and trying to figure out how some of the form processing 
works.

For example, I have this field:

class ReCaptchaField(forms.Field):
def __init__(self, *args, **kwargs):
super(ReCaptchaField, self).__init__(widget=ReCaptchaWidget)

The widget renders some HTML (reCaptcha2 stuff).

I understand the concept of Field.clean, but the existing code is cleaning 
an array, and I haven't been able to work out where those values come from 
or what they are:

def clean(self, values):
super(ReCaptchaField, self).clean(values[1])
recaptcha_challenge_value = smart_unicode(values[0])
recaptcha_response_value = smart_unicode(values[1])
... code to do stuff with the values

How can I figure out what those incoming values are?   The widget has a 
value_from_datadict, but I can't see where those values get populated 
either.

class ReCaptchaWidget(forms.Widget):
def render(self, name, value, attrs=None):
return 
mark_safe(force_unicode(captcha.displayhtml(settings.RECAPTCHA_PUB_KEY)))

def value_from_datadict(self, data, files, name):
return data.get('recaptcha_challenge_field', None), 
data.get('recaptcha_response_field', None)

For example, there are variables named recaptcha_challenge_field, but that 
string literal isn't used anywhere else.  Seems like magic going on here... 
;(

Thanks for any pointers.

Doug

-- 
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/2630b767-678b-4f29-879a-4c2d01a3e252%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Django @transaction.atomic() to prevent creating objects in concurrency

2018-04-20 Thread Aamu Padi
I have a ticket model, and its ticket serialiazer. The ticket model has a
*bought* and a *booked_at* field. And also a *unique_together* attribute
for *show* and *seat*.

class Ticket(models.Model):
show = models.ForeignKey(Show, on_delete=models.CASCADE)
seat = models.ForeignKey(Seat, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
booked_at = models.DateTimeField(default=timezone.now)
bought = models.BooleanField(default=False)

class Meta:
unique_together = ('show', 'seat')


   - On the ticket serializer, the serializer on validation checks whether
   the ticket was bought or not.
   - If it is bought, then it will raise an error.
  - If its not bought then Check if the ticket was booked within 5
  minutes.
 - If its booked within 5 minutes, then raise an error.
 - Else if the booked time is more than 5 minutes, than delete the
 old ticket and return valid.

TicketSerializer:

class TicketSerializer(serializers.Serializer):
seat = serializers.PrimaryKeyRelatedField(queryset=Seat.objects.all())
show = serializers.PrimaryKeyRelatedField(queryset=Show.objects.all())
user = serializers.PrimaryKeyRelatedField(queryset=User.objects.all())
bought = serializers.BooleanField(default=False)

def validate(self, attrs):
if attrs['seat']:
try:
ticket = Ticket.objects.get(show=attrs['show'], seat=seat)
if not ticket.bought:
if ticket.booked_at < timezone.now() -
datetime.timedelta(minutes=5):
# ticket booked crossed the deadline
ticket.delete()
return attrs
else:
# ticket in 5 mins range
raise serializers.ValidationError("Ticket with same
show and seat exists.")
else:
raise serializers.ValidationError("Ticket with same
show and seat exists.")
except Ticket.DoesNotExist:
return attrs
else:
raise serializers.ValidationError("No seat value provided.")


On the view, I am using *@transaction.atomic()* to make sure the ticket/s
are created only if all of them are valid, or don't create ANY ticket if
not valid.

@transaction.atomic()
@list_route(
methods=['POST'],
)
def book_tickets_by_show(self, request, show_id=None):
try:
show = Show.objects.get(id=show_id)
user = request.user
...
...
data_list = [...]
with transaction.atomic():
try:
serializer = TicketSerializer(data=data_list, many=True)
if serializer.is_valid():
serializer.save()

return Response(serializer.errors,
status=status.HTTP_400_BAD_REQUEST)
except (Seat.DoesNotExist, ValueError, ConnectionError) as e:
return Response({'detail': str(e)},
status=status.HTTP_400_BAD_REQUEST)
except (Show.DoesNotExist, IntegrityError) as e:
return Response({'detail': str(e)},
status=status.HTTP_400_BAD_REQUEST)

What I would like to know is will it help in preventing when more than one
requests are called for creating the ticket/s for same seat/s?

Suppose, User A wants to book ticket with seat 5,6 and User B wants to book
ticket with seat 3,6, and another User C wants to book the ticket with
seats 2,3,4,5,6. Will the above method prevent booking tickets for their
respective seats for all the users, and only create tickets for one user
(maybe whose transaction was first)? Or if there is a better way then could
you please advice me how to. I hope I was clear. If not please ask.

Thank you

-- 
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHSNPWsHM%3Dk1ezHr3Vp_k9_4fviz%2B7ZmzZ7iuieZgifLg09k2w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


RE: Use self-increasing arguments in numeric for-loop in Django

2018-04-20 Thread Matthew Pava
You have a list of values.  Use the Pythonic way to iterating over a list, 
which does not involve using indices.

{% for o in graphobject %}
{{ o }}
{{ o.subfield }}
{% endfor %}


From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On 
Behalf Of Bill Torcaso
Sent: Friday, April 20, 2018 8:50 AM
To: Django users
Subject: Re: Use self-increasing arguments in numeric for-loop in Django


I an new-ish to Django, and I ask this to hear from more experienced users 
whether this would work at all, and whether it would be considered a good or 
bad practice.

Goal: given an object and an integer index, retrieve the sub-object at that 
index within the incoming object.

Method:  Write a custom tag.  Provide the tag with the larger object, and the 
integer index.  Return the sub-object at that index (or whatever field(s) are 
needed).

Usage:


  {% for i in metarange %}

{% graphobject_get_by_index graphobject {{ i }} %}

  {% endfor %}


In this particular case, there is an issue about whether the sub-object can be 
null, for some definition of null.  There are various ways to deal with that.

Again, I am new-ish to Django.  Is there a similar approach in which 
'get_by_index' is a method on class Graphobject, such that this would work?

{% graphobject.get_by_index {{ i }} %}

Thanks for any explanation about these approaches.


On Thursday, April 19, 2018 at 12:01:37 PM UTC-4, 
shaw...@gmail.com wrote:

I am currently working on django.

I have a list of values which stored in 'graphobject', and 'metarange' is 
defined as range(0,59). In that case, how could I use numbers as argument to 
display the value stored in graphobject? I tried using following codes but it 
doesn't work



{% for i in metarange %}

{% if graphobject.i != '' %}

{{ graphobject.i }}

{% endif %}

{% endfor %}



Please tell me how could I do 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+unsubscr...@googlegroups.com.
To post to this group, send email to 
django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/9c162dd2-201b-4370-bac0-f3afb3db163a%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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/62ae273ee7634261ad5421b0809c28cc%40ISS1.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.


Re: Use self-increasing arguments in numeric for-loop in Django

2018-04-20 Thread Bill Torcaso

I an new-ish to Django, and I ask this to hear from more experienced users 
whether this would work at all, and whether it would be considered a good 
or bad practice.

Goal: given an object and an integer index, retrieve the sub-object at that 
index within the incoming object.

Method:  Write a custom tag.  Provide the tag with the larger object, and 
the integer index.  Return the sub-object at that index (or whatever 
field(s) are needed).

Usage:

  {% for i in metarange %}{% graphobject_get_by_index graphobject {{ i }} 
%}  {% endfor %}



In this particular case, there is an issue about whether the sub-object can 
be null, for some definition of null.  There are various ways to deal with 
that.

Again, I am new-ish to Django.  Is there a similar approach in which 
'get_by_index' is a method on class Graphobject, such that this would work?

{% graphobject.get_by_index {{ i }} %}

Thanks for any explanation about these approaches.


On Thursday, April 19, 2018 at 12:01:37 PM UTC-4, shaw...@gmail.com wrote:
>
> I am currently working on django.
>
> I have a list of values which stored in 'graphobject', and 'metarange' is 
> defined as range(0,59). In that case, how could I use numbers as argument 
> to display the value stored in graphobject? I tried using following codes 
> but it doesn't work
>
>
> {% for i in metarange %}{% if graphobject.i != '' %}{{ graphobject.i }}{% 
> endif %}{% endfor %}
>
>
> Please tell me how could I do 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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/9c162dd2-201b-4370-bac0-f3afb3db163a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


issue with django model and form

2018-04-20 Thread Pranay reddy


I have "user" Model

username email phonenumber

I want to access all "user" model emails in other model "B" and make user 
to select more than one email and store it has commaseperatedvalues

"B" colums are

  organization Designation share_knowledge_with
abc   manager   (here all emails which user selected
 to be stored with commaseperated)   

I tried like this but not working:

MODEL

class B(models.Model):
   organization=models.CharField(max_length=200,blank=False,null=True)
   emails_for_help = models.TextField(null=True,help_text="select with whom 
   you want to share knowledge") 

form

class Bform(ModelForm): 
 emails_for_help=forms.ModelMultipleChoiceField(queryset=User.objects.all(),
 widget=forms.CheckboxSelectMultiple)
 class Meta:
model=B
fields=["organization","emails_for_help"]

I tried like this but it is taking null value in "emails_for_help"

-- 
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/67f9dbe0-e0ec-448f-b63d-9049eae551d5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


ImageSlider in Django

2018-04-20 Thread deviya sweety
Hi friends,
I want sliding images in my site, how can I do it in django?
I've tried to use Django-hero-slider in my project but when I try to add 
slideritem objects in the admin page I'm getting error.
can anyway help me in this issue please!!
Thankyou in advance :)

-- 
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/103030f0-976a-444e-90e5-aca6781afdc3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Authentication Django RestFramework

2018-04-20 Thread Amirul Abu
Dear Musharaf,

If you want authentication built-in you should use Django and not Django
REST Framework.



On Fri, Apr 20, 2018 at 2:58 PM, Andréas Kühne 
wrote:

> Hi Musharef,
>
> Yes, everything you are asking for is possible. However as far as I know
> there is no standard solution for your problem, you will have to write all
> of that by yourself. That being said, it's not that hard:
> 1. A view for registration - returns a created username / password - or
> the username / password the user choose.
> 2. You have a counter on the user that increments on every request before
> they are a paying user.
> 3. Tokens don't need to expire unless you want them to.
>
> Med vänliga hälsningar,
>
> Andréas
>
> 2018-04-19 21:38 GMT+02:00 Musharaf Baig :
>
>> There is not register/login mechanism for users.
>>
>> I need two types of authentication:
>>
>>1.
>>
>>To make it available I would like to let the user use it for free
>>with a limited number of call s or time
>>
>> - for example after the API is called 50 times, I would like that the
>> token expires. An alternative solution is that  the token should expire in
>> 2 days.
>>
>> 2) An authentication with a key that does not expire at all.
>>
>> Is this possible? Need suggestions. 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 https://groups.google.com/group/django-users.
>> To view this discussion on the web visit https://groups.google.com/d/ms
>> gid/django-users/27c80056-e9c7-4375-998c-d5304a3d7c5d%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 https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/CAK4qSCeYbBpcaAGM5OeiDxLA9uoaY
> 4ZpVYSx0XdC-2t5XKc2AQ%40mail.gmail.com
> 
> .
>
> For more options, visit https://groups.google.com/d/optout.
>



-- 
*Amirul bin Abu*

H/P:(+6)013-6305920
Email: amirul...@gmail.com
Website: http://amirulabu.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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2B83b8Jj6aAwaXn3CfBJOArjJjvQzn9hvm-rc8G7KXhUygKNmw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.