Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
Using email for authentication (with username now at 150 characters)

In my current app I don't need users to have a username (although there is the slight possibility that it may be needed later) and I would prefer them to signup with an email instead.

The docs and also this blog post show how to have authentication without a username, but it looks pretty convulted to get things working.
https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#a-full-example
https://www.caktusgroup.com/blog/2013/08/07/migrating-custom-user-model-django/

However now that django has had the username limit lifted to 150 characters could I just set username field to email? and live with the outside risk of a long email

(- or do better approach of setting it to `sha256(user.email).hexdigest()[:150]`
and setting
```
class User(AbstractUser):
class Meta:
unique_together = ('email', )
```

from here: https://stackoverflow.com/questions/15597188/using-email-as-username-field-in-django-1-5-custom-user-model-results-in-fielder)

Is there any downside to this approach?


/r/django
https://redd.it/6ygh8m
Flask file upload pattern documentation mentions flash()

http://flask.pocoo.org/docs/0.12/patterns/fileuploads/#a-gentle-introduction

This part of the documentation has no explanation of what `flash` is.
Is it an error? A typo? Or am I missing something?

@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)


On a side note, I get an error when trying to upload the file on a dev/local instance.

File "/home/user/.virtualenvs/flask/lib/python3.6/site-packages/werkzeug/routing.py", line 1776, in build
raise BuildError(endpoint, values, method, self)
werkzeug.routing.BuildError: Could not build url for endpoint 'uploaded_file' with values ['filename']. Did you mean 'upload_file' instead?

^ In this case, I copy pasted the code from the corresponding pattern and executed it. The file *does* upload but doesn't throws this error.

/r/flask
https://redd.it/7r0rcf
Flask-SQLAlchemy - please explain how this code which did not supply the app instance to the SQLAlchemy() callable worked

