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
Thinking of writing a tutorial. Would like some feedback.

As the title states, I'm thinking of writing a tutorial for Django. I would like to get some feedback and suggestions from the community regarding the content or if the tutorial itself is even a good idea.

The tutorial will be at an intermediate to advanced level. Though, I could write it in such a way that beginners will be able to understand it.

The idea is as follows:

## How to build a REST API in Django without a framework

The tutorial will guide a user through setting up a simple application with 3 - 5 models and make these models available through a RESTful API. Further tutorials may include how to utilize modern front end technologies to build a SPA if there is enough interest.

You may ask; "Why not use a REST framework like DRF or Tastypie?". Well, I do have an answer. They are not always the best choice for an application. In many cases, they are simply unnecessary. Here are the reasons why using a REST framework may not be right for an application:

### Bloat:

Most REST frameworks come with a lot of functionality that a given developer will not use. Some come with entire work flows that a developer will not use. Do you need all of that code that checks whether or not you are using session or JWT authentication if you're using Token authentication? Sending and receiving JSON data is a remarkably simple task. Django has a built in `JsonResponse` class that can do this for you. All you have to do is provide it a dict. The hard parts are (de)serialization, filtering, pagination, etc. Django has these facilities built in as well, but few people really know how to use them in conjunction with one another. My tutorial will aim to remedy this.

### Cognitive Load

Each REST framework has it's own idioms and best practices. It forces the developer to have to learn an entire new library. This forces the developer to pick a single framework and stick with it to become acquainted with it enough to use it effectively. A great example of this is how differently Tastypie and DRF handle permissions.

### Lack of simple flexibility

Most REST frameworks are completely customizable through overriding base classes. In DRF for instance, you can override the default serializer class to enforce your own custom behavior. The only problem is that knowing which classes to override and how requires an intimate knowledge of the framework or a healthy amount of research.

### Nested resources are a pain in the ass

DRF and Tastypie come with built in functionality to allow for the creation of nested resources. However, these never seem to work in exactly the way you want them to unless you override a class or method to make them do so. I have run into some nasty bugs with both of these frameworks when trying to create a nested resource. Django's ORM does a really great job of taking rows in your database and transforming them into simple objects. It really doesn't need to be difficult to nest them, especially when they have relations.

### Implicit non-declaritive classes

This is fine if you are tacking on an API endpoint as an afterthought. However, if the API is the core of the application, it's generally not such a great idea in my opinion. Both Tastypie and DRF allow the developer to simply provide the serializer/resource class with a model or queryset and then all the work happens behind the scenes. You can declare any fields that you want excluded and even have some options for enforcing simple validation and permissions. What if you need to enforce validation on a field to field basis? What if you need two similar fields to have very different validation methods? What if you need to validate fields based on other fields? What if you need to also perform permissions checks based on a certain field in conjunction with the requesting user? DRF and Tastypie both have answers to all of these questions, though they may not be very simple answers.

## Caveats

I am in no way trying to bash on Tastypie or DRF. They are both very useful and valuable tools if you need an all e
ncompassing solution that uses a majority of their features. I just believe that many applications would be less complex if they chose not to use these tools. Both DRF and Tastypie do a great job and you should still use them if you like to use them. I simply want to offer an alternative tool for your tool belt.

## The Tutorial

Ok, here's the meat and potatoes:

I want to write a tutorial that walks a reader through creating a REST API using Marhsmallow schemas and two custom view classes that inherit from `django.views.generic.View`. These view classes will handle pagination, filtering, object creation, object deletion, listing objects, getting a single object, and permissions. The schema will handle data validation and (de)serialization.

The tutorial will also cover creating nested resources in the form of the following: `/api/v1/users/<id>/posts/` which returns a paginated and filterable list of a user's posts.

I know many of you in this sub already know how to do this, but the tutorial will be written for those who do not.

So, if you could help me by answering any of the following questions, that would be fantastic:

* What would you like to see in a Django REST API tutorial in general?
* Have you tried to build a REST API in Django without a framework before?
* What are some API features you have found difficult to implement in the past?
* Have you used Marshmallow before?
* What are your thoughts on the method I am presenting here?
* Do you think this tutorial would be helpful for an intermediate Django developer?
* What kind of application do you think would be best to demonstrate this concept?
* Do you think this tutorial is a good idea? Why or why not?

I'm thinking I'll probably put the tutorial in a Github repo so others can help if they so wish. I am also building another website where the tutorial will be posted. I may also put it up on medium or something like that.

If you have any questions of your own, I'll be happy to answer them.

/r/django
https://redd.it/5js4yf
Pythonanywhere. All static files are working except in django admin?

What am I doing wrong here? I am trying to use the django-material library. It works locally but not on python anywhere. I have run `python manage.py collectstatic` I can't figure out how to get the server to point to the admin static files! Below is my settings.py

