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
Autopopulating grid layout?

I'm just getting into wagtail, getting back into webdev. Last time I was doing web stuff, there were no frameworks, so it is a bit of a learning curve for me.

I'm wondering if there is an easy way(i.e package or template) that would allow for a grid stye layout of stores/posts, which would auto update each time a new post was made, similar to the mroe popular news/blog sites like nerdist or whatever.

Or is this something that has to be built from scratch?

/r/django
https://redd.it/6clzqo
CSRF Verification Failure on Localhost?

This is an extension on my last topic https://www.reddit.com/r/django/comments/6c1q2c/added_google_recaptcha_and_i_get_ssl_certificate/. I didn't really get a solution to.

I now tried a different technique and get a different error message than from all the other stuff I've done before. I followed instructions from this website on setting up stunnel: http://userpath.co/blog/a-simple-way-to-run-https-on-localhost/

and then used this command:
`HTTPS=on python manage.py runserver`

I get this message: https://pastebin.com/JXWVWNJq

My contact form html template has a csrf token
https://pastebin.com/Pcnmw3pA

Of course the major question is what am I doing wrong?
Second question is can https be run on localhost?

If I try https://localhost:8000 I get "This site can’t provide a secure connection localhost sent an invalid response." So I understand that the 403 is saying that my website is unsecure, but the redirect to google recaptcha is.


/r/django
https://redd.it/6chqsz
Structuring Python code

A while back there was a link posted aimed at beginners on how to structure Python code, files etc... Does anyone happen to have a link discussing this?

Thanks in advance

/r/Python
https://redd.it/6cmjj4
Django with Docker and Pycharm

Is anyone using a Django project inside a docker container in the Pycharm IDE?
I tried it but I can't figure out how to setup Pycharm for using the python interpreter.
Maybe someone could help me?

/r/django
https://redd.it/6cmy62
Building a Reusable App -- question about templates

Hey Folks:

I'm considering building a reusable app that I could plug into a couple of my other projects. It would be an accounting app to handle all things accounting (revenues, expenses, invoicing, receivables, assets... etc etc).

My question is about the templates. If the app is to be as reusable as possible, how does one set up the templates? Should templates even be part of the app at all? Thinking of my other two projects that are already built, they use some custom CSS with specific classes, etc. so if I have templates as part of the reusable app, the templates would not follow those same standards. Likewise with my other project -- different CSS classes, etc.

So -- do templates normally form part of the reusable app? Should they be excluded? If there are parts of the app that need to be using templates, how should they be built so they are very reusable?

Any thoughts on that front?

/r/django
https://redd.it/6corzx
Whats the best way to create a weather heat map using python?

Send me your ideas on how to create a wether heat map as a little project.

/r/Python
https://redd.it/6cp9oy
Multiple projects, shared auth, single sign-on

Im trying to determine how to best structure multiple django apps on subdomains, so they can share user account data. One user account works across 2 subdomains, ideally single-sign on.

I understand we can use SESSION_COOKIE_DOMAIN to share the session cookie, that part seems ok.

And I understand we could simply use shared user tables, and set user model to point to table of another project. But it seems we would want to share code between the projects. Should we have a separate User app in a third repo?

I have seen django-mama-cas , does anyone have experience with it? Or any alternatives?

/r/django
https://redd.it/6cp4gw
New Wireless Sniffer Tool Written In Python to Be An Alternative to Airodump-ng
https://github.com/M1ND-B3ND3R/BoopSuite

/r/Python
https://redd.it/6csfws
Flask, uWsgi, nginx and errors logging

How do I turn on error logging.
I have a service starting my app.

[Unit]
Description=uWSGI instance to start the site
After=network.target

[Service]
User=me
Group=me-group
ExecStart=/usr/bin/env bash -c 'cd /www/web_projects/site_app/; uwsgi -- /www/web_projects/site_app/start_site.ini'
logger=/log/site_app_log.log
[Install]
WantedBy=multi-user.target

I have turned on logger. Which does create the file. But it does not write anything to the file.

nginx setup looks like the following.

location / {
proxy_pass http://127.0.0.1:8080;
}

When I start the site using the command line, I get the error writing to the console. But when the service starts the site I do not get any errors. Where and how to I log the errors?

Thanks.

/r/flask
https://redd.it/6cu9qd
Reusable snippets with dRY Views?

