Using Signals and method in models to automate key and qrcode>> help

2021-04-17 Thread Tal Bar-Or


Hello ,

i have a simple model that create additional one to one relation to User a 
profile like table with tables of pytop code and qrcode associated per user.

I had success to create the pytop code ,but the qr code i have difficulty 
to achieve it since i need to get,

The current users created and the pyotp code, on evry user creation i have 
signals receivers that create the additional profile associated tables , 
but i need your advice ,on methods attached to the model that will create 
automatically the request tables , what is the best practice to achieve 
such task.

Please advice

Thanks

*The profile model*

from django.db import models
from django.contrib.auth.models import User
import pyotp
import qrcode


# Create your models here.



def otp_google_auth(code,usr):
print('details: ',code,usr)

googleauth = pyotp.totp.TOTP(code).provisioning_uri(name=str(usr) + 
'@google.com', issuer_name='Secure Dalet')
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(googleauth)
qr.make(fit=True)

img = qr.make_image(fill_color="black", back_color="white")

google_qr = img 

class Profile(models.Model):

user = models.OneToOneField(User, on_delete=models.CASCADE, blank=True, 
null=True)

#qr_creation = otp_google_auth(secret, user)
otp_code = models.CharField(max_length=200, default=token_creation)
user_qr =  models.ImageField(upload_to='images/', default= None)
last_name = models.CharField(max_length=200, null=True, blank=True)
phone = models.CharField(max_length=200, null=True, blank=True)

def __str__(self):
return str(self.user)

def token_creation(self):

secret = pyotp.random_base32()
self.otp_code.save(secret, save=False)
super().save()

*The  signals for profile table*

from django.db.models.signals import post_save
from django.dispatch import receiver

from django.contrib.auth.models import User
from .models import Profile

@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):

if created:
Profile.objects.create(user=instance)
print('Profile created!')

#post_save.connect(create_profile, sender=User)

@receiver(post_save, sender=User)
def update_profile(sender, instance, created, **kwargs):

if created == False:
instance.profile.save()
print('Profile updated!')




-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/94e3236b-3633-4937-89bd-bf38adade13dn%40googlegroups.com.


Re: Create email template with the click of a button

2021-04-17 Thread Ryan Nowakowski
I've had a similar issue in the past. With the latest version of Django pretty 
much each database back in now supports JSONField. So I created a custom field 
that inherits from JSON field to take a string of comma delimited values, split 
them by comma, validate each value individually, then store the whole thing in 
the database as a JSON list.

In your case you'll use the email validator used by the EmailField to validate 
each value.

On April 15, 2021 3:21:44 PM CDT, Smiley  wrote:
>Hello,
>
>I have another problem. 
>
>I am trying to get CC to accept multiple email addresses but I do not
>seem 
>to get it to work as it always says *Enter a valid email address*. I've
>
>searched for solutions, heard the new field *MultiValueField* but I
>also do 
>not know how to use this in *forms.py*.
>
>
>
>This is my *forms.py* code:
>
>from django import forms
>#  from django.forms.fields import MultiValueField
>from django.forms.widgets import EmailInput, TextInput
>
>
>class ComposeForm(forms.Form):
>email_to = forms.EmailField(label="To", widget=EmailInput(attrs
>={"size": 76}))
>email_cc = forms.EmailField(
>label="CC",
>required=False,
>widget=EmailInput(attrs={"size": 76, "multiple": True}),
>)
>email_subject = forms.CharField(
>required=False, widget=TextInput(attrs
>={"placeholder": "Subject", "size": 76})
>)
>email_message = forms.CharField(
>required=True, label="", widget=forms.Textarea(attrs
>={"rows": 19, "cols": 78})
>)
>
>
>
>And this is my *views.py* code:
>
>from django.shortcuts import render
>
>from .forms import ComposeForm
>
>
>def email_template(request):
>if request.method == "GET":
>form = ComposeForm()
>else:
>form = ComposeForm(request.POST)
>if form.is_valid():
>print(form)
>email_to = form.cleaned_data["email_to"]
>email_cc = form.cleaned_data["email_cc"]
>email_subject = form.cleaned_data["email_subject"]
>email_message = form.cleaned_data["email_message"]
>
>print("Emails:", email_cc)
>else:
>print("DEBUG:", form.errors)
>return render(request, "email_template.html", {"form": form})
>
>
>
>And this is my *email_template.html* HTML:
>
>
>
>
>
>
> 
>Home
>
>
>
>{% csrf_token %}
>
>{{ form.as_table }}
>
>
>
>
>
>
>
>
>
>Please tell me whether it's possible to have multiple email addresses
>in 
>*email_cc* aka *CC* field in Django or do I have to use frontend
>frameworks 
>for this one job or start using one for better practice (get used to
>using 
>frameworks to build frontend side)?
>
>Please advise.
>
>Regards,
>Kristen
>
>sebasti...@gmail.com kirjutas teisipäev, 23. märts 2021 kl 22:24:01
>UTC+2:
>
>> I have implement it as a bootstrap modal where a Form with fields are
>
>> shown. When User click submit the Page load New but If you don't want
>that 
>> Page is load New you need a Ajax jquery submit to django
>>
>> Kristen  schrieb am Mo., 22. März 2021, 15:46:
>>
>>> Hello,
>>>
>>> Correct. I want to the user to click a button and have a form appear
>
>>> where the user can compose an email.
>>>
>>> Gmail uses javascript to render their form on top of the inbox
>because 
 they don't want you to have to leave the inbox page. You probably
