Tumblelog by Soup.io
Newer posts are loading.
You are at the newest post.
Click here to check if anything new just came in.

November 04 2017

Generic CBV Permissions Helper

A permission helper that can be included in any generic CBV, it uses the model attribute of the class to load all the permissions and tests a user can perform that action before dispatching the view.

October 19 2017

Soft hyphenation filters

Automatically hyphenate raw text or HTML code

October 15 2017

inline forms for deeply nested models

I had a problem trying to display my model would have a foreign key and that model would have one too etc.

Now there was a point I wanted to display the foreign keys of that field and display its fields and so on so forth.

This pretty much expands ...

October 09 2017

add encoding comment to project all python file

add comment "# coding:utf8# to all python file

October 07 2017

Do Not Escape Characters When Using dumpdata Command (Tested in Django 1.11)

Adds --pretty option to django ./manage.py dumpdata command, which produces pretty utf-8 strings instead of ugly unicode-escaped s**t:

$ ./manage.py dumpdata app.pricingplan --indent=1

  "pk": 1, 
  "model": "app.pricingplan", 
  "fields": {
   "name": "\u0411\u0430\u0437\u043e\u0432\u044b\u0439", 
  "pk": 2, 
  "model": "app.pricingplan", 
  "fields": {
   "name": "\u0425\u0443 ...

October 03 2017

CBV decorator from view function decorator

The Mixin approach for applying permissions to CBV views has 2 issues:

  1. you need to read the code to see what permissions are being applied to a View
  2. multiple bits of disparate code required to specify, e.g., a simple permission check

Here's an alternative -- takes any function decorator ...

September 21 2017

Convert tab indented string to dictionary


{u'Ogrenci': [u'Tum okullar', u'Lisans', u'Onlisans', u'Yuksek Lisans / Doktora', u'Ingilizce Hazirlik'], u'Ogretim Elemani': [u'Tum okullar', u'Lisans', u'Onlisans', u'Yuksek Lisans / Doktora', u'Ingilizce Hazirlik']}

September 17 2017

Automigrate, autocreatesuperuser if not User.count() in runserver and use manage.py:main as entrypoint

With this awesome manage.py, it will try to migrate first when called with runserver.

Also, this manege.py has super power to be used in your entry point as such:

entry_points = {
    'console_scripts': [
        # u haz a setup.py -> u haz importable module :) 
        'yourcommand = yourproject.manage:main',

Example output:

$ yourcommand ...

September 12 2017

Show Logs on Django Admin

Place the code any admin.py file in your registered apps

August 22 2017

Compact primary keys

If the primary key on a table is an integer, it can be desirable after a lot of adding and removing either during testing (as was my case) or otherwise, to tidy up the key space a little and see the primary keys run up as unbroken sequences from 1 ...

August 21 2017

update primary key (cascade to child tables and inherited models) - Django 1.11

An upgrade to the excellent snippet by variant at https://djangosnippets.org/snippets/2915/

Upgraded to work with Django 1.11.

August 07 2017

Django Auth with JWT

This is an example of Django auth with JWT tokens

August 05 2017

Django Online Now Users - Middleware

django online users, usage: {{ request.online_now }} or {{ request.online_now_ids }}, complete tutorial: https://python.web.id/blog/django-count-online-users/, this snippet forked from: https://gist.github.com/dfalk/1472104

August 04 2017

Image as base64 in django

Easy way to get base64 for the image to display in the template, eg: <img src="{{ post.get_cover_base64 }}">

July 27 2017

Dynamic DEBUG setting for PyCharm

  1. Next to the Play button you can Edit Configuration
  2. Click the green + on the left, add 2 "Django Server". Call one DEBUG, call the other RUN.
  3. Add "DEBUG False" to RUN in its "Environment variables"
  4. Add "DEBUG True" to DEBUG in its "Environment variables" (make sure the port # is different ...

July 19 2017

Place a file in an object from command-line (Python 3.x only)

You can place this snippet in management/commands/ and have it upload files from disk into objects in your database.

Usage: python manage.py upload_file_to_model myapp mymodel myfield 1 field_name /some/file/path

July 06 2017

Friendly ID(Python 3.X)

This is just modified version of friendly id for make this script compatible with python 3.x

Invoice numbers like "0000004" are a little unprofessional in that they expose how many sales a system has made, and can be used to monitor the rate of sales over a given time ...

June 21 2017

Mixin to set url kwargs to Class Based Views

This mixin is for class based views. Kwargs passed from url patterns to views via the dispatch method.

June 12 2017

Python at Instagram (PyCon 2017 Must-See Talk 2/6)

Part two of six in the 2017 edition of our annual PyCon Must-See Series, highlighting the talks our staff especially loved at PyCon. While there were many great talks, this is our team's shortlist.

One of the talks that I considered a must-see was a keynote presentation by Instagram employees Lisa Guo and Hui Ding about upgrading Python and Django.

This is something that many businesses know they "should" do, but think is too impractical "right now". Instagram performed an upgrade without any downtime and without slowing down the pipeline of new features. Caktus carries out Django upgrades as part of our managed hosting and upgrade protection services, so this talk was especially relevant to what we do as a company.

June 09 2017

CSV file to Django Models

Handling CSV (comma separated ...
Older posts are this way If this message doesn't go away, click anywhere on the page to continue loading posts.
Could not load more posts
Maybe Soup is currently being updated? I'll try again automatically in a few seconds...
Just a second, loading more posts...
You've reached the end.

Don't be the product, buy the product!