INSTALLED_APPS = (
'material',
'material.admin',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
......)

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.media',
],
},
},

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")

/r/djangolearning
https://redd.it/5ieh7z
Django-page-cms gets official inline page editing support
http://django-page-cms.readthedocs.io/en/latest/edit-content.html

/r/django
https://redd.it/5jl3ej
[AF] Flask Admin not displaying Foreign Key column

I'm building a blog with Flask. I use Flask-Admin for setting up an administrative interface. Everything is fine, but I cannot see the "tags" field in the Post form. This is defined as a relationship/FK in my Post model.

Please see below my model file and admin configuration. Appreciate any idea.

Thanks!

##### MODELS.PY
tags = db.Table('post_tag',
db.Column('tag_id', db.Integer, db.ForeignKey('tag.id')),
db.Column('post_id', db.Integer, db.ForeignKey('post.id'))
)


class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
date = db.Column(db.DateTime, default=datetime.utcnow)
title = db.Column(db.String(140))
slug = db.Column(db.String(200))
content = db.Column(db.Text)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
_tags = db.relationship('Tag', secondary=tags,
backref=db.backref('posts', lazy='dynamic'))

@observes('title')
def compute_slug(self, title):
self.slug = re.sub('[^\w]+', '-', title.lower())

@staticmethod
def newest(num):
return Post.query.order_by(desc(Post.date)).limit(num)

@property
def tags(self):
return ",".join([t.name for t in self._tags])

@tags.setter
def tags(self, string):
if string:
self._tags = [Tag.get_or_create(name) for name in string.split(',')]

def __repr__(self):
return "<Post '{}': '{}'>".format(self.title,self.date)


class Tag(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(25), nullable=False, unique=True, index=True)

@staticmethod
def get_or_create(name):
try:
return Tag.query.filter_by(name=name).one()
except:
return Tag(name=name)

@staticmethod
def all():
return Tag.query.all()
def __str__(self):
return self.name

def __repr__(self):
return self.name



##ADMIN.PY
admin = Admin(app, name='Admin', template_mode='bootstrap3')

class CKTextAreaWidget(TextArea):
def __call__(self, field, **kwargs):
if kwargs.get('class'):
kwargs['class'] += ' ckeditor'
else:
kwargs.setdefault('class', 'ckeditor')
return super(CKTextAreaWidget, self).__call__(field, **kwargs)

class CKTextAreaField(TextAreaField):
widget = CKTextAreaWidget()

class PostAdmin(sqla.ModelView):
form_overrides = dict(content=CKTextAreaField)
create_template = 'blog/ckeditor.html'
edit_template = 'blog/ckeditor.html'
form_excluded_columns = ('slug')
def is_accessible(self):
return current_user.is_authenticated
admin.add_view(PostAdmin(Post, db.session))

class TagAdmin(sqla.ModelView):
def is_accessible(self):
return current_user.is_authenticated
admin.add_view(TagAdmin(Tag, db.session))

class UserAdmin(sqla.ModelView):
def is_accessible(self):
return current_user.is_authenticated
admin.add_view(UserAdmin(User, db.session))

/r/flask
https://redd.it/5dbvbi
How to authenticate IMAP using oauth tokens?

I'm building a flask app that authenticates a user using Google's OAuth and then connects to gmail using IMAP for further email processing, however, i'm having trouble with the whole process.

My current code is as follows. I'm able to sign the user into google and access their user information, such as their email, and obtain an access token, however, i'm not sure where to go from there. Any/all help is greatly appreciated.

from flask import Flask, request, url_for, session, redirect, jsonify
from flask_oauth import OAuth
import json
import imaplib
import email
from bs4 import BeautifulSoup



GOOGLE_CLIENT_ID = 'MY ID'
GOOGLE_CLIENT_SECRET = 'MY SECRET'
REDIRECT_URI = '/authorized' # one of the Redirect URIs from Google APIs console

SECRET_KEY = 'Uber'
DEBUG = True

app = Flask(__name__)
app.secret_key = 'Uber'
oauth = OAuth()

google = oauth.remote_app('google',
base_url='https://www.google.com/accounts/',
authorize_url='https://accounts.google.com/o/oauth2/auth',
request_token_url=None,
request_token_params={'scope': 'https://www.googleapis.com/auth/userinfo.email',
'response_type': 'code'},
access_token_url='https://accounts.google.com/o/oauth2/token',
access_token_method='POST',
access_token_params={'grant_type': 'authorization_code'},
consumer_key=GOOGLE_CLIENT_ID,
consumer_secret=GOOGLE_CLIENT_SECRET)


@app.route('/')
def index():
access_token = session.get('access_token')
if access_token is None:
return redirect(url_for('login'))

