X Tutup
Skip to content

Commit 4d5bd28

Browse files
committed
Updated attachable and added test for file upload.
1 parent b4dd254 commit 4d5bd28

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

README.rst

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,22 @@ Review results for batch operation:
245245

246246
Attachments
247247
----------------
248+
See `Attachable documentation`_ for list of valid file types
249+
Attaching a note:
248250

249-
Adding attachments (See `Attachable documentation`_ for list of valid file types):
251+
::
252+
253+
attachment = Attachable()
254+
255+
attachable_ref = AttachableRef()
256+
attachable_ref = .EntityRef = entity.to_ref()
257+
258+
attachment.AttachableRef.append(attachable_ref)
259+
260+
attachment.Note = 'This is a note'
261+
attachment.save(qb=qb)
262+
263+
Attaching a file:
250264

251265
::
252266

quickbooks/objects/attachable.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,21 @@ def to_ref(self):
4949
ref.type = self.qbo_object_name
5050
ref.value = self.Id
5151
return ref
52+
53+
def save(self, qb=None):
54+
if not qb:
55+
qb = QuickBooks()
56+
57+
if self.Id and self.Id > 0:
58+
json_data = qb.update_object(self.qbo_object_name, self.to_json(), _file_path=self._FilePath)
59+
else:
60+
json_data = qb.create_object(self.qbo_object_name, self.to_json(), _file_path=self._FilePath)
61+
62+
if self.FileName:
63+
obj = type(self).from_json(json_data['AttachableResponse'][0]['Attachable'])
64+
else:
65+
obj = type(self).from_json(json_data['Attachable'])
66+
67+
self.Id = obj.Id
68+
69+
return obj

tests/integration/test_attachable.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,21 @@ def test_update_note(self):
4646

4747
query_attachable = Attachable.get(attachable.Id, qb=self.qb_client)
4848
self.assertEquals(query_attachable.Note, "Note updated on {}".format(self.time.strftime("%Y-%m-%d %H:%M:%S")))
49+
50+
def test_create_file(self):
51+
attachable = Attachable()
52+
53+
vendor = Vendor.all(max_results=1, qb=self.qb_client)[0]
54+
55+
attachable_ref = AttachableRef()
56+
attachable_ref.EntityRef = vendor.to_ref()
57+
attachable.AttachableRef.append(attachable_ref)
58+
59+
attachable.FileName = 'TestFileName'
60+
attachable._FilePath = __file__
61+
attachable.ContentType = 'application/txt'
62+
63+
attachable.save(qb=self.qb_client)
64+
query_attachable = Attachable.get(attachable.Id, qb=self.qb_client)
65+
66+
self.assertEquals(query_attachable.AttachableRef[0].EntityRef.value, vendor.Id)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import unittest
2+
3+
from quickbooks import QuickBooks
4+
from quickbooks.objects.attachable import Attachable
5+
6+
7+
class AttachableTests(unittest.TestCase):
8+
def test_unicode(self):
9+
attachable = Attachable()
10+
attachable.FileName = "test"
11+
12+
self.assertEquals(str(attachable), "test")
13+
14+
def test_to_ref(self):
15+
attachable = Attachable()
16+
attachable.FileName = "test"
17+
attachable.Id = 12
18+
19+
ref = attachable.to_ref()
20+
21+
self.assertEquals(ref.name, "test")
22+
self.assertEquals(ref.type, "Attachable")
23+
self.assertEquals(ref.value, 12)
24+
25+
def test_valid_object_name(self):
26+
attachable = Attachable()
27+
client = QuickBooks()
28+
result = client.isvalid_object_name(attachable.qbo_object_name)
29+
30+
self.assertTrue(result)

0 commit comments

Comments
 (0)
X Tutup