~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/bugs/doc/bugattachments.txt

  • Committer: Julian Edwards
  • Date: 2011-07-28 20:46:18 UTC
  • mfrom: (13553 devel)
  • mto: This revision was merged to the branch mainline in revision 13555.
  • Revision ID: julian.edwards@canonical.com-20110728204618-tivj2wx2oa9s32bx
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
    >>> from lp.bugs.interfaces.bugattachment import IBugAttachment
16
16
    >>> from lp.registry.interfaces.person import IPersonSet
17
17
    >>> bugset = getUtility(IBugSet)
18
 
    >>> bug_four = bugset.get(4)
19
 
    >>> bug_four.attachments.count()
 
18
    >>> bug_one = bugset.get(1)
 
19
    >>> bug_one.attachments.count()
20
20
    0
21
21
 
22
22
 
28
28
 
29
29
    >>> from StringIO import StringIO
30
30
 
31
 
    >>> from canonical.lazr.testing.event import TestEventListener
 
31
    >>> from canonical.launchpad.ftests.event import TestEventListener
32
32
    >>> from lazr.lifecycle.event import IObjectCreatedEvent
33
33
    >>> def attachment_added(attachment, event):
34
34
    ...     print "Attachment added: %r" % attachment.libraryfile.filename
45
45
    ...    content="a comment for the attachment",
46
46
    ...    owner=foobar)
47
47
 
