Skip to content

Commit fc69bc9

Browse files
author
鸿濡
committed
combo获取值失败,把数据做成类
1 parent bc4c161 commit fc69bc9

14 files changed

+208
-105
lines changed

apps/db/db_engin.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
from sqlalchemy import create_engine
55
import platform
66
import os
7-
from apps.project.project_models import ProjectBase
8-
from apps.staff.staff_models import UserBase, UserPorject
7+
from db_models import Base
98

109
DB_NAME = "staff.db"
1110

@@ -23,6 +22,4 @@
2322
engine = create_engine("sqlite:///%s/staff.db" % DB_PATH, echo=False)
2423
# 如果表不存在,则创建表
2524
if not os.path.exists(DB_FILE_PATH):
26-
UserBase.metadata.create_all(engine)
27-
ProjectBase.metadata.create_all(engine)
28-
UserPorject.metadata.create_all(engine)
25+
Base.metadata.create_all(engine)

apps/db/db_models.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#coding=cp936
2+
__author__ = 'admin'
3+
4+
5+
from sqlalchemy import Table, MetaData, Column, ForeignKey, Integer, String, Unicode, DateTime, create_engine
6+
from sqlalchemy.ext.declarative import declarative_base
7+
from sqlalchemy.orm import relationship, backref
8+
9+
Base = declarative_base()
10+
11+
class User(Base):
12+
__tablename__ = 'users'
13+
14+
id = Column(Integer, primary_key=True)
15+
name = Column(String)
16+
employee_id = Column(String)
17+
phone_number = Column(String)
18+
tel_number = Column(String)
19+
birth_date = Column(String)
20+
title = Column(String)
21+
position = Column(String)
22+
education = Column(String)
23+
userproject = relationship('UserProject')
24+
25+
class Project(Base):
26+
__tablename__ = 'projects'
27+
28+
id = Column(Integer, primary_key=True)
29+
name = Column(String)
30+
search_id = Column(String)
31+
source_place = Column(String)
32+
main_designer = Column(String)
33+
design_all = Column(String)
34+
responsible_man = Column(String)
35+
start_time = Column(String)
36+
end_time = Column(String)
37+
attendee = Column(String)
38+
userproject = relationship('UserProject')
39+
40+
41+
class UserProject(Base):
42+
__tablename__ = 'users_projects'
43+
44+
id = Column(Integer, primary_key=True)
45+
user_id = Column(Integer, ForeignKey('users.id'))
46+
project_id = Column(Integer, ForeignKey('projects.id'))
47+
#user = relationship("users", backref="users_projects")
48+
#project = relationship("projects", backref="users_projects")

apps/device/device_models.py

Whitespace-only changes.

apps/graph/__init__.py

Lines changed: 0 additions & 1 deletion
This file was deleted.
File renamed without changes.

apps/project/project_manager.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#coding=cp936
22
__author__ = 'admin'
33

4-
from project_models import Project
4+
from apps.db.db_models import Project
55
from apps.db.db_session import session
66
from apps.utils.tools import ToolsManager
7+
from apps.staff.staff_manager import StaffManager
78

89
class ProjectManager(object):
910
@staticmethod
@@ -19,9 +20,7 @@ def add_project(user_obj):
1920

2021
@staticmethod
2122
def delete_project(ids_list):
22-
items = session.query(Project).filter(Project.id.in_(ids_list))
23-
for item in items:
24-
session.delete(item)
23+
session.query(Project).filter(Project.id.in_(ids_list)).delete(synchronize_session=False)
2524
session.commit()
2625

2726
@staticmethod
@@ -48,16 +47,18 @@ def search_project():
4847
def updata_project(dic):
4948
item = session.query(Project).filter(Project.id == dic.get('id')).one()
5049
item.name = dic.get('name')
51-
item.search_id=dic.get('search_id')
52-
item.source_place=dic.get('source_place')
53-
item.main_designer=dic.get('main_designer')
54-
item.design_all=dic.get('design_all')
55-
item.responsible_man=dic.get('responsible_man')
56-
item.attendee=dic.get('attendee')
57-
item.start_time=dic.get('start_time')
58-
item.end_time=dic.get('end_time')
50+
item.search_id = dic.get('search_id')
51+
item.source_place = dic.get('source_place')
52+
item.main_designer = dic.get('main_designer')
53+
item.design_all = dic.get('design_all')
54+
item.responsible_man = dic.get('responsible_man')
55+
item.attendee = dic.get('attendee')
56+
item.start_time = dic.get('start_time')
57+
item.end_time = dic.get('end_time')
5958
session.add(item)
6059
session.commit()
60+
StaffManager.delete_staff_project_by_project_ids([item.id])
61+
StaffManager.add_staff_project(item.id, dic.get('attendee_ids'))
6162

6263
@staticmethod
6364
def get_one_item_by_id(item_id):

apps/project/project_models.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

apps/project/project_page.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#coding=cp936
22
__author__ = 'admin'
33

4+
import copy
45
from PyQt4 import QtGui, QtCore
5-
from project_models import Project
6+
from apps.db.db_models import Project
67
from project_manager import ProjectManager
78
from apps.utils.tools import ToolsManager
89
from apps.staff.staff_manager import StaffManager
10+
from apps.utils import constant
911

1012

1113
class project_tab(QtGui.QWidget):
@@ -53,7 +55,7 @@ class MyTable(QtGui.QTableWidget):
5355
def __init__(self,parent=None):
5456
super(MyTable,self).__init__(parent)
5557