>don't 
 have that requirement.

 Let

View photos or videos in the Django app

2021-04-17 Thread מוריה יצחקי
Hi
How to view photos or videos in the Django app
I would be very happy to help
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/b23fe389-107e-42c5-a0e4-d9104550dc54n%40googlegroups.com.


Re: Create email template with the click of a button

2021-04-17 Thread Sebastian Jung
Hello,

Emailfield is nothing other then a normal InputField but later with a
special validator. I would take InputField then write your own Validator
where you split input commasparated an validate every address with a regex.

https://stackoverflow.com/questions/3217682/checking-validity-of-email-in-django-python

Regards

Am Do., 15. Apr. 2021 um 22:22 Uhr schrieb Smiley :

> Hello,
>
> I have another problem.
>
> I am trying to get CC to accept multiple email addresses but I do not seem
> to get it to work as it always says *Enter a valid email address*. I've
> searched for solutions, heard the new field *MultiValueField* but I also
> do not know how to use this in *forms.py*.
>
>
> 
>
> This is my *forms.py* code:
>
> from django import forms
> #  from django.forms.fields import MultiValueField
> from django.forms.widgets import EmailInput, TextInput
>
>
> class ComposeForm(forms.Form):
> email_to = forms.EmailField(label="To", widget=EmailInput(attrs
> ={"size": 76}))
> email_cc = forms.EmailField(
> label="CC",
> required=False,
> widget=EmailInput(attrs={"size": 76, "multiple": True}),
> )
> email_subject = forms.CharField(
> required=False, widget=TextInput(attrs
> ={"placeholder": "Subject", "size": 76})
> )
> email_message = forms.CharField(
> required=True, label="", widget=forms.Textarea(attrs
> ={"rows": 19, "cols": 78})
> )
>
>
> 
>
> And this is my *views.py* code:
>
> from django.shortcuts import render
>
> from .forms import ComposeForm
>
>
> def email_template(request):
> if request.method == "GET":
> form = ComposeForm()
> else:
> form = ComposeForm(request.POST)
> if form.is_valid():
> print(form)
> email_to = form.cleaned_data["email_to"]
> email_cc = form.cleaned_data["email_cc"]
> email_subject = form.cleaned_data["email_subject"]
> email_message = form.cleaned_data["email_message"]
>
> print("Emails:", email_cc)
> else:
> print("DEBUG:", form.errors)
> return render(request, "email_template.html", {"form": form})
>
>
> 
>
> And this is my *email_template.html* HTML:
>
> 
> 
> 
> 
> 
> 
> Home
> 
> 
> 
> {% csrf_token %}
> 
> {{ form.as_table }}
> 
> 
> 
> 
> 
>
>
>
> 
>
> Please tell me whether it's possible to have multiple email addresses in
> *email_cc* aka *CC* field in Django or do I have to use frontend
> frameworks for this one job or start using one for better practice (get
> used to using frameworks to build frontend side)?
>
> Please advise.
>
> Regards,
> Kristen
>
> sebasti...@gmail.com kirjutas teisipäev, 23. märts 2021 kl 22:24:01 UTC+2:
>
>> I have implement it as a bootstrap modal where a Form with fields are
>> shown. When User click submit the Page load New but If you don't want that
>> Page is load New you need a Ajax jquery submit to django
>>
>> Kristen  schrieb am Mo., 22. März 2021, 15:46:
>>
>>> Hello,
>>>
>>> Correct. I want to the user to click a button and have a form appear
>>> where the user can compose an email.
>>>
>>> Gmail uses javascript to render their form on top of the inbox because
 they don't want you to have to leave the inbox page. You probably don't
 have that requirement.

 Let me know if that works for you.

