Skip to content

Commit

Permalink
Finally
Browse files Browse the repository at this point in the history
  • Loading branch information
wyyalt committed Oct 22, 2017
1 parent b5b0968 commit 3ae7835
Show file tree
Hide file tree
Showing 38 changed files with 506 additions and 373 deletions.
2 changes: 1 addition & 1 deletion .idea/maya.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

662 changes: 332 additions & 330 deletions .idea/workspace.xml

Large diffs are not rendered by default.

Binary file modified app01/__pycache__/__init__.cpython-35.pyc
Binary file not shown.
Binary file modified app01/__pycache__/apps.cpython-35.pyc
Binary file not shown.
Binary file modified app01/__pycache__/maya.cpython-35.pyc
Binary file not shown.
Binary file modified app01/__pycache__/models.cpython-35.pyc
Binary file not shown.
Binary file modified app01/__pycache__/views.cpython-35.pyc
Binary file not shown.
40 changes: 24 additions & 16 deletions app01/maya.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from django.utils.safestring import mark_safe
from app01 import models
from django.http import QueryDict
from types import FunctionType


class UserInfoAdmin(admin.MayaAdmin):
Expand Down Expand Up @@ -55,14 +54,22 @@ def checkbox(self,model_obj=None,is_header=False):
:return:
"""

select = """
"""
<div class="btn-group first-col" role="group">
<input type="button" class="btn btn-default btn-xs" value="全选">
<input type="button" class="btn btn-default btn-xs" value="反选">
<input type="button" class="btn btn-default btn-xs" value="取消">
</div>
"""

select = """
<div class="option-field">
<a style="cursor:pointer;margin-right:3px;" class="check"><span class="glyphicon glyphicon-check" aria-hidden="true"></span>全选</a>
<a style="cursor:pointer;margin-right:4px;" class="check"><span class="glyphicon glyphicon-share" aria-hidden="true"></span>反选</a>
<a style="cursor:pointer;" class="check"><span class="glyphicon glyphicon-unchecked" aria-hidden="true"></span>取消</a>
</div>
"""

if is_header:
return mark_safe(select)
else:
Expand All @@ -81,34 +88,35 @@ def multi_del(self,request):
return True


initial.text = "初始化"
initial.text = "请选择Action"
multi_del.text = "批量删除"

list_action = [initial,multi_del]

#优化
class SearchOption(object):
def __init__(self, field_or_func, is_multi):
self.field_or_func = field_or_func
self.is_multi = is_multi

@property
def is_func(self):
if isinstance(self.field_or_func, FunctionType):
return True
from maya.utils.filter import SearchOption

#搜索自定义函数
def user_city(self,option,request):
from maya.utils.filter import FilterList
queryset = models.UserCity.objects.filter(id__lt=3)
return FilterList(option,queryset,request)

list_filter = [
SearchOption('username',False),
SearchOption('user_group',False),
SearchOption('username',False,text_func_name="user_pass"),
SearchOption('user_group',True),
# SearchOption('user_city',False),
SearchOption(user_city,False),

]


admin.site.register(models.UserInfo,UserInfoAdmin)

class UserGroupAdmin(admin.MayaAdmin):
list_display = ('title',)
list_display = ('id','title',)
admin.site.register(models.UserGroup,UserGroupAdmin)

class UserCityAdmin(admin.MayaAdmin):
list_display = ('title',)
list_display = ('id','title',)
admin.site.register(models.UserCity,UserCityAdmin)
Binary file modified app01/migrations/__pycache__/0001_initial.cpython-35.pyc
Binary file not shown.
Binary file modified app01/migrations/__pycache__/0002_usergroup.cpython-35.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified app01/migrations/__pycache__/__init__.cpython-35.pyc
Binary file not shown.
7 changes: 7 additions & 0 deletions app01/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ class UserInfo(models.Model):
user_city = models.ForeignKey("UserCity",verbose_name="城市")
user_group = models.ManyToManyField("UserGroup",verbose_name="用户组")

def __str__(self):
return self.username


def user_pass(self):
return "%s-%s"%(self.username,self.password)

class UserGroup(models.Model):
title = models.CharField(max_length=32,verbose_name="用户组")