access_token = access_token[0]
from urllib2 import Request, urlopen, URLError

headers = {'Authorization': 'OAuth '+access_token}
req = Request('https://www.googleapis.com/oauth2/v1/userinfo',
None, headers)
try:
res = urlopen(req)
except URLError, e:
if e.code == 401:
# Unauthorized - bad token
session.pop('access_token', None)
return redirect(url_for('login'))
return res.read()
j = json.loads(res.read())
email_address = j['email']
print email_address, access_token
return "Hello World"


@app.route('/login')
def login():
callback=url_for('authorized', _external=True)
return google.authorize(callback=callback)



@app.route(REDIRECT_URI)
@google.authorized_handler
def authorized(resp):
access_token = resp['access_token']
session['access_token'] = access_token, ''
return redirect(url_for('index'))


@google.tokengetter
def get_access_token():
return session.get('access_token')

def Uber_Cost(email_address, access_token):

# this function is completed, i'm just not sure how to authenticate IMAP using the oauth from above

/r/flask
https://redd.it/5dc81w
YuleLog - A terminal based Yule Log Fireplace for the Christmas Season. Made with Asciimatics!

I had the idea this morning to make a terminal based fireplace like the ones you see on youtube around this time of year. I looked around and didn't really see anything like it so I figured I'd go ahead with it.

I have always loved Asciimatics and remembered there's an awesome fire effect built in. There was a Christmas example, but it only had floating text.

I then found a photo of a log and converted it with https://picascii.com/ and spent some time tweaking the position a bit till it looked good on different console sizes.

It's up on Pypi so you can

`$ pip install YuleLog`

Not a lot of work at all but I'm pretty happy with the outcome and I hope you all are too :D

tl;dr - YuleLog -

Screenshot: https://github.com/Duroktar/YuleLog/blob/master/yule_log/screenshot.png

Github - https://github.com/Duroktar/YuleLog

Pypi - https://pypi.python.org/pypi

Release page - https://github.com/Duroktar/YuleLog/releases/tag/0.0.1

Love, Peace and Happiness to you all. Merry Christmas!!

edit: WORKS BEST IN FULLSCREEN!

/r/Python
https://redd.it/5k5o9l
Basic Model concept but I can't wrap my head around how to do it... Help, please.

Spending part of the Christmas holiday working on my first big Django project and I've hit a wall. I just can't wrap my head around how to do this so any help would be appreciated.

Custom user account (already made and set up) has a field which is used to reference an external API. Information related to the user (transaction list with details) is pulled from the external API (JSON) and saved to my database. Each user will have multiple transactions and each transaction will have multiple pieces of data associated with it that needs to be saved (to be later presented as a graph).

I can't wrap my head around how to set up the model to get the JSON data into my database. This includes creating the model and saving it.

I realize it's probably an elementary problem but if anyone can just list what I need to do to get this done I'd appreciate it. Most tutorials use data from forms (or blog entries) to illustrate how to save data to a database so I guess I need to know how to process and save data from a non-form outside source.

/r/django
https://redd.it/5k45xh
I implemented a module in python for oversampling skewed datasets. Would love to hear your thoughts on it!

It is an implementation of the ADASYN algorithm (link to paper: bit.ly/22KgAnP) and is designed to work along the scikit-learn API. I ran into the need of an oversampling module that focuses more on examples that are harder to classify, and I said, what the heck... I will implement it on my own. Feel free to use it in your projects and if you find any bugs just open an issue

https://github.com/stavskal/ADASYN

Don't forget to star if you find it useful ;)

/r/pystats
https://redd.it/44r9ye
Supervisord project: Python 2 to 3 porting help needed

Supervisor is a process supervisor used by hundreds of thousands (millions?) of production systems. It is used by people who don't even use Python as their application language.

supervisord is one of the most useful tools to run production systems.. and is more relevant when we look at the Docker world..Where is it is usually run as "init 1".

For us (and a crazy ton of systems people who dont even use python for application code), the "supervisor" incompatibility is a blocker to move to py3.

Unfortunately, I think the maintainers are handicapped by py3 expertise - https://github.com/Supervisor/supervisor/labels/python%203

>Supervisor has some major issues on Python 3. Many of these issues are encoding related as well so merging this one patch doesn't move the needle much. We need someone who has strong experience in Python 2/3 porting and is willing to spend a non-trivial amount of time looking at these bytes/strings issues together.


https://github.com/Supervisor/supervisor/pull/471#issuecomment-267793117

/r/Python
https://redd.it/5k1875
a Py3.6 fizzBuzz oneliner with f-strings

print(*map(lambda i: f"{'Fizz' * (not i%3)}{'Buzz' * (not i%5)}" or i, range(1,101)), sep='\n')



/r/Python
https://redd.it/5k7lnb