>>>
>>> I don't

Re: Create email template with the click of a button

2021-04-17 Thread Ryan Nowakowski
Sorry for the weird wording below. Speech to text isn't all is cracked up to 
be. Just one more point: the JSONField allows your to easily do queries on the 
individual email addresses if your need to.

On April 17, 2021 12:52:15 PM CDT, Ryan Nowakowski  wrote:
>I've had a similar issue in the past. With the latest version of Django
>pretty much each database back in now supports JSONField. So I created
>a custom field that inherits from JSON field to take a string of comma
>delimited values, split them by comma, validate each value
>individually, then store the whole thing in the database as a JSON
>list.
>
>In your case you'll use the email validator used by the EmailField to
>validate each value.
>
>On April 15, 2021 3:21:44 PM CDT, Smiley 
>wrote:
>>Hello,
>>
>>I have another problem. 
>>
>>I am trying to get CC to accept multiple email addresses but I do not
>>seem 
>>to get it to work as it always says *Enter a valid email address*.
>I've
>>
>>searched for solutions, heard the new field *MultiValueField* but I
>>also do 
>>not know how to use this in *forms.py*.
>>
>>
>>
>>This is my *forms.py* code:
>>
>>from django import forms
>>#  from django.forms.fields import MultiValueField
>>from django.forms.widgets import EmailInput, TextInput
>>
>>
>>class ComposeForm(forms.Form):
>>email_to = forms.EmailField(label="To", widget=EmailInput(attrs
>>={"size": 76}))
>>email_cc = forms.EmailField(
>>label="CC",
>>required=False,
>>widget=EmailInput(attrs={"size": 76, "multiple": True}),
>>)
>>email_subject = forms.CharField(
>>required=False, widget=TextInput(attrs
>>={"placeholder": "Subject", "size": 76})
>>)
>>email_message = forms.CharField(
>>required=True, label="", widget=forms.Textarea(attrs
>>={"rows": 19, "cols": 78})
>>)
>>
>>
>>
>>And this is my *views.py* code:
>>
>>from django.shortcuts import render
>>
>>from .forms import ComposeForm
>>
>>
>>def email_template(request):
>>if request.method == "GET":
>>form = ComposeForm()
>>else:
>>form = ComposeForm(request.POST)
>>if form.is_valid():
>>print(form)
>>email_to = form.cleaned_data["email_to"]
>>email_cc = form.cleaned_data["email_cc"]
>>email_subject = form.cleaned_data["email_subject"]
>>email_message = form.cleaned_data["email_message"]
>>
>>print("Emails:", email_cc)
>>else:
>>print("DEBUG:", form.errors)
>>return render(request, "email_template.html", {"form": form})
>>
>>
>>
>>And this is my *email_template.html* HTML:
>>
>>
>>
>>
>>
>>
>> 
>>Home
>>
>>
>>
>>{% csrf_token %}
>>
>>{{ form.as_table }}
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>Please tell me whether it's possible to have multiple email addresses
>>in 
>>*email_cc* aka *CC* field in Django or do I have to use frontend
>>frameworks 
>>for this one job or start using one for better practice (get used to
>>using 
>>frameworks to build frontend side)?
>>
>>Please advise.
>>
>>Regards,
>>Kristen
>>
>>sebasti...@gmail.com kirjutas teisipäev, 23. märts 2021 kl 22:24:01
>>UTC+2:
>>
>>> I have implement it as a bootstrap modal where a Form with fields
>are
>>
>>> shown. When User click submit the Page load New but If you don't
>want
>>that 
>>> Page is load New you need a Ajax jquery submit to django
>>>
>