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
[AF] Authenticating internal APIs

I built a website using flask_login for handling 'logged in' permissions. This extension works great for protecting my views using `@login_required`, but it does nothing to protect my APIs (as far as I'm aware). How do I protect my internal API's? Do I integrate token-based JWT authentication?

EDIT: I had my decorators in the wrong order, sorry. `@login_required` wasn't working for me so I figured that it doesn't work with APIs for some reason. I actually discovered my problem: I placed my `l@login_required` decorator before my route handler, not after. Noob mistake.

/r/flask
https://redd.it/8akawc
What would you ask Guido if you had the chance ?

What if you had the chance to interview the creator of python himself ? What would you ask him?

/r/Python
https://redd.it/8amnrd
Serializing a dictionary that is not a model in Django restframework

I have read a few answers and posts about serializing an dictionary . But I still can't get it to work. Here is the problem. I do some data processing in django app , and it returns this dictionary (It has information about a quiz):

{101: {'subject': 'General-Intelligence', 'topics': ['Coding Decoding', 'Dice & Boxes', 'Statement & Conclusion', 'Venn Diagram', 'Mirror and Water Image', 'Paper Cutting and Folding', 'Clock/Time', 'Matrix', 'Direction', 'Blood Relation', 'Series Test', 'Ranking', 'Mathematical Operations', 'Alphabet Test', 'Odd one out', 'Analogy'], 'num_questions': 25, 'creator': 'Rajesh K Swami'}}

I want to serialize this dictionary. So what I have done is created a class for this dictionary. ie.

class PsudoTests:

def __init__(self,body):
self.body = body

Also a serializer:

class PsudoTestSerializer(serializers.Serializer):
body = serializers.DictField()

Now in api view :

class TestListView(generics.ListAPIView):
def get_serializer_class(self):
serializer = PsudoTestSerializer
def get_queryset(self):
me = Studs(self.request.user)
tests = me.toTake_Tests(1) # this method brings in the above dictionary that i want to serialize
p_test = PsudoTests(tests) #this creates an instance of class created above

return p_test

Now when i go to the url there is a key error:

"Got KeyError when attempting to get a value for field body on serializer PsudoTestSerializer.\nThe serializer field might be named incorrectly and not match any attribute or key on the dict instance.\nOriginal exception text was: 'body'."


/r/django
https://redd.it/8apaxg
Why use threads in Python?

I just learned about the GIL and from the looks of it, it appears even if we use threads in python, our program will take the same time if we just use a single thread due to GIL. If that's the case, using threads for performance isn't really the ideal use case in python, so why use it?

/r/Python
https://redd.it/8ao7lt
Is Django for freelancing worth it?

I am interested in learning webdev for freelancing. I've tried php, asp mvc and django. I heard from most people that php is the way to go but i prefer python and django.

So how is the freelance landscape for django and python?


/r/django
https://redd.it/8aqdxo
“The Cathedral and the Bazaar”: Mathematica vs. python, IPython and Jupyter, and their application to scientific papers
https://www.theatlantic.com/science/archive/2018/04/the-scientific-paper-is-obsolete/556676/

/r/Python
https://redd.it/8aqmzk
Pyforms: Embedding a pyplot into a pyForm using ControlMatplotlib

I have come across the ControlMatplotlib control, however I cannot for the life of me figure out how to pass the pyplot to this control.

There is little to no example code for the use of this control and the docs don't have example code.

Any insight is greatly appreciated.

/r/Python
https://redd.it/8apvjp
[AF] Does anyone use webtest? If so, why use it over flask's built-in test client?

I've been learning unit/integration testing following [sloria/cookiecutter-flask](https://github.com/sloria/cookiecutter-flask) as an example. I know this approach is opinionated, but does anyone know what value webtest brings over flask's built-in test client? I'm relatively new to flask but I suspect that the author simply prefers using webtest (perhaps since it's a more general tool).

/r/flask
https://redd.it/89tvn5
Thumbnail in form or formset

Hi everyone,

What's the easiest way to display the image that has been uploaded to the form in addition to or rather than just the filepath of the image within a form or formset. Really struggling with this one.

Thanks!


/r/django
https://redd.it/8as2gu
Allowing user to add details to flask-mail message

So I have a modal on my site from which I want my user to be able to enter their email, hit submit, and then the app to message me with the users email. So far I've got the site sending a message but I'm unsure how to get the users email into the message. Any help would be much appreciated.

My code for sending the email


@app.route('/sendmail/', methods=['POST','GET'])
def send_mail():
try:
#emaiil = request.form['email']
msg = Message("Access request",
sender="SENDING_EMAIL",
recipients=["MY_EMAIL"])
msg.body = "Users email is"
mail.send(msg)
flash('Request sent!')
return redirect(url_for('main'))
except Exception, e:
return(str(e))

My modal for which the user enters their email


<div class="modal-body">
<p>Password can be obtained from somewhere</p>
<form action="{% print url_for('send_mail') %}" class ="form- inline" method="post">
<input type = "email" class = "form-control" placeholder="Email" name = "email">
<a href="/sendmail" class ="btn btn-default">Submit</a>
<button type="button" class="btn btn-secondary" data- dismiss="modal modal2">Close</button>
</form>

The modal is called from here


@app.route("/", methods=['POST','GET'])
def main():
return render_template('main.html')

/r/flask
https://redd.it/8at9sg
How do you code in css without running collectstatic with each change?

How do you guys write css without collecting static on each change to see the effect?

Thanks!

/r/django
https://redd.it/8atv09
Domain Tree Structure Best Practices?

I'm curious as to what judgement criteria is generally used when organizing the placement of apps (or sections) of a website within the tree structure?

In particular, when is it best practice to place an app name in front of your domain www.appname.domain.com vs behind it www.domain/appname.com? Is this related more to SPA's?


/r/django
https://redd.it/8atrk0
Combinations in Python

Need help counting the different ways I can get 10 pairs of couples from a ballroom dance club with 10 males and 12 females to choose from. I was thinking importing itertools would be helpful.

/r/Python
https://redd.it/8auuzu