Expand Down
Binary file modified app02/__pycache__/__init__.cpython-35.pyc
Binary file not shown.
Binary file modified app02/__pycache__/apps.cpython-35.pyc
Binary file not shown.
Binary file modified app02/__pycache__/maya.cpython-35.pyc
Binary file not shown.
Binary file modified app02/__pycache__/models.cpython-35.pyc
Binary file not shown.
Binary file modified app02/migrations/__pycache__/0001_initial.cpython-35.pyc
Binary file not shown.
Binary file modified app02/migrations/__pycache__/__init__.cpython-35.pyc
Binary file not shown.
Binary file modified curd/__pycache__/__init__.cpython-35.pyc
Binary file not shown.
Binary file modified curd/__pycache__/settings.cpython-35.pyc
Binary file not shown.
Binary file modified curd/__pycache__/urls.cpython-35.pyc
Binary file not shown.
Binary file modified curd/__pycache__/wsgi.cpython-35.pyc
Binary file not shown.
Binary file modified db.sqlite3
Binary file not shown.
Binary file modified maya/__pycache__/__init__.cpython-35.pyc
Binary file not shown.
Binary file modified maya/__pycache__/apps.cpython-35.pyc
Binary file not shown.
Binary file modified maya/__pycache__/models.cpython-35.pyc
Binary file not shown.
Binary file modified maya/migrations/__pycache__/__init__.cpython-35.pyc
Binary file not shown.
Binary file modified maya/service/__pycache__/admin.cpython-35.pyc
Binary file not shown.
42 changes: 24 additions & 18 deletions maya/service/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,14 @@ def changelist_view(self,request):
base_add_url = reverse("{0}:{1}_{2}_add".format(self.site.namespace,self.app_label,self.model_name))
add_url = "{0}?{1}".format(base_add_url,param_dict.urlencode())


#分页功能
"""
分页功能
"""
from maya.utils import pagination
from copy import deepcopy

condition = {}
print(request.GET)

page_param_dict = deepcopy(request.GET)
page_param_dict._mutable = True
Expand All @@ -152,7 +154,9 @@ def changelist_view(self,request):
page_info = pagination.PageInfo(request.GET.get('page'),total_num,prefix_url,page_param_dict,2,10,)


