Ejemplo n.º 1
0
    def delete(self, document_id):
        """删除文档:
            包括删除文档记录和更新memer中的文档id
        """
        response_document = couch_db.get(r'/jsmm/%(document_id)s' %
                                         {"document_id": document_id})
        document = json.loads(response_document.body.decode('utf-8'))

        response_member = couch_db.get(r'/jsmm/%(member_id)s' %
                                       {"member_id": document["memberId"]})
        member = json.loads(response_member.body.decode('utf-8'))
        if document['docType'] == 'researchReport':
            member['researchReport'].remove(document['_id'])
        elif document['docType'] == 'unitedTheory':
            member['unitedTheory'].remove(document['_id'])
        elif document['docType'] == 'politicsInfo':
            member['politicsInfo'].remove(document['_id'])
        elif document['docType'] == 'propaganda':
            member['propaganda'].remove(document['_id'])
        # 删除Document记录
        couch_db.delete(r'/jsmm/%(document_id)s?rev=%(document_rev)s' % {
            'document_id': document_id,
            'document_rev': document['_rev']
        })
        # 更新member中的document的id
        couch_db.put(r'/jsmm/%(id)s' % {"id": member["_id"]}, member)

        # del_result = json.loads(response_del_document.body.decode('utf-8'))

        self.write({'success': 'true'})
Ejemplo n.º 2
0
    def put(self, member_id):
        """
        修改_id为member_id的member对象。
        """
        # 获得前台对象#
        member_updated = json.loads(self.request.body.decode('utf-8'))
        # 根据memeber_id,查询数据库中的memeber对象
        response = couch_db.get(r'/jsmm/%(id)s' % {'id': member_id})
        member = json.loads(response.body.decode('utf-8'))
        # 将前台数据赋予后台对象,然后将后台对象保存。
        member.update(member_updated)
        # 将document中的member数据更新

        query = {'keys': [member_id]}
        documents_response = couch_db.post(
            r'/jsmm/_design/documents/_view/by_memberid', query)
        documents = json.loads(documents_response.body.decode('utf-8'))

        for doc in documents['rows']:
            doc['value']['name'] = member['name']
            doc['value']['branch'] = member['branch']
            doc['value']['organ'] = member['organ']
            couch_db.put(r'/jsmm/%(id)s' %
                         {'id': doc['value']['_id']}, doc['value'])

        couch_db.put(r'/jsmm/%(id)s' % {'id': member_id}, member)
        response = {'success': 'true'}
        self.write(response)
Ejemplo n.º 3
0
    def put(self, document_id, attachment_name):
        """保存附件

        如果保存任何一个附件时出现错误,返回:{'success': 'false', 'errors': <errors>}
        如果全部保存成功,返回:{'success': 'true'}
        """
        # 前端file input的name属性应指定为"docs"。
        # 接收到的文件列表结构为:{'docs': [{'filename': <filename>, 'body': <body>,
        # 'content_type': <content_type>}]}
        docs = self.request.files['docs']
        # 格式为:[{'filename': <filename>, 'error': <error>, 'reason': <reason>}]
        errors = []
        for doc in docs:
            body = doc['body']
            content_type = doc['content_type']
            # 每次保存附件后,文档的rev都会变
            head_response = couch_db.head(r'/jsmm/%(document_id)s' %
                                          {'document_id': document_id})
            rev = head_response.headers.get_list('Etag')[0][1:-1]
            put_response = couch_db.put(
                r'/jsmm/%(document_id)s/%(attachment_name)s?rev=%(rev)s' % {
                    'document_id': document_id,
                    'attachment_name': attachment_name,
                    'rev': rev
                }, body, content_type)
            status_code = self._parse_header(put_response)[0]
            if status_code.startswith('4'):  # 400, 401, 404, 409
                error = json.loads(put_response.body.decode('utf-8'))
                errors.append({'filename': doc['filename']}.update(error))
        if len(errors) > 0:
            self.write({'success': 'false', 'errors': errors})
        else:
            self.write({'success': 'true'})
Ejemplo n.º 4
0
    def post(self, member_id, doc_type):

        response_member = couch_db.get(r'/jsmm/%(member_id)s' %
                                       {"member_id": member_id})
        member_in_db = json.loads(response_member.body.decode('utf-8'))

        docs = self.request.files['docs']
        doc_name = docs[0]['filename']

        document_info = {
            '_id': make_uuid(),
            'memberId': member_in_db['_id'],
            'name': member_in_db['name'],
            'type': 'document',
            'branch': member_in_db['branch'],
            'organ': member_in_db['organ'],
            'fileUploadTime': time.strftime("%Y-%m-%d", time.localtime()),
            'docType': doc_type,
            'fileName': doc_name
        }

        document_response = couch_db.post(r'/jsmm/', document_info)

        result = json.loads(document_response.body.decode('utf-8'))
        document_id = result["id"]

        if doc_type == 'researchReport':
            if 'researchReport' not in member_in_db:
                member_in_db['researchReport'] = []
            member_in_db['researchReport'].append(document_id)
        elif doc_type == 'unitedTheory':
            if 'unitedTheory' not in member_in_db:
                member_in_db['unitedTheory'] = []
            member_in_db['unitedTheory'].append(document_id)
        elif doc_type == 'politicsInfo':
            if 'politicsInfo' not in member_in_db:
                member_in_db['politicsInfo'] = []
            member_in_db['politicsInfo'].append(document_id)
        elif doc_type == 'propaganda':
            if 'propaganda' not in member_in_db:
                member_in_db['propaganda'] = []
            member_in_db['propaganda'].append(document_id)

        couch_db.put(r'/jsmm/%(id)s' % {"id": member_id}, member_in_db)

        return self.put(document_id, doc_name)
