Skip to content

Instantly share code, notes, and snippets.

View bluethon's full-sized avatar
🤣
free

Haibo Jia bluethon

🤣
free
  • Beijing, China
View GitHub Profile
@bluethon
bluethon / create_group_and_permission.py
Last active September 11, 2016 06:47
group user get_or_create contenttype
# http://stackoverflow.com/questions/22250352/in-django-how-do-you-programmatically-create-a-group-with-permissions
from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType
from api.models import Project
new_group, created = Group.objects.get_or_create(name='new_group')
# Code to add permission to group ???
ct = ContentType.objects.get_for_model(Project)
# Now what - Say I want to add 'Can add project' permission to new_group?
permission = Permission.objects.create(codename='can_add_project', name='Can add project', content_type=ct)
@bluethon
bluethon / model_define_groups.py
Last active September 11, 2016 07:00
group attribute demo
class some():
groups = models.ManyToManyField(
Group,
verbose_name='组',
blank=True,
# help_text='The groups this user belongs to. A user will get all '
# 'permissions granted to each of their groups.',
related_name="user_set",
related_query_name="user",
)
@bluethon
bluethon / check_user_group.py
Created September 11, 2016 07:33
check if user have the group
if user.groups.filter(name=group_name).count():
pass
@bluethon
bluethon / pip.conf
Created September 30, 2016 09:45
修改pip源 ~/.pip/pip.conf
[global]
timeout = 6000
index-url = https://pypi.douban.com/simple
@bluethon
bluethon / Find_full_path_of_python_interpreter
Created October 4, 2016 03:34
python中获取解释器路径
# http://docs.python.org/library/sys.html
import sys
print(sys.executable)
@bluethon
bluethon / 0_reuse_code.js
Created October 15, 2016 06:10
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
from django.http import QueryDict
def url_with_querystring(path, query_dict):
""" URL查询参数拼接 """
qdict = QueryDict('', mutable=True)
qdict.update(query_dict)
return path + '?' + qdict.urlencode()
url_dict = {
'course': course,
@bluethon
bluethon / django_execute_sql.py
Created October 16, 2016 04:58
django执行SQL语句
from django.db import connection, transaction
cursor = connection.cursor() # 获得一个游标(cursor)对象
cursor.execute('update order_invoiceinfo set type = 1')
transaction.commit() # 提交到数据库
# # 更新操作
# cursor.execute('update other_other2 set name ="李四" where id=%s', [3]) # 执行sql语句
# transaction.commit_unless_managed() # 提交到数据库
# # 查询操作
# cursor.execute('select * from other_other2 where id>%s', [1])
RAVEN_CONFIG = {
'dsn': '<your-dsn-here>',
}
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'root': {
'level': 'WARNING',
'handlers': ['sentry'],
# Changing field 'MyTable.associated'
# db.alter_column(u'data_mytable', 'associated',
# self.gf('django.db.models.fields.IntegerField')()
# )
db.execute(
'ALTER TABLE "data_mytable" '
'ALTER COLUMN "associated" DROP DEFAULT, '
'ALTER COLUMN "associated" DROP NOT NULL, '
'ALTER COLUMN "associated" TYPE INTEGER '
'USING '