Hello, in [the Housenet sample
app](https://github.com/0Hughman0/Housenet) we see a non-trivial use
of Flask-SQLAlchemy. Unlike the [doc
example](http://flask-sqlalchemy.pocoo.org/2.3/quickstart/#a-minimal-application)
and [the example included in the source
code](https://github.com/mitsuhiko/flask-sqlalchemy/blob/master/examples/hello.py),
this app moves the database definition into a separate module as one
would expect of a more complex application.

However, I am confused as to why he calls
`flask_sqlalchemy.SQLAlchemy()` without passing an `app` instance at
[line 9 of his database
code](https://github.com/0Hughman0/Housenet/blob/master/housenet/database/database.py#L9).

Next how is he able to call his SQLAlchemy model classes and [make
queries](https://github.com/0Hughman0/Housenet/blob/master/housenet/views.py#L22) such as:

housemate = Housemate.query.get(name)

even though `Housemate` was defined with the argumentless call to
`flask_sqlalchemy.SQLAlchemy()`?


I would appreciate answers to these questions and also any links to
other complex applications where the SQLAlchemy model is factored out
into a file outside of the main app file, although I think this one
example is quite complete.


/r/flask
https://redd.it/9peo9k
How are the model classes 'registered' with the database object in Flask SQLAlchemy i.e. how does the database object get to know of the existence of model classes?

I'm trying to setup SQLAlchemy to work with Flask, and I'm getting some errors.Looking back at the docs, and at the quickstart section, I cant figure out how exactly the tables corresponding to the model classes are being created in the database

Looking at the quickstart code at: https://flask-sqlalchemy.palletsprojects.com/en/2.x/quickstart/#a-minimal-application

How does the db.create_all() method actually create all tables? How does it read all models (not knowing their location - whether they're in a separate module or not) and create a table for each?

/r/flask
https://redd.it/fz5wsr
[Discussion] Juergen Schmidhuber: Critique of Turing Award for Drs. Bengio & Hinton & LeCun

I saw this [tweet](https://twitter.com/SchmidhuberAI/status/1276058162974666753) from Schmidhuber today:

*ACM lauds the awardees for work that did not cite the origins of the used methods. I correct ACM's distortions of deep learning history and mention 8 of our direct priority disputes with Bengio & Hinton.*

His new article: http://people.idsia.ch/~juergen/critique-turing-award-bengio-hinton-lecun.html

**Abstract.** ACM's 2018 A.M. Turing Award was about *deep learning* in artificial neural networks. ACM lauds the awardees for work based on algorithms and conceptual foundations first published by other researchers whom the awardees failed to cite (see [Executive Summary](http://people.idsia.ch/~juergen/critique-turing-award-bengio-hinton-lecun.html#exec) and Sec. [I](http://people.idsia.ch/~juergen/critique-turing-award-bengio-hinton-lecun.html#I), [V](http://people.idsia.ch/~juergen/critique-turing-award-bengio-hinton-lecun.html#V), [II](http://people.idsia.ch/~juergen/critique-turing-award-bengio-hinton-lecun.html#II), [XII](http://people.idsia.ch/~juergen/critique-turing-award-bengio-hinton-lecun.html#XII), [XIX](http://people.idsia.ch/~juergen/critique-turing-award-bengio-hinton-lecun.html#XIX), [XXI](http://people.idsia.ch/~juergen/critique-turing-award-bengio-hinton-lecun.html#XXI), [XIII](http://people.idsia.ch/~juergen/critique-turing-award-bengio-hinton-lecun.html#XIII), [XIV](http://people.idsia.ch/~juergen/critique-turing-award-bengio-hinton-lecun.html#XIV), [XX](http://people.idsia.ch/~juergen/critique-turing-award-bengio-hinton-lecun.html#XX), [XVII](http://people.idsia.ch/~juergen/critique-turing-award-bengio-hinton-lecun.html#XVII)). ACM explicitly mentions "astonishing" deep learning breakthroughs in 4 fields: [(A) speech recognition](http://people.idsia.ch/~juergen/critique-turing-award-bengio-hinton-lecun.html#A), [(B) natural language processing](http://people.idsia.ch/~juergen/critique-turing-award-bengio-hinton-lecun.html#B), [(C) robotics](http://people.idsia.ch/~juergen/critique-turing-award-bengio-hinton-lecun.html#C), [(D) computer vision](http://people.idsia.ch/~juergen/critique-turing-award-bengio-hinton-lecun.html#D), as well as "powerful" new deep learning tools in 3 fields: [(VII) medicine, astronomy, materials science](http://people.idsia.ch/~juergen/critique-turing-award-bengio-hinton-lecun.html#VII). Most of these breakthroughs and tools, however, were directly based on the results of my own labs in the past 3 decades (e.g., Sec. [A](http://people.idsia.ch/~juergen/critique-turing-award-bengio-hinton-lecun.html#A), [B](http://people.idsia.ch/~juergen/critique-turing-award-bengio-hinton-lecun.html#B), [C](http://people.idsia.ch/~juergen/critique-turing-award-bengio-hinton-lecun.html#C), [D](http://people.idsia.ch/~juergen/critique-turing-award-bengio-hinton-lecun.html#D), [VII](http://people.idsia.ch/~juergen/critique-turing-award-bengio-hinton-lecun.html#VII), [XVII](http://people.idsia.ch/~juergen/critique-turing-award-bengio-hinton-lecun.html#XVII), [VI](http://people.idsia.ch/~juergen/critique-turing-award-bengio-hinton-lecun.html#VI), [XVI](http://people.idsia.ch/~juergen/critique-turing-award-bengio-hinton-lecun.html#XVI)). I correct ACM's distortions of deep learning history (e.g., Sec. [II](http://people.idsia.ch/~juergen/critique-turing-award-bengio-hinton-lecun.html#II), [V](http://people.idsia.ch/~juergen/critique-turing-award-bengio-hinton-lecun.html#V), [XX](http://people.idsia.ch/~juergen/critique-turing-award-bengio-hinton-lecun.html#XX), [XVIII](http://people.idsia.ch/~juergen/critique-turing-award-bengio-hinton-lecun.html#XVIII)) and also mention 8 of our direct priority disputes with Bengio & Hinton (Sec.

/r/MachineLearning
https://redd.it/hfi3hz
I made a Mastermind like game

Hi

This is my first code ever so please be kind. I just finished some online tutorial and wanted to test what I remembered from it. Thank you in advance for your time.

#game mastermind
#code breaking game

#X = not in phrase
#O = in phrase and in the right place
#A = in phrase but at the wrong place

import random

print('Mastermind !')

maxnumber=input('Input your maximum number between 4 and 9 (9 being the hardest) : ')

phrase = [random.randint(1,int(maxnumber))]
for x in range(3):
a = random.randint(1,int(maxnumber))
while a in phrase:
a = random.randint(1,int(maxnumber))
phrase.append(a)

playerphrase= 0000
counter = 0

maxcounter=input('How many turns do you want to give yourself (3 is not doable 4 is hard and it gets easier

/r/Python
https://redd.it/k1fmq5
Django Rest Framework Custom manager or instance.save(), how to insert a new record?

I have a custom user model same as this one: Django's Documentation and it has a custom manager that contains create_user method (that also uses instance.save() method), however in UserSerializer(serializers.ModelSerializer) I can define create method this way (1):

def create(self, validateddata):
user = User(
email=validated
data'email',
username=validateddata['username']
)
user.set
password(validateddata['password'])
user.save()

return user

or this way (2):

def create(self, validated
data):
User.objects.createuser(
email=validated
data'email',
username=validateddata['username'],
password=validated
data'password'
)

return

/r/djangolearning
https://redd.it/z2z5oe
React/Flask google oauth2 problem

Hello, i am trying to implement google oauth2 in my flask/react app, and my problem is that my session cookies are lost after i redirect(in the callback route) to my frontend page. I would like to know any way to work around it or if you know about better option how to implement google oauth. Thank you.

Relevant code:

clientsecretsfile = os.path.join(pathlib.Path(file).parent, "clientsecret.json")

flow = Flow.from
clientsecretsfile(
clientsecretsfile=clientsecretsfile,
scopes="https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/userinfo.email", "openid",
redirecturi="http://127.0.0.1:5000/callback"
)

def login
isrequired(function): #a function to check if the user is authorized or not
def wrapper(*args, **kwargs):
print(session)
if "google
id" not in session: #authorization required
return abort(401)


/r/flask
https://redd.it/zpajdn
Add a profile model to a custom user model

How do I add a profile model to a custom user model?

I have a working custom user model `A`. It is working in that sense that after registration, a database entry is established. Now, I want to extend `A` with a profile model `AProfile`. For ordinary user models, one uses `user = models.OneToOneField(User, on_delete=models.CASCADE)`, hence I typed `user = models.OneToOneField(A, on_delete=models.CASCADE)` but it doesn't work. Accessing the profile via `request.user.aprofile` yields `RelatedObjectDoesNotExist: A has no aprofile.` What am I doing wrong?

I basically followed: https://docs.djangoproject.com/en/5.0/topics/auth/customizing/#a-full-example but replaced/inserted the information I need, fi. removing `date_of_birth` and adding `first_name`, `last_name`.

Custom user models are hard and unintuitive.

Edit: The Code:
```python
from django.db import models
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser


class SurffreundUserManager(BaseUserManager):
def create_user(self, email, first_name, last_name, password=None):
"""
Creates and saves a User with the given email, date of
birth and password.
"""
if not email:
raise ValueError("Users must have an email address")



/r/django
https://redd.it/1ecyor7