#action
"""
Action
"""
action_list = []
for action in self.list_action:
action_list.append({
Expand All @@ -169,32 +173,34 @@ def changelist_view(self,request):

return redirect(base_url)


#组合搜索
list_filter = self.list_filter
if list_filter:
from django.db.models import ManyToManyField,ForeignKey
for item in list_filter:
if item.is_func:
pass
"""
组合搜索
"""
from django.db.models import ManyToManyField,ForeignKey
from maya.utils.filter import FilterList
def get_list_filter():
for option in self.list_filter:
if option.is_func:
filter_data = option.field_or_func(self,option,request)
else:
field = self.model_class._meta.get_field(item.field_or_func)
field = self.model_class._meta.get_field(option.field_or_func)
if isinstance(field,ForeignKey):
filter_data_list = field.rel.model.objects.all()
filter_data = FilterList(option,field.rel.model.objects.all(),request)
elif isinstance(field,ManyToManyField):
filter_data_list = field.rel.model.objects.all()
filter_data = FilterList(option,field.rel.model.objects.all(),request)
else:
print(field.objects.all())
filter_data_list = field.objects.all()
filter_data = FilterList(option, field.model.objects.all(),request)

yield filter_data

context ={
'data_list':self.model_class.objects.all()[page_info.start:page_info.end],
'data_list':self.model_class.objects.all(**condition)[page_info.start:page_info.end],
'list_display':self.list_display,
'maya_admin':self,
'add_url':add_url,
'page_index':page_info.page_index,
'action_list':action_list
'action_list':action_list,
'filter_data_list':get_list_filter()
}

return render(request,'change_list.html',context)
Expand Down
45 changes: 38 additions & 7 deletions maya/templates/change_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,41 @@
<meta charset="UTF-8">
<title>Change_List</title>
<link type="text/css" rel="stylesheet" href="/static/plugins/bootstrap-3.3.7/css/bootstrap.css">
<style>
.list_filter a {
display: inline-block;
border: 1px solid #dddddd;
padding: 5px 10px;
margin: 0 10px 10px 0;
}
.list_filter a.active {
background-color: #2aabd2;
color: white;
}
.filter_list {
padding-left: 70px;
}
.filter_all{
float: left;
}
</style>
</head>
<body>
<div class="container">

<h3>数据列表</h3>
<h1>数据列表</h1>
<!-- 组合搜索开始 -->
<div class="list_filter">
{% for queryset in filter_data_list %}
<div>
{% for item in queryset %}
{{ item }}
{% endfor %}
</div>
{% endfor %}
</div>
<!-- 组合搜索结束 -->
<!-- 组合搜索开始 -->
<form method="POST">
{% csrf_token %}
<div style="margin-bottom: 10px" class="clearfix">
Expand Down Expand Up @@ -46,20 +76,21 @@ <h3>数据列表</h3>
<script>

$(document).ready(function () {
$(".first-col").parent().css("width","123")
$(".option-field").parent().css("width","158")
});
$("input[type='button']").click(function () {
if($(this).val() === "全选"){
$(".check").click(function () {
if($(this).text() === '全选'){
$("input[type='checkbox']").prop('checked',true)
}
if($(this).val() === "反选"){
$("input[type='checkbox']").each(function () {
if($(this).text() === '反选'){
$("input[type='checkbox']").each(function (){
$(this).prop("checked",!$(this).prop("checked"))
})
}
if($(this).val() === "取消"){
if($(this).text() === '取消'){
$("input[type='checkbox']").prop('checked',false)
}

})
</script>
</html>
Binary file modified maya/templatetags/__pycache__/maya_form_tags.cpython-35.pyc
Binary file not shown.
Binary file modified maya/templatetags/__pycache__/maya_tags.cpython-35.pyc
Binary file not shown.
Binary file added maya/utils/__pycache__/filter.cpython-35.pyc
Binary file not shown.
Binary file modified maya/utils/__pycache__/pagination.cpython-35.pyc
Binary file not shown.
79 changes: 79 additions & 0 deletions maya/utils/filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from types import FunctionType
from django.utils.safestring import mark_safe
from copy import deepcopy
class SearchOption(object):
def __init__(self, field_or_func, is_multi,text_func_name=None,value_func_name=None):
self.field_or_func = field_or_func
self.is_multi = is_multi
self.text_func_name = text_func_name
self.value_func_name = value_func_name

@property
def is_func(self):
if isinstance(self.field_or_func, FunctionType):
return True

@property
def name(self):
if self.is_func:
return self.field_or_func.__name__
else:
return self.field_or_func

class FilterList(object):
def __init__(self,option,queryset,request):
self.option = option
self.queryset = queryset
self.param_dict = deepcopy(request.GET)
self.param_dict._mutable = True
self.path_info = request.path_info

def __iter__(self):
#生成all_url
yield mark_safe("<div class='filter_all'>")
if self.option.name in self.param_dict:
#pop当前option
pop_val = self.param_dict.pop(self.option.name)
all_url = "{0}?{1}".format(self.path_info,self.param_dict.urlencode())

#保留原搜索条件
self.param_dict.setlist(self.option.name,pop_val)
yield mark_safe("<a href='{0}'>全部</a>".format(all_url))
else:
all_url = "{0}?{1}".format(self.path_info, self.param_dict.urlencode())
yield mark_safe("<a class='active' href='{0}'>全部</a>".format(all_url))

yield mark_safe("</div><div class='filter_list'>")

from django.http.request import QueryDict

#list_url
for row in self.queryset:
param_dict = deepcopy(self.param_dict)
val = getattr(row,self.option.value_func_name)() if self.option.value_func_name else row.pk
val = str(val)
content = getattr(row,self.option.text_func_name)() if self.option.text_func_name else str(row)

active = False
if self.option.is_multi:
value_list = param_dict.getlist(self.option.name)

if val in value_list:
value_list.remove(val)
param_dict.setlist(self.option.name,value_list)
active = True
else:
param_dict.appendlist(self.option.name, val)
else:
option = param_dict.getlist(self.option.name)
if val in option:
active = True
param_dict[self.option.name] = val

list_url = "{0}?{1}".format(self.path_info,param_dict.urlencode())
if active:
tpl = "<a class='active' href='{0}'>{1}</a>".format(list_url,content)
else:
tpl = "<a href='{0}'>{1}</a>".format(list_url, content)
yield mark_safe(tpl)
yield mark_safe("</div>")

0 comments on commit 3ae7835

Please sign in to comment.