Ejemplo n.º 1
0
class AttachmentTest(AppCase):
    def setUp(self):
        self.setup_app()
        self.create_project()
        self.create_issue()

        self.attachment = Attachment(
                file_id='12345',
                project=self.test_project,
                parent=self.test_issue
        )
        self.attachment.save()

    def tearDown(self):
        self.teardown_dbs()

    def test_pre_delete(self):
        self.test_project.attachments.append(self.attachment)
        self.test_project.save()

        self.test_issue.attachments.append(self.attachment)
        self.test_issue.save()

        self.assertEqual(Attachment.objects.count(), 1)
        self.assertEqual(len(self.test_issue.attachments), 1)
        self.assertEqual(len(self.test_project.attachments), 1)

        self.attachment.delete()

        self.assertEqual(Attachment.objects.count(), 0)
        self.assertEqual(len(self.test_issue.attachments), 0)
        self.assertEqual(len(self.test_project.attachments), 0)
Ejemplo n.º 2
0
    def setUp(self):
        self.setup_app()
        self.create_project()
        self.create_issue()

        self.attachment = Attachment(
                file_id='12345',
                project=self.test_project,
                parent=self.test_issue
        )
        self.attachment.save()
Ejemplo n.º 3
0
    def process_attachments(self, files, parent):
        for k, file in files.iteritems():
            filename = file.filename
            if file and \
                    '.' in filename and \
                    filename.split('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']:
                filename = secure_filename(filename)
                filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
                file.save(filepath)

                file_id = self.upload_file(filepath)
                attachment = Attachment(file_id=file_id)
                attachment.project = self
                attachment.parent = parent
                attachment.save()

                self.attachments.append(attachment)
                self.save()

                parent.attachments.append(attachment)
                parent.save()
Ejemplo n.º 4
0
    def add(route_id, attach_type, key):
        from app.helpers.route import RouteHelper

        assert isinstance(route_id, ObjectId)
        assert RouteHelper.get(route_id)
        assert isinstance(attach_type, AttachType)

        # get book's info by isbn from douban
        if attach_type == AttachType.DOUBAN:
            r = requests.get(app.config['DOUBAN_ISBN_API'] + key)

            json_info = json.loads(r.text)
            if 'code' in json_info:
                assert json_info['code'] != 6000, u'未找到该书籍'

            info = r.text
        elif attach_type == AttachType.URL:
            req_paras = {
                'url': key,
                'key': app.config['EMBEDLY_KEY']
            }
            r = requests.get(app.config['EMBEDLY_EXTRACT_API'], params=req_paras)

            json_info = json.loads(r.text)
            assert json_info['type'] != 'error', u'无法获取相关网页信息'

            info = r.text
        else:
            info = key

        new_attach = Attachment()
        new_attach.route = route_id
        new_attach.atype = attach_type.value
        new_attach.info = info
        new_attach.save()

        f_route = RouteHelper.get(route_id)
        f_route.attached.append(new_attach.id)
        f_route.save()

        return new_attach
Ejemplo n.º 5
0
 def get_all():
     return Attachment.objects()
Ejemplo n.º 6
0
 def get(attach_id):
     return Attachment.objects(id=attach_id).first()
Ejemplo n.º 7
0
 def teardown_dbs(self):
     Issue.drop_collection()
     User.drop_collection()
     Project.drop_collection()
     Attachment.drop_collection()
     Comment.drop_collection()
Ejemplo n.º 8
0
def add_new_attach(type, size, chat_id):
    add_value(Attachment(type=type, size=size, chat_id=chat_id))
    commit_value()