I have a model for News (Info). The News is displayed on a few pages in an accordion style div. So as to not violate DRY, I placed the HTML for News in a separate file and use the {% include %} tag. It is wonderful.

But what I can't figure out is keeping DRY with my views. To show the news I use (truncated):

def get_context_data(self, **kwargs):
context = super(About, self).get_context_data(**kwargs)
context['newslist'] = Info.objects.filter(priority=0).order_by('reported_on')
context['important_news'] = Info.objects.filter(priority=1).order_by('reported_on')
return context

For each page that uses News, I've been re-copying the code into that page's View. Is there a way to do this and still keep DRY? Or am I overreacting? Or both?~~~~

/r/djangolearning
https://redd.it/6cvgdx
What's everyone working on this week?

Tell /r/python what you're working on this week! You can be bragging, grousing, sharing your passion, or explaining your pain. Talk about your current project or your pet project; whatever you want to share.


/r/Python
https://redd.it/6cuvrk
[AF] Swagger API [connexion] behind nginx(using gunicorn) using allow rules and ufw(Uncomplicated Firewall) rules.

From: https://gist.github.com/Morabaraba/06403358de009ede60e1d842c48100b7

So I have a wonderful Swagger API implemented in flask using connexion and sqlalchemy.

It is only a B2B(Business to Business) app. At the moment I have a `allow` rule for each client in `nginx` and use ufw on the cli:

```
ufw allow proto tcp from 42.42.42.42 to any port 1334
```

But instead of changing conf files and executing ufw commands manually, I would like to automate it.

I feel a simple `user_ip_address` table with a restful crud api will work:

```py
class user_ip_address(db.Model):
__tablename__ = 'user_ip_address'
user_id = db.Column(db.Integer)
ip_address = db.Column(db.String)
```

You call `/api/set-user-ip-address` with a `authorization` header identifying the user and the ip address in the `POST` body.

But at this point do I do a ugly:

```py
system.os('ufw allow proto tcp from {} to any port 1334'.format(ip))
```

and

```py
system.os('echo "allow {}" >> /etc/nginx/sites-enabled/h4ck3webapp.conf && service nginx relead'.format(ip))
```

While it might work it feels like a hack of hacks.

I'm now going to dive a bit deeper into nginx allow rules and uncomplicated firewall docs.

But I just need to bounce the idea. Coding in a bubble is not fun.



/r/flask
https://redd.it/6cvo4j
[AF]WTForms-JSON Form Not Posting Data

[WTForms-JSON docs here.](https://wtforms-json.readthedocs.io/en/latest/)

This has been a bug from hell I cannot wrap my head around.

I have a simple API handler that works perfectly fine from Postman JSON POSTs.

However when I plug the data into a web form the JSON is all empty. For example:

162.249.161.234 - - [23/May/2017 10:52:59] "GET /?customer=test+customer HTTP/1.1" 200 -
{'customer': None}

My Flask is as follows:

@app.route('/')
def index():
form = SubmitWorkorderForm.from_json(request.json)
print form.data
return render_template('submitworkorder.html', form = form)

@app.route('/submitworkorder', methods=['POST'])
def submitworkorder():
form = SubmitWorkorderForm.from_json(request.json)
print form.data
if form.validate_on_submit():
customer = form.customer.data
return jsonify({'customer' : customer })

And my HTML:

<div class="container">
<form class="form-inline">
{{ form.customer }}
{{ form.hidden_tag() }}

<input type="submit" value="go"/>
</form>
<br>
<div id="successAlert" class="alert alert-success" role="alert" style="display:none;"></div>
<div id="errorAlert" class="alert alert-danger" role="alert" style="display:none;"></div>

Lastly my JS:

$(document).ready(function() {
console.log("form Data:", $('form').serialize())
$.ajax({
type: "POST",
dataType: "json",
url : "/submitworkorder",
data : $('form').serialize(),
success: function (data) {
if (data.error) {
$('#errorAlert').text(data.error).show();
$('#successAlert').hide();
}
else {
$('#successAlert').text(data.customer + 'successfully created.').show();
$('#errorAlert').hide();
}
}
});

});

I have been working on this for the better part of 8 hours with no progress at all. Any help is appreciated.

/r/flask
https://redd.it/6cv8gw