Ejemplo n.º 5
0
    def delete(self, tab_name):
        # 判断自定义tab名称是否已经存在
        tab_id = 'custab_' + ''.join(lazy_pinyin(tab_name))
        tab_selector = {"selector": {"tab_id": {"$eq": tab_id}}}

        response = couch_db.post(r'/jsmm/_find/', tab_selector)
        tabs = json.loads(response.body.decode('utf-8'))
        tab_list = tabs["docs"]
        # 判断是否存在名称为tab_name的tab
        if len(tab_list) <= 0:
            result = {"success": False, "content": u"该tab名称不存在,请重新输入!"}
        else:
            tab_target = tab_list[0]
            # 刪除member中的tab信息
            member_selector = {
                "selector": {
                    "$and": [{
                        "type": {
                            "$eq": "member"
                        }
                    }, {
                        tab_id: {
                            "$ne": "null"
                        }
                    }]
                }
            }

            member_response = couch_db.post(r'/jsmm/_find/', member_selector)
            member_list = json.loads(
                member_response.body.decode('utf-8'))["docs"]
            for member in member_list:
                del member[tab_id]
                couch_db.put(r'/jsmm/%(id)s' % {"id": member["_id"]}, member)

            # 刪除tab
            couch_db.delete(r'/jsmm/%(id)s?rev=%(rev)s' % {
                'id': tab_target["_id"],
                'rev': tab_target["_rev"]
            })
            result = {"success": True}
        self.write(result)
Ejemplo n.º 6
0
def docCallBack(file):
    loadDb = couch_db.get(r'/jsmm/%(id)s' % {'id': file['member_id']})
    memberInDb = json.loads(loadDb.body.decode('utf-8'))

    documentInfo = {
        '_id': make_uuid(),
        'memberId': memberInDb['_id'],
        'name': memberInDb['name'],
        'type': 'document',
        'branch': memberInDb['branch'],
        'organ': memberInDb['organ'],
        'fileUploadTime': newTime(),
        'fileName': file['filename'],
        'file_url': file['path'],
        'fileType': file['filename'].split('.')[-1]
    }

    # if file['doc_type'] == 'researchReport':
    #     documentInfo['docType'] = 'researchReport'
    # elif file['doc_type'] == 'departmentInfo':
    #     documentInfo['docType'] = 'departmentInfo'
    # elif file['doc_type'] == 'speechesText':
    #     documentInfo['docType'] = 'speechesText'
    # elif file['doc_type'] == 'speechesText':
    #     documentInfo['docType'] = 'speechesText'

    if file['doc_type'] == 'researchReport':
        if ('researchReport' not in memberInDb):
            memberInDb['researchReport'] = []
        memberInDb['researchReport'].append(documentInfo['_id'])
    elif file['doc_type'] == 'unitedTheory':
        if ('unitedTheory' not in memberInDb):
            memberInDb['unitedTheory'] = []
        memberInDb['unitedTheory'].append(documentInfo['_id'])
    elif file['doc_type'] == 'politicsInfo':
        if ('politicsInfo' not in memberInDb):
            memberInDb['politicsInfo'] = []
        memberInDb['politicsInfo'].append(documentInfo['_id'])
    elif file['doc_type'] == 'propaganda':
        if ('propaganda' not in memberInDb):
            memberInDb['propaganda'] = []
        memberInDb['propaganda'].append(documentInfo['_id'])

    documentResponse = couch_db.post(r'/jsmm/', documentInfo)
    memberResponse = couch_db.put(r'/jsmm/%(id)s' % {"id": file['member_id']},
                                  memberInDb)
Ejemplo n.º 7
0
def imageCallBack(file):
    loadDb = couch_db.get(r'/jsmm/%(id)s' % {'id': str(file['member_id'], encoding="utf-8")})
    memberInDb = json.loads(loadDb.body.decode('utf-8'))
    memberInDb['picture'] = '/image/'+file['filename']
    couch_db.put(r'/jsmm/%(id)s' % {"id": str(file['member_id'], encoding="utf-8")}, memberInDb)