48
 
    >>> bug_four.addAttachment(
 
48
    >>> bug_one.addAttachment(
49
49
    ...     owner=foobar,
50
50
    ...     data=data,
51
51
    ...     filename="foo.bar",
58
58
    >>> import transaction
59
59
    >>> transaction.commit()
60
60
 
61
 
    >>> bug_four.attachments.count()
 
61
    >>> bug_one.attachments.count()
62
62
    1
63
 
    >>> attachment = bug_four.attachments[0]
 
63
    >>> attachment = bug_one.attachments[0]
64
64
    >>> attachment.type.title
65
65
    'Unspecified'
66
66
 
68
68
passed in is often a file-like object, but can be a string too.
69
69
 
70
70
    >>> data = filecontent
71
 
    >>> attachment_from_strings = bug_four.addAttachment(
 
71
    >>> attachment_from_strings = bug_one.addAttachment(
72
72
    ...     owner=foobar,
73
73
    ...     data=data,
74
74
    ...     filename="foo.baz",
83
83
If no description is given, the title is set to the filename.
84
84
 
85
85
    >>> data = StringIO(filecontent)
86
 
    >>> screenshot = bug_four.addAttachment(
 
86
    >>> screenshot = bug_one.addAttachment(
87
87
    ...     owner=foobar,
88
88
    ...     data=data,
89
89
    ...     filename="screenshot.jpg",
99
99
    u'image/jpeg'
100
100
 
101
101
    >>> data = StringIO('</something-htmlish>')
102
 
    >>> debdiff = bug_four.addAttachment(
 
102
    >>> debdiff = bug_one.addAttachment(
103
103
    ...     owner=foobar,
104
104
    ...     data=data,
105
105
    ...     filename="something.debdiff",
131
131
    ...           'field.actions.save': 'Save Changes'})
132
132
 
133
133
Note that the +addcomment-form view is actually registered on a "bug in
134
 
context", i.e. an IBugTask, so let's grab the first bugtask on bug_four
 
134
context", i.e. an IBugTask, so let's grab the first bugtask on bug_one
135
135
and work with that:
136
136
 
137
 
    >>> bugtask = bug_four.bugtasks[0]
 
137
    >>> bugtask = bug_one.bugtasks[0]
138
138
 
139
139
    >>> add_comment_view = getMultiAdapter(
140
140
    ...     (bugtask, add_request), name='+addcomment-form')
229
229
    Attachment added: u'RA.txt'
230
230
    >>> len(add_comment_view.errors)
231
231
    0
232
 
    >>> bug_four.attachments[bug_four.attachments.count()-1].title
 
232
    >>> bug_one.attachments[bug_one.attachments.count()-1].title
233
233
    u'RA.txt'
234
234
 
235
235
Since the ObjectCreatedEvent was generated, a notification about the
261
261
    Attachment added: u'fo\xf6 bar'
262
262
    >>> len(add_comment_view.errors)
263
263
    0
264
 
    >>> attachments = bug_four.attachments
265
 
    >>> attachments[bug_four.attachments.count()-1].libraryfile.filename
 
264
    >>> attachments = bug_one.attachments
 
265
    >>> attachments[bug_one.attachments.count()-1].libraryfile.filename
266
266
    u'fo\xf6 bar'
267
 
    >>> attachments[bug_four.attachments.count()-1].libraryfile.http_url
 
267
    >>> attachments[bug_one.attachments.count()-1].libraryfile.http_url
268
268
    'http://.../fo%C3%B6%20bar'
269
269
 
270
270
If a filename contains a slash, it will be converted to a dash instead.
287
287
    Attachment added: u'foo-bar-baz'
288
288
    >>> len(add_comment_view.errors)
289
289
    0
290
 
    >>> attachments[bug_four.attachments.count()-1].libraryfile.filename
 
290
    >>> attachments[bug_one.attachments.count()-1].libraryfile.filename
291
291
    u'foo-bar-baz'
292
 
    >>> attachments[bug_four.attachments.count()-1].libraryfile.http_url
 
292
    >>> attachments[bug_one.attachments.count()-1].libraryfile.http_url
293
293
    'http://.../foo-bar-baz'
294
294
 
295
295
    >>> config_data = config.pop('max_attachment_size')
300
300
--------
301
301
 
302
302
If a user can view/edit the bug the attachment is attached to, he can
303
 
also view/edit the attachment. At the moment the bug_four is public, so
 
303
also view/edit the attachment. At the moment the bug_one is public, so
304
304
anonymous can read the attachment's attributes, but he can't set them:
305
305
 
306
306
    >>> login(ANONYMOUS)
328
328
 
329
329
Now let's make the bug private instead:
330
330
 
331
 
    >>> bug_four.setPrivate(True, getUtility(ILaunchBag).user)
 
331
    >>> bug_one.setPrivate(True, getUtility(ILaunchBag).user)
332
332
    True
333
333
 
334
334
Foo Bar isn't explicitly subscribed to the bug, BUT he is an admin, so he can
338
338
    u'Even Better Title'
339
339
    >>> attachment.title = 'Even Better Title'
340
340
 
341
 
Mr. No Privs, who is not subscribed to bug_four, cannot access or set the
 
341
Mr. No Privs, who is not subscribed to bug_one, cannot access or set the
342
342
attachments attributes:
343
343
 
344
344
    >>> login("no-priv@canonical.com")
375
375
 
376
376
Let's make the bug public again:
377
377
 
378
 
    >>> bug_four.setPrivate(False, getUtility(ILaunchBag).user)
 
378
    >>> bug_one.setPrivate(False, getUtility(ILaunchBag).user)
379
379
    True
380
380
 
381
381
 
398
398
    >>> len(bugs)
399
399
    1
400
400
    >>> bugs[0].id
401
 
    4
 
401
    1
402
402
 
403
403
    >>> from canonical.launchpad.searchbuilder import any
404
404
    >>> attachmenttype = any(*BugAttachmentType.items)
409
409
    >>> len(bugs)
410
410
    1
411
411
    >>> bugs[0].id
412
 
    4
 
412
    1
413
413
 
414
414
There are no patches attached to any bugs:
415
415
 
435
435
    >>> len(bugs)
436
436
    1
437
437
    >>> bugs[0].id
438
 
    4
 
438
    1
439
439
 
440
440
An easy way to determine whether an attachment is a patch is to read its
441
441
`is_patch` attribute.
537
537
"Adding bug attachments to private bugs" below.
538
538
 
539
539
 
540
 
    >>> from lp.services.librarian.interfaces import (
 
540
    >>> from canonical.launchpad.interfaces.librarian import (
541
541
    ...     ILibraryFileAliasSet)
542
542
 
543
543
    >>> file_content = "Hello, world"