56-
head_labels = [u'ID', u'工程名称',u'检索号',u'来源',u'主设人',u'设总',u'负责主工',u'参加人员',u'开始时间',u'结束时间',u'工程所用仪器']
58+
head_labels = constant.PROJECT_COLUMN
5759
self.setColumnCount(len(head_labels))
5860
self.setRowCount(0)
5961
self.setHorizontalHeaderLabels(head_labels)
@@ -63,9 +65,11 @@ def add_project(self):
6365
dialog = Dialog()
6466
if dialog.exec_():
6567
dic = dialog.get_add_datas()
68+
attendee_ids = copy.deepcopy(dic['attendee_ids'])
69+
del dic['attendee_ids']
6670
user = Project(**dic)
6771
project = ProjectManager.add_project(user)
68-
StaffManager.add_staff_project(project.id, dic['attendee'])
72+
StaffManager.add_staff_project(project.id, attendee_ids)
6973
self.refresh_project()
7074
dialog.destroy()
7175

@@ -81,8 +85,8 @@ def refresh_project(self):
8185
for meta in datas_meta:
8286
newItem = QtGui.QTableWidgetItem(unicode(meta))
8387
self.setItem(i,j,newItem)
84-
j = j + 1
85-
i = i + 1
88+
j += 1
89+
i += 1
8690

8791
def delete_project(self):
8892
indexes = self.selectionModel().selectedRows()
@@ -96,6 +100,7 @@ def delete_project(self):
96100
id_text = self.item(index.row(), 0).text()
97101
ids_list.append(int(id_text))
98102
ProjectManager.delete_project(ids_list)
103+
StaffManager.delete_staff_project_by_project_ids(ids_list)
99104
self.refresh_project()
100105
else:
101106
pass
@@ -259,11 +264,17 @@ def get_add_datas(self):
259264
datas_dic['start_time'] = unicode(self.start_time_edit.text())
260265
datas_dic['end_time'] = unicode(self.end_time_edit.text())
261266
attendee_ids = ''
267+
attendee_names = ''
262268
for row_num in range(self.listWidgetB.count()):
263269
row_text = unicode(self.listWidgetB.item(row_num).text())
264-
staff_id = StaffManager.get_one_item_by_name(row_text).id
265-
attendee_ids += str(staff_id)+","
266-
datas_dic['attendee'] = attendee_ids.rstrip(',')
270+
staff = StaffManager.get_one_item_by_name(row_text)
271+
staff_id = staff.id
272+
staff_name = staff.name
273+
attendee_ids += str(staff_id) + ","
274+
attendee_names += staff_name + ","
275+
276+
datas_dic['attendee_ids'] = attendee_ids.rstrip(',')
277+
datas_dic['attendee'] = attendee_names.rstrip(',')
267278
return datas_dic
268279

269280
def add_fun(self):

apps/staff/staff_manager.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#coding=cp936
22
__author__ = 'admin'
33

4-
from staff_models import User, UserPorject
4+
from apps.db.db_models import User, UserProject
55
from apps.db.db_session import session
66
from apps.utils.tools import ToolsManager
7+
from sqlalchemy import and_, or_
78

89
class StaffManager(object):
910
@staticmethod
@@ -18,9 +19,7 @@ def add_staff(user_obj):
1819

1920
@staticmethod
2021
def delete_staff(ids_list):
21-
items = session.query(User).filter(User.id.in_(ids_list))
22-
for item in items:
23-
session.delete(item)
22+
session.query(User).filter(User.id.in_(ids_list)).delete(synchronize_session=False)
2423
session.commit()
2524

2625
@staticmethod
@@ -64,15 +63,30 @@ def get_one_item_by_name(item_name):
6463

6564
@staticmethod
6665
def add_staff_project(project_id, attendee_ids_str):
67-
attendee_ids_list = attendee_ids_str.split(',')
68-
for id in attendee_ids_list:
69-
item = UserPorject(user_id=id, project_id=project_id)
70-
session.add(item)
66+
if attendee_ids_str != '':
67+
attendee_ids_list = attendee_ids_str.split(',')
68+
for id in attendee_ids_list:
69+
item = UserProject(user_id=id, project_id=project_id)
70+
session.add(item)
71+
session.commit()
72+
73+
@staticmethod
74+
def delete_staff_project_by_project_ids(project_ids):
75+
session.query(UserProject).filter(UserProject.project_id.in_(project_ids)).delete(synchronize_session=False)
7176
session.commit()
7277

78+
@staticmethod
79+
def delete_staff_project_by_staff_ids(user_ids):
80+
session.query(UserProject).filter(UserProject.user_id.in_(user_ids)).delete(synchronize_session=False)
81+
session.commit()
82+
83+
@staticmethod
84+
def delete_staff_project_by_staff_project_id(user_id, project_id):
85+
session.query(UserProject).filter(and_(UserProject.user_id == user_id, UserProject.project_id == project_id)).delete()
86+
7387
@staticmethod
7488
def search_staff_project(staff_name):
7589
staff_id = StaffManager.get_one_item_by_name(staff_name).id
76-
items= session.query(UserPorject).filter(UserPorject.user_id == staff_id)
90+
items= session.query(UserProject).filter(UserProject.user_id == staff_id).all()
7791
for item in items:
7892
print item.user_id, item.project_id

apps/staff/staff_models.py

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)