Example #1
0
 def test_return_attachments_url_for_images_in_get_sample_url(self, _reverse):
     instance = Mock(spec=Attachment())
     instance.attachment_type = instance.IMAGE
     view_name = "some_view_name"
     url = Attachment.get_attachment_url(instance, view_name)
     _reverse.assert_called_once_with("attachments:%s" % view_name, kwargs={'identifier': instance.pk})
     self.assertEqual(_reverse.return_value, url)
Example #2
0
    def test_does_not_raise_validation_error_for_valid_mime_type(self):
        second = Second.objects.create(third_field="xyz")

        model = Attachment(
            file_name="something.jpg",
            description="alsdkfj",
            mimetype="image/jpeg",
            attachment_type=1,
            attach_to=second,
            attachment="here is a dummy image blob...."
        )

        self.assertEqual(None, model.full_clean())
Example #3
0
    def test_be_able_to_get_attachments_for_model_instance(self):
        second = Second.objects.create(third_field="xyz")
        Attachment.objects.create(file_name="x.doc", attach_to=second,
                                  attachment="first bunch of something")
        Attachment.objects.create(file_name="x.doc", attach_to=second,
                                  attachment="second bunch of something")

        first = First.objects.create(first_field="asdf", second_field="xyz")
        Attachment.objects.create(file_name="x.doc", attach_to=first, attachment_type=Attachment.DOCUMENT,
                                  attachment="Hi there, i'm a bunch of bytes")

        for attachment in Attachment.get_attachments_for(second):
            self.assertEqual(attachment.attach_to, second)

        self.assertEqual(Attachment.get_attachments_for(second).count(), 2)
Example #4
0
 def test_save_image_and_return_stream_in_create_thumbnail(self, image, stringio):
     attachment = Mock()
     attachment = Attachment(attachment=attachment)
     attachment.mimetype = "attachment/pdf"
     thumbnail = attachment.create_thumbnail(max_size=100)
     image.open.return_value.save.assert_called_once_with(stringio.return_value, 'pdf')
     self.assertEqual(stringio.return_value.getvalue.return_value, thumbnail)
    def test_evidence_attachments(self):
        task = Task.objects.create(name='Test task',
                                   creator=self.user)
        self.assertEquals(Task.objects.filter(name='Test task').count(), 1)

        evidence = Evidence.objects.create(task=task)
        self.assertEquals(Evidence.objects.filter(task=task).count(), 1)

        att = Attachment()
        att.creator = self.user

        file_mock = mock.MagicMock(spec=File, name='FileMock')
        file_mock.name = 'test1.jpg'
        storage_mock = mock.MagicMock(spec=Storage, name='StorageMock')
        storage_mock.url = mock.MagicMock(name='url')
        storage_mock.url.return_value = '/tmp/test1.jpg'

        ct = ContentType.objects.get(app_label=evidence._meta.app_label,
                                     model=evidence._meta.model_name)
        att.content_type = ct
        att.object_id = evidence.id
        att.attachment_file = file_mock
        
        with mock.patch('django.core.files.storage.default_storage._wrapped', storage_mock):
            att.save()
        self.assertEquals(Attachment.objects.count(), 1)
        self.assertEquals(att, evidence.attachments.first())
Example #6
0
def attach_as_file(handle_id, name, content, user, overwrite=False):
    """
        Attach a file to a handle_id.
        - handle_id the id of the node to attach a file to.
        - name the name of the file
        - content the content of the file (e.g. a string)
        - user the user doing the attach
        - overwrite should the file be overwritten if it exists. (Default: False)

        All files are stored in {root}/niweb/media/attachments, and the metadata is attached in the django db.
    """
    nh = NodeHandle.objects.get(pk=handle_id)
    _file = SimpleUploadedFile(name,
                               content.encode('utf-8'),
                               content_type="text/plain")
    if overwrite:
        attachment = Attachment.objects.filter(
            object_id=handle_id, attachment_file__endswith=name).first()
        if attachment:
            attachment.attachment_file.delete()
    if not attachment:
        attachment = Attachment()
    attachment.content_type = ContentType.objects.get_for_model(nh)
    attachment.object_id = handle_id
    attachment.creator = user
    attachment.attachment_file = _file
    return attachment.save()
Example #7
0
def resume(request):
    registration = get_object_or_none(ConferenceRegistration, user=request.user, submitted=True, cancelled=False)

    if not registration:
        request.user.message_set.create("You aren't registered for conference...")
        return HttpResponseRedirect(reverse('confreg'))
    
    if request.method == 'POST':
        form = ConferenceResumeForm(request.POST, request.FILES)
        if form.is_valid():
            resume = Attachment()
            resume.creator = request.user
            resume.content_type = ContentType.objects.get_for_model(registration)
            resume.object_id = registration.id
            resume.attachment_file = form.cleaned_data['resume']
            resume.save()
            
            request.user.message_set.create(message="Thank you!")
            return HttpResponseRedirect(reverse('confreg'))
    else:
        form = ConferenceResumeForm()
                
    return render_to_response('conference/resume.html',
                              {'registration': registration,
                               'form': form,
                               'user': request.user,
                              },
                              context_instance=RequestContext(request)
                             )
Example #8
0
    def test_get_attachment_for_model_for_each_model_in_list_in_get_attachments_for_list(self):
        first_model, second_model = Mock(), Mock()
        attachment = Mock()
        Attachment.get_attachments_for.return_value = [attachment]
        result = list(Attachment.get_attachments_for_list([first_model, second_model]))
        self.assertEqual([
            ((first_model,), {}),
            ((second_model,), {}),
        ], Attachment.get_attachments_for.call_args_list)

        self.assertEqual([attachment] * 2, result)
Example #9
0
    def _add_attachment(self, code, name='audit'):
        with tempfile.NamedTemporaryFile(
                mode='w+b', delete=False, suffix=".trash",
                dir=settings.MEDIA_ROOT) as temporary_file:
            try:
                temporary_file.write(b'\x04\x02')
                temporary_file.seek(0)
                file_type, created = FileType.objects.get_or_create(
                    name=name, label='audit', code='audit')

                attachment = Attachment(content_object=self.engagement,
                                        code=code,
                                        file_type=file_type)

                attachment.file.save(temporary_file.name, File(temporary_file))
                attachment.save()

            finally:
                if os.path.exists(temporary_file.name):
                    os.remove(temporary_file.name)
Example #10
0
    def form_valid(self, form):
        """ Save the attachments """
        response = super().form_valid(form)
        files = self.request.FILES.getlist('files')
        for f in files:
            for message in form.messages:
                Attachment(
                    content_object=message,
                    creator=self.request.user,
                    attachment_file=f,
                ).save()

        return response
Example #11
0
def _preview(request,
             context_processors,
             extra_context,
             model=FreeThreadedComment,
             form_class=MyThreadedCommentForm,
             attach_form_class=AttachmentForm):
    """
    Returns a preview of the comment so that the user may decide if he or she wants to
    edit it before submitting it permanently.
    """
    tcviews._adjust_max_comment_length(form_class)
    attach_count = -1
    if "attach_count" in request.POST:
        attach_count = int(request.POST["attach_count"])

    form = form_class(request.POST or None, instance=model())
    attach_forms = [
        attach_form_class(request.POST or None,
                          request.FILES or None,
                          prefix=str(x),
                          instance=Attachment())
        for x in range(0, attach_count)
    ]
    context = {
        'next': tcviews._get_next(request),
        'form': form,
        'attach_forms': attach_forms,
        'attach_count': attach_count,
    }
    if attach_count > -1:
        context['attach_forms'] = attach_forms
        context['attach_count'] = attach_count
    else:
        context['attach_forms'] = None
        context['attach_count'] = None

    if form.is_valid() and all([af.is_valid() for af in attach_forms]):
        new_comment = form.save(commit=False)
        for af in attach_forms:
            attachment = af.save(request, new_comment, commit=False)
        context['comment'] = new_comment
    else:
        context['comment'] = None

    # do we need a visibility check here?
    # probably not, since nothing is actually saved.

    return render_to_response('threadedcomments/preview_comment.html',
                              extra_context,
                              context_instance=RequestContext(
                                  request, context, context_processors))
Example #12
0
    def handle(self, *args, **options):

        if len(args) != 1:
            print "Usage: import_attachments", self.args
            return
        try:
            user = args[0]
            user = User.objects.get(username=user)
        except:
            print "unknown user", user
            return

        dirname = os.path.join(settings.MEDIA_ROOT,
                               settings.ATTACHMENT_LOCATION)
        for f in os.listdir(dirname):
            if not os.path.isdir(f):
                path = os.path.join(settings.ATTACHMENT_LOCATION, f)
                existing = Attachment.objects.filter(file=path)
                if len(existing) == 0:
                    a = Attachment(file=path, uploader=user, description=f)
                    a.save()
                    print "storing ", path
                else:
                    print "not storing ", path
Example #13
0
def get_attachment_form(request,
                        template_name="threadedcomments/attachment_form.html",
                        form_class=AttachmentForm):

    if request.is_ajax():
        attach_form = form_class(prefix=request.POST['prefix'],
                                 instance=Attachment())
        response = render_to_response(
            template_name,
            {
                'attach_form': attach_form,
            },
            context_instance=RequestContext(request),
        )
        return response
Example #14
0
def registration_preview(request):
    #def pay_membership_preview(request, username):
    username = request.user.username

    f = ConferenceRegistrationForm(request.POST, request.FILES)
    f.user = request.user
    if f.is_valid():
        if f.cleaned_data.get('resume', None):
            resume = Attachment()
            resume.creator = request.user
            resume.content_type = ContentType.objects.get_for_model(
                request.user)
            resume.object_id = request.user.id
            resume.attachment_file = f.cleaned_data['resume']
            resume.save()

    return ConferenceRegistrationFormPreview(ConferenceRegistrationForm)(
        request, username=username)
Example #15
0
def registration_preview(request):
    username = request.user.username
    
    f = ConferenceRegistrationForm(request.POST, request.FILES)
    f.user = request.user
    if f.is_valid():
        if f.cleaned_data.get('resume', None):
            resume = Attachment()
            resume.creator = request.user
            resume.content_type = ContentType.objects.get_for_model(request.user)
            resume.object_id = request.user.id
            resume.attachment_file = f.cleaned_data['resume']
            resume.save()
            
    return ConferenceRegistrationFormPreview(ConferenceRegistrationForm)(request, username=username)
Example #16
0
def get_attachment_form(request,
                        template_name="topics/attachment_form.html",
                        form_class=AttachmentForm,
                        group_slug=None,
                        bridge=None):

    if request.is_ajax():
        attach_form = form_class(prefix=request.POST['prefix'],
                                 instance=Attachment())
        response = render_to_response(
            template_name,
            {
                'attach_form': attach_form,
            },
            context_instance=RequestContext(request),
        )
        return response
    else:
        raise Http404
Example #17
0
    def testDeepCopying(self):
        """
        Test that doing a deep copy of a file actually attempt to create a
        second version of a file.
        """
        att1 = Attachment.objects.create_for_object(
            self.tm, file=self.test_file1, attached_by=self.bob,
            title="Something", summary="Something")
        f = File(self.test_file1)
        att1.file.save('models.py', f)

        att2 = att1.copy(self.tm2, deepcopy=True)

        # Ensure the saved_copy uses its proper file path
        attachments = Attachment.objects.attachments_for_object(self.tm2)
        for attachment in attachments:
            self.assertEqual(
                attachment.file.name,
                Attachment.get_attachment_dir(attachment,
                                              attachment.file_name())
            )
Example #18
0
def upload_file(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            description = form.cleaned_data['description']
            attachment = Attachment(file=request.FILES['file'], description=description, uploader=request.user)
            attachment.save()
            return HttpResponseRedirect('/attachments/')
    if request.method == 'DELETE':
        aid = request.GET.get('id',None)
        if aid:
            attachment = Attachment.objects.get(id=aid)
            if attachment:
                attachment.delete()
        return HttpResponseRedirect('/attachments/')
    return HttpResponseRedirect('/attachments/')
Example #19
0
def articletobinder(article):
    bb = re.match(r'(?P<name>[^(]*)\(*(?P<location>[^)]*)', article.title)
    location_parts = bb.group('location').split(',')
    if len(location_parts) == 2 :
        (city, state) = location_parts
    else:
        city = bb.group('location').strip()
        state = ''
        
#   loc = re.match(r'(?P<city>\w*)\W*(?P<state>\w*).*$',  bb.group('location'))
#     if article.alias:
#         trimslug = re.match(r'[^\w]*([\w-]+)$', article.alias)
#         josslug = trimslug.group(1)
#     else:
#         josslug = slugify(bb.group('name').strip() + ' ' + bb.group('location').strip())
    
    jos_combined_text = article.introtext + article.fulltext
    entry = Entry.objects.create(
        name = bb.group('name').strip(),
        city = city.strip(),
        state = state.strip(),
        # location = bb.group('location').strip(),
        creator = import_user,
        )
    soup = BeautifulSoup.BeautifulSoup(jos_combined_text)
    for img in soup.findAll('img', src=True):
        absolutepath = os.path.join(JOOMLA_IMG_ROOT, unquote(img['src']))
        os.chdir(os.path.dirname(absolutepath))
        try:
            pict = File(open(os.path.basename(absolutepath), 'r'))
            a = Attachment()
            a.creator = import_user
            a.content_object = entry
            a.attachment_file = pict
            a.save()
            pict.close()
            img['src'] = urlparse(a.attachment_file.url)[2] # python 2.4 version 
                                                            # of urlparse
        except IOError:
            print 'cannot open ' , absolutepath
    
    entry.content = html2text(unicode(soup))
    entry.save()
    Tag.objects.add_tag(entry, 'joomlacontent')
Example #20
0
def edit(request, thread):
    """
    If a thread isn't closed, and the user is logged in, post a reply
    to a thread. Note we don't have "nested" replies at this stage.
    """
    if not request.user.is_authenticated():
        return HttpResponseServerError()
    t = get_object_or_404(Thread, pk=thread)
    if t.closed:
        return HttpResponseServerError()
    if not Forum.objects.has_access(t.forum, request.user.groups.all()):
        return HttpResponseForbidden()

    if request.method == "POST":
        form = ReplyForm(data=request.POST, files=request.FILES)
        if form.is_valid():
            body = form.cleaned_data['body']
            p = Post(
                thread=t,
                author=request.user,
                body=body,
                time=datetime.now(),
                )
            p.save()

            sub = Subscription.objects.filter(thread=t, author=request.user)
            if form.cleaned_data.get('subscribe',False):
                if not sub:
                    s = Subscription(
                        author=request.user,
                        thread=t
                        )
                    s.save()
            else:
                if sub:
                    sub.delete()

            # Subscriptions are updated now send mail to all the authors subscribed in
            # this thread.
            mail_subject = ''
            try:
                mail_subject = settings.FORUM_MAIL_PREFIX
            except AttributeError:
                mail_subject = '[Forum]'

            mail_from = ''
            try:
                mail_from = settings.FORUM_MAIL_FROM
            except AttributeError:
                mail_from = settings.DEFAULT_FROM_EMAIL

            mail_tpl = loader.get_template('forum/notify.txt')
            c = Context({
                'body': wordwrap(striptags(body), 72),
                'site' : Site.objects.get_current(),
                'thread': t,
                })

            email = EmailMessage(
                    subject=mail_subject+' '+striptags(t.title),
                    body= mail_tpl.render(c),
                    from_email=mail_from,
                    to=[mail_from],
                    bcc=[s.author.email for s in t.subscription_set.all()],)
            email.send(fail_silently=True)

            for attachedfilefield in form.files:
                #file_path = '%s%s' % (settings.MEDIA_ROOT, form.files[attachedfilefield])
                attachment_file = form.files[attachedfilefield]
                attach=Attachment()
                attach.handle_uploaded_attachment(p,
                    attachment_file,
                    request.user,
                    attachment_file.name,
                    t.title
                    )

            return HttpResponseRedirect(p.get_absolute_url())
    else:
        post_id = request.GET.get('pid', 0)
        if post_id ==0:
            return HttpResponseServerError(_("The Post Do Not Exist"))
        try:
            p = Post.objects.all().get(id=post_id)
        except Post.DoesNotExist:
            raise Http404(_("Not Found"))
        
        body=p.body
        initial = {'subscribe': True,
            'body':body}
        form = ReplyForm(initial=initial)

    return render_to_response('forum/edit.html',
        RequestContext(request, {
            'form': form,
            'post': p
        }))
Example #21
0
def new_topic(request, group_slug=None, bridge=None):
    is_member = False
    group = None
    if group_slug is None:
        group_slug = "ewb"

    group = get_object_or_404(BaseGroup, slug=group_slug)
    is_member = group.user_is_member(request.user, admin_override=True)

    if not group.is_visible(request.user):
        return HttpResponseForbidden()

    attach_count = 0
    if request.method == "POST":
        if not request.user.is_authenticated():
            return HttpResponseForbidden()
        try:
            attach_count = int(request.POST.get("attach_count", 0))
        except ValueError:
            attach_count = 0

        if group.slug == "ewb" or is_member:
            # has been previewed.  mark it as good to go!
            if request.POST.get("previewed", None) and request.POST.get(
                    "postid", None):
                topic = GroupTopic.objects.get(id=request.POST['postid'],
                                               creator=request.user)
                if topic.pending:
                    topic.pending = False
                    topic.save()

                    # extra security check that sender isn't forged.
                    # can't hurt...
                    sender_valid = False
                    if group.user_is_admin(request.user) and request.POST.get(
                            'sender', None):
                        if request.POST['sender'] == group.from_email:
                            sender_valid = True
                            sender = '"%s" <%s>' % (group.from_name,
                                                    group.from_email)

                        elif get_object_or_none(
                                EmailAddress, email=request.POST['sender']
                        ) in request.user.get_profile().email_addresses():
                            sender_valid = True
                            sender = '"%s %s" <%s>' % (
                                request.user.get_profile().first_name,
                                request.user.get_profile().last_name,
                                request.POST['sender'])

                        elif request.user.is_staff and request.POST[
                                'sender'] == "*****@*****.**":
                            sender_valid = True
                            sender = '"EWB-ISF Canada" <*****@*****.**>'

                    if topic.send_as_email:
                        if sender_valid:
                            request.user.message_set.create(
                                message=escape("Sent as %s" % sender))
                            topic.send_email(sender=sender)
                        else:
                            request.user.message_set.create(
                                message="Unable to send email.")

                    # redirect out.
                    request.user.message_set.create(
                        message=_("You have started the topic %(topic_title)s")
                        % {"topic_title": topic.title})

                else:
                    # probably double-clicked the submit button and this is the dupe request...
                    pass

                return HttpResponseRedirect(topic.get_absolute_url())

            # confirmation was cancelled, so delete the temp post and bump back to edit screen
            topic = None
            if request.POST.get("goback", None) and request.POST.get(
                    "postid", None):
                topic = get_object_or_none(GroupTopic,
                                           id=request.POST['postid'],
                                           pending=True,
                                           creator=request.user)

            if topic:
                topic_form = GroupTopicForm(instance=topic,
                                            user=request.user,
                                            group=group)
                attach_forms = []
                topic.delete()

            # validate form and show preview...
            else:
                topic_form = GroupTopicForm(request.POST,
                                            user=request.user,
                                            group=group)
                attach_forms = [
                    AttachmentForm(request.POST,
                                   request.FILES,
                                   prefix=str(x),
                                   instance=Attachment())
                    for x in range(0, attach_count)
                ]

                # do not take blank attachment forms into account
                for af in attach_forms:
                    if not af.is_valid() and not af['attachment_file'].data:
                        attach_forms.remove(af)
                        attach_count = attach_count - 1

                # all good.  save it!
                if topic_form.is_valid() and all([
                        af.is_valid() for af in attach_forms
                ]) and not request.POST.get("goback", None):
                    # save the post but mark it as "pending".... and display a confirmation.
                    topic = topic_form.save(commit=False)
                    if group:
                        group.associate(topic, commit=False)
                    topic.creator = request.user
                    topic.pending = True
                    topic.save()

                    # save the attachments.
                    # We need the "Topic" object in order to retrieve attachments properly
                    # since other functions only get the Topic object
                    base_topic = GroupTopic.objects.get(id=topic.id)
                    attachments = []
                    for af in attach_forms:
                        attachment = af.save(request, base_topic)
                        attachments.append(
                            af.cleaned_data['attachment_file'].name)

                    sender = None
                    if topic_form.cleaned_data.get('send_as_email', None):
                        sender = topic_form.cleaned_data.get('sender', None)

                    is_large_group = False
                    if group.members.count() > 50:
                        is_large_group = True

                    return render_to_response(
                        "topics/preview.html", {
                            "group": group,
                            "topic": topic,
                            "is_member": is_member,
                            "sender": sender,
                            "attachments": attachments,
                            "is_large_group": is_large_group,
                        },
                        context_instance=RequestContext(request))
        else:
            # if they can't start a topic, why are we still loading up a form?
            request.user.message_set.create(message=_(
                "You are not a member and so cannot start a new topic"))
            topic_form = GroupTopicForm(instance=GroupTopic())
            attach_forms = [
                AttachmentForm(prefix=str(x), instance=Attachment())
                for x in range(0, attach_count)
            ]

    else:
        topic_form = GroupTopicForm(instance=GroupTopic(),
                                    user=request.user,
                                    group=group)
        attach_forms = []

    return render_to_response("topics/new_topic.html", {
        "group": group,
        "topic_form": topic_form,
        "attach_forms": attach_forms,
        "attach_count": attach_count,
        "is_member": is_member,
    },
                              context_instance=RequestContext(request))
Example #22
0
 def test_open_image_with_stringio_of_attachment_in_create_thumbnail(self, image_class, stringio):
     attachment = Attachment(attachment=Mock())
     attachment.mimetype = "x/y"
     attachment.create_thumbnail(max_size=100)
     self.assertEqual(((attachment.attachment,), {}), stringio.call_args_list[0])
     image_class.open.assert_called_once_with(stringio.return_value)
Example #23
0
def ticket_from_message(message):
    # 'message' must be an RFC822 formatted message.
    
    #BUG: We need to check for messages address to multiple tickets
    #BUG: Don't break with multiple 'to' addresses
    #BUG: What if a ticket is CC'd?
    
    message = email.message_from_string(message)
    subject = message.get('subject', 'No Subject')
    subject = subject.replace('Re:', '')
    subject = subject.replace('RE:', '')
    subject = subject.replace('re:', '')
    subject = subject.replace('FW:', '')
    subject = subject.replace('Fw:', '')
    subject = subject.replace('fw:', '')
    subject = subject.strip()

    sender = message.get('from', None)
    sender_email = parseaddr(sender)[1]

    recipients = getaddresses(message.get_all('to', []) + message.get_all('cc', []))
    #TODO: Check if all recipients are associated with the ticket(s), add if not
    
    tickets = []
    for recipient in recipients:
        name_part, user_part, plus_part, domain_part = get_address_parts(recipient)
        if user_part + '@' + domain_part == settings.IT_MAILSUCK_ADDRESS:
            #This recipient is a ticket address
            print 'Message destined for ticket system'
            if plus_part:
                #Message is destined for a specific ticket
                print 'Message allegedly for ticket: %s' %(plus_part)
                try:
                    t = Ticket.objects.get(pk=plus_part)
                    print 'Ticket found %s' %(plus_part)
                    if not t in tickets:
                        tickets.append(t)
                except Ticket.ObjectNotFound:
                    print 'Unable to locate ticket %s' %(plus_part)
                    continue
                    #BUG: This should be logged (in fact, all incoming messages should be logged...)
            else:
                t = Ticket()
                if not t in tickets:
                    tickets.append(t)
        else:
            print 'Not destined for ticket system, skipping' %(recipient)
            continue

#BUG: Don't blindly accept mail/attachments for existing tickets.  Allow only from people involved with the ticket.
#BUG: Don't blindly create new tickets, check account status

    body_plain, body_html = '', ''
    counter = 0
    files = []
    for part in message.walk():
        if part.get_content_maintype() == 'multipart':
            continue
        
        print 'Part params: '
        print part.get_params()
        name = part.get_param('name')

        if part.get_content_maintype() == 'text' and name == None:
            if part.get_content_subtype() == 'plain':
                body_plain = decodeUnknown(part.get_charset(), part.get_payload(decode=True))
            else:
                body_html = part.get_payload(decode=True)
        else:
            if not name:
                name = part.get_filename()
            
            files.append({
                'filename': name,
                'content': part.get_payload(decode=True),
                'type': part.get_content_type()},
                )
        counter += 1

    if body_plain:
        body = body_plain
    else:
        body = 'No plain-text email body available. Please see HTML attachment.'

    if body_html:
        files.append({
            'filename': 'email_html_body.html',
            'content': body_html,
            'type': 'text/html',
        })
    now = datetime.now()

    for ticket in tickets:
        if ticket.pk:
            #TODO: Existing tickets need a comment created
            pass
        else:
            #TODO: Lookup contact by e-mail, find associated company
            ticket.company = Company.objects.get(pk=1) #BUG: Hardcoded
            ticket.contact = User.objects.get(pk=1) #BUG: Hardcoded
            #TODO: Need to have finish 'team' setup and have a default tech for each account
            ticket.assignedto = User.objects.get(pk=1)
            ticket.source = TicketSource.objects.get(pk='E-Mail')
            ticket.summary =  subject
            ticket.description = body

        important_types = ('high', 'important', '1', 'urgent')
        if message.get('priority', '') in important_types or message.get('importance', '') in important_types:
            ticket.priority = TicketPriority.objects.get(name='Emergency')
        else:
            ticket.priority = TicketPriority.objects.get(name='Normal')

        #TODO: Check ticket status, change appropriately
        #TODO: Make sure everyone in 'to' or 'cc' get included in the ticket notification list

        #Save the ticket before attaching files...
        print ticket
        ticket.save()
    
    for file in files:
        print "Scanning through attachments"
        if file['content']:
            print 'Attaching file: %s' %(file['filename'])
            filename = file['filename'].encode('ascii', 'replace').replace(' ', '_')
            filename = re.sub('[^a-zA-Z0-9._-]+', '', filename)

            print 'Almost there: %s' %(filename)
            fh, fpath = mkstemp(prefix='itmailattach-', suffix='-%s' %(filename), text=True)
            f = open(fpath, 'w+')
            print 'Writing attachment to %s' %(fpath)
            f.write(file['content'])
            f.flush()
            fa = File(open(fpath, 'r'))

            for ticket in tickets:
                a = Attachment()
                a.content_object = ticket
                a.creator = User.objects.get(pk=1) #BUG: We need a way to specify a 'system' user or something
                a.attachment_file = fa
                a.save()

            fa.close()
            f.close()
            os.remove(fpath)

    return True
Example #24
0
def add_project(request):
    if request.method == 'POST':
        form1 = CreateProjectForm(request.POST)
        input_data = request.POST.copy()
        if form1.is_valid():
            obj1 = sql_util.save_entry_to_db(form1, input_data)
            pk = obj1.id
            app_label = 'pages'
            model_name = 'Project'
            if request.user.has_perm("attachments.add_attachment"):
                model = apps.get_model(app_label, model_name)
                obj = get_object_or_404(model, pk=pk)
                files = request.FILES.getlist('attachment_file')
                images = request.FILES.getlist('attachment_image')
                if len(files) > 0:
                    for f in files:
                        test = MultiValueDict({'attachment_file': [f]})
                        form = AttachmentForm(request.POST, test)
                        if form.is_valid():
                            form.save(request, obj)
                if len(images) > 0:
                    for i in images:
                        test = MultiValueDict({'attachment_image': [i]})
                        form = ImageForm(request.POST, test)
                        if form.is_valid():
                            form.save(request, obj)
            if 'undertaking' in input_data:
                form2 = CreateReferenceProjectForm(request.POST)
                if form2.is_valid():
                    obj2 = sql_util.save_ref_to_db(form2, obj1)
            return HttpResponseRedirect(reverse('success'))
        else:
            input_data = request.POST.copy()
            attachment_model = Attachment(pk=1)  # tää on viel kyssäri
            image_model = Image(pk=1)
            context = {
                'form1':
                CreateProjectForm(request.POST),
                'form2':
                CreateReferenceProjectForm(request.POST),
                'attachment':
                attachment_model,
                'image':
                image_model,
                'filters':
                sql_util.get_filters(),
                'selected_filters':
                parse_util.filters_qs_to_dict(
                    parse_util.parse_input_filters(input_data)),
                'is_reference':
                'undertaking' in input_data
            }
            return render(request, 'add_project.html', context)
    elif request.method == 'GET':
        form1 = CreateProjectForm()
        form2 = CreateReferenceProjectForm()
        attachment_model = Attachment(pk=1)  # tää on viel kyssäri
        image_model = Image(pk=1)
        context = {
            'form1': form1,
            'form2': form2,
            'attachment': attachment_model,
            'image': image_model,
            'filters': sql_util.get_filters(),
        }
        return render(request, 'add_project.html', context)
Example #25
0
def edit_project(request, id):
    project = models.Project.project_db.get(id=id)
    if request.method == 'POST':
        form1 = CreateProjectForm(request.POST)
        if form1.is_valid():
            input_data = request.POST.copy()
            sql_util.edit_entry_in_db(project, form1, input_data)
            app_label = 'pages'
            model_name = 'Project'
            if request.user.has_perm("attachments.add_attachment"):
                model = apps.get_model(app_label, model_name)
                obj = get_object_or_404(model, pk=id)
                files = request.FILES.getlist('attachment_file')
                images = request.FILES.getlist('attachment_image')
                if len(files) > 0:
                    for f in files:
                        test = MultiValueDict({'attachment_file': [f]})
                        form = AttachmentForm(request.POST, test)
                        if form.is_valid():
                            form.save(request, obj)
                        #else invalid
                if len(images) > 0:
                    existing_images = Image.objects.attachments_for_object(id)
                    for image in existing_images:
                        delete_image(request, image.pk)
                    for i in images:
                        test = MultiValueDict({'attachment_image': [i]})
                        form = ImageForm(request.POST, test)
                        if form.is_valid():
                            form.save(request, obj)
                        #else invalid
            print(input_data)
            if 'undertaking' in input_data:
                form2 = CreateReferenceProjectForm(request.POST)
                print(form2.is_valid())
                if form2.is_valid():
                    sql_util.edit_ref_in_db(project.referenceproject, form2)
                #else invalid - look at what happens when form1 is not valid
            return HttpResponseRedirect(reverse(success))
        else:
            input_data = request.POST.copy()
            attachment_model = Attachment(pk=1)  # tää on viel kyssäri
            image_model = Image(pk=1)
            context = {
                'form1':
                CreateProjectForm(request.POST),
                'form2':
                CreateReferenceProjectForm(request.POST),
                'attachment':
                attachment_model,
                'image':
                image_model,
                'id':
                id,
                'filters':
                sql_util.get_filters(),
                'selected_filters':
                parse_util.filters_qs_to_dict(
                    parse_util.parse_input_filters(input_data))
            }
            return render(request, 'edit_project.html', context)

    elif request.method == 'GET':
        f1, f2, f3, f4, f5 = sql_util.get_filters()
        filters_qset = project.filters.all()
        filters_dict = parse_util.filters_qs_to_dict(filters_qset)
        form1 = CreateProjectForm(
            initial={
                'project_name':
                project.project_name,
                'destination_name':
                '' if project.destination_name ==
                '—' else project.destination_name,
                'start_date':
                parse_util.format_time(project.start_date),
                'end_date':
                parse_util.format_time(project.end_date),
                'keywords':
                '' if project.keywords == '—' else project.keywords,
                'project_description':
                project.project_description,
                'documentation_path':
                '' if project.documentation_path ==
                '—' else project.documentation_path,
                'project_manager':
                '' if project.project_manager ==
                '—' else project.project_manager,
            })
        try:
            form2 = CreateReferenceProjectForm(
                initial={
                    'undertaking':
                    project.referenceproject.undertaking,
                    'client':
                    project.referenceproject.client,
                    'area':
                    project.referenceproject.area,
                    'construction_cost':
                    project.referenceproject.construction_cost,
                    'project_accepted':
                    parse_util.format_time(
                        project.referenceproject.project_accepted),
                    'construction_permit_granted':
                    parse_util.format_time(
                        project.referenceproject.construction_permit_granted),
                })
        except ObjectDoesNotExist:
            form2 = False
        attachment_model = Attachment(pk=1)
        image_model = Image(pk=1)
        context = {
            'form1':
            form1,
            'form2':
            form2,
            'attachment':
            attachment_model,
            'image':
            image_model,
            'id':
            id,
            'filters':
            sql_util.get_filters(),
            'selected_filters':
            parse_util.filters_qs_to_dict(project.filters.all())
        }
        return render(request, 'edit_project.html', context)
Example #26
0
 def test_not_create_thumbnail_when_not_image_type_in_save(self, create_thumbnail):
     attached_file = Mock()
     attached_file.name = "image.doc"
     attached_file.chunks.return_value = []
     Attachment(attachment=attached_file).save()
     self.assertEqual(0, create_thumbnail.call_count)
Example #27
0
def upload(request, app_model, id, field=None, form=FileForm, filename_prefix=None):
    """
    Main Multiuploader module.
    Parses data from jQuery plugin and makes database changes.
    """
    att = False
    if request.method == 'POST':
        if "application/json" in request.META['HTTP_ACCEPT_ENCODING']:
            mimetype = 'application/json'
        else:
            mimetype = 'text/plain'

        log.info('received POST to main multiuploader view')
        if request.FILES == None:
            return HttpResponseBadRequest('Must have files attached!')

        #getting file data for farther manipulations
        file = request.FILES[u'files[]']
        wrapped_file = UploadedFile(file)
        filename = wrapped_file.name
        file_size = wrapped_file.file.size
        log.info ('Got file: "%s"' % str(filename))
        log.info('Content type: "$s" % file.content_type')

        """ Use a form to validate the upload """
        instance = form({ 'filename': filename, 'id': id })
        if not instance.is_valid():
            return HttpResponse(simplejson.dumps([{
                                        'error': instance.errors['__all__'],
                                        'name': filename,
                                        'size': file_size,
                                        }]), mimetype=mimetype)

        in_app, in_model = app_model.split('.')
        model = get_model(in_app, in_model)

        if field:
            field_info = model._meta.get_field_by_name(field)
        else:
            field_info = [False]

        obj = get_object_or_404(model, pk=id)
        if not isinstance(field_info[0], FileField) or not isinstance(field_info[0], ImageField):
            """ if the field we are uploading to is not a FileField or
                ImageField it is a generic attachment """
            if Attachment:
                """ attach the file """
                att = Attachment()
                att.content_object = obj
                att.client = obj.client
                att.file.save(filename, file, save=False)
                att.added_by = request.user
                att.save()
                if field:
                    """ if we pass in an optional field name then the sending
                    object tracks the attachement too.  So we set the attachment
                    ID in the foreign object """
                    setattr(obj, field, att)
            else:
                raise "Cannot attach file"
        else:
            """ this does not use the Attachment model and instead tracks the
            upload independantly.  Just upload the file and save it to the
            foreign model """
            if not field:
                raise "Field is mandatory when not a generic attachement"
            setattr(obj, field, file)

        obj.save()
        log.info('File saving done')

        upload_done.send(sender=create_string_wrapper('uploadit'),
                        app=app_model.split('.')[0],
                        model=app_model.split('.')[1],
                        field=field, instance=obj,
                        filename=filename, attachment_object=att)

        #generating json response array
        result = []
        result.append({"name":filename,
                       "size":file_size,
                       "delete_type":"POST",})

        response_data = simplejson.dumps(result)

        return HttpResponse(response_data, mimetype=mimetype)

    else: #GET
        return HttpResponseBadRequest()
Example #28
0
 def test_call_get_attachment_url_with_preview_in_preview(self):
     instance = Mock(spec=Attachment())
     Attachment.preview(instance)
     instance.get_attachment_url.assert_called_once_with("preview")
Example #29
0
def free_comment(request,
                 content_type=None,
                 object_id=None,
                 edit_id=None,
                 parent_id=None,
                 add_messages=False,
                 ajax=False,
                 model=FreeThreadedComment,
                 form_class=MyFreeThreadedCommentForm,
                 attach_form_class=AttachmentForm,
                 context_processors=[],
                 extra_context={}):
    """
    Receives POST data and either creates a new ``ThreadedComment`` or 
    ``FreeThreadedComment``, or edits an old one based upon the specified parameters.

    If there is a 'preview' key in the POST request, a preview will be forced and the
    comment will not be saved until a 'preview' key is no longer in the POST request.

    If it is an *AJAX* request (either XML or JSON), it will return a serialized
    version of the last created ``ThreadedComment`` and there will be no redirect.

    If invalid POST data is submitted, this will go to the comment preview page
    where the comment may be edited until it does not contain errors.
    """
    if not edit_id and not (content_type and object_id):
        raise Http404  # Must specify either content_type and object_id or edit_id
    if "preview" in request.POST:
        return _preview(request,
                        context_processors,
                        extra_context,
                        model=model,
                        form_class=form_class,
                        attach_form_class=attach_form_class)
    if edit_id:
        instance = get_object_or_404(model, id=edit_id)
    else:
        instance = model()
    tcviews._adjust_max_comment_length(form_class)
    attach_count = -1
    if "attach_count" in request.POST:
        attach_count = int(request.POST["attach_count"])
    form = form_class(request.POST, instance=instance)
    attach_forms = [
        attach_form_class(request.POST,
                          request.FILES,
                          prefix=str(x),
                          instance=Attachment())
        for x in range(0, attach_count)
    ]

    # do not take blank attachment forms into account
    for af in attach_forms:
        if not af.is_valid() and not af['attachment_file'].data:
            attach_forms.remove(af)
            attach_count = attach_count - 1

    if form.is_valid() and all([af.is_valid() for af in attach_forms]):
        new_comment = form.save(commit=False)

        # visibility check!
        if request.user.is_anonymous():
            return HttpResponseForbidden()

        # get parent object
        ctype = ContentType.objects.get(id=content_type)
        parentgrp = helpers.get_obj(ct=ctype, id=object_id)
        if not helpers.is_visible(request.user, parentgrp):
            return HttpResponseForbidden()

        # set up the comment object for saving
        if not edit_id:
            new_comment.ip_address = request.META.get('REMOTE_ADDR', None)
            new_comment.content_type = get_object_or_404(ContentType,
                                                         id=int(content_type))
            new_comment.object_id = int(object_id)
        if model == ThreadedComment:
            new_comment.user = request.user
        if parent_id:
            new_comment.parent = get_object_or_404(model, id=int(parent_id))
        new_comment.save()

        # handle attachments
        for af in attach_forms:
            attachment = af.save(request, new_comment)

        # and send email
        # (can't do this in a post-save hook since attachments aren't saved at that point)
        new_comment.send_to_watchlist()

        # handle tags
        newtags = set(form.cleaned_data['tags'].split(','))
        oldtags = set(new_comment.content_object.tags.split(','))
        alltags = newtags | oldtags
        alltags.remove('')
        tagstring = ""
        for t in alltags:
            tagstring += t + ","
        new_comment.content_object.tags = tagstring
        new_comment.content_object.save()

        # and display success messages
        if model == ThreadedComment:
            if add_messages:
                request.user.message_set.create(
                    message="Your message has been posted successfully.")
        else:
            request.session['successful_data'] = {
                'name': form.cleaned_data['name'],
                'website': form.cleaned_data['website'],
                'email': form.cleaned_data['email'],
            }
        if ajax == 'json':
            return JSONResponse([
                new_comment,
            ])
        elif ajax == 'xml':
            return XMLResponse([
                new_comment,
            ])
        else:
            return HttpResponseRedirect(tcviews._get_next(request))
    elif ajax == "json":
        return JSONResponse({'errors': form.errors}, is_iterable=False)
    elif ajax == "xml":
        template_str = """
<errorlist>
    {% for error,name in errors %}
    <field name="{{ name }}">
        {% for suberror in error %}<error>{{ suberror }}</error>{% endfor %}
    </field>
    {% endfor %}
</errorlist>
        """
        response_str = Template(template_str).render(
            Context({'errors': zip(form.errors.values(), form.errors.keys())}))
        return XMLResponse(response_str, is_iterable=False)
    else:
        return _preview(request,
                        context_processors,
                        extra_context,
                        model=model,
                        form_class=form_class,
                        attach_form_class=attach_form_class)
Example #30
0
 def test_create_thumbnail_with_given_dimensions_and_image_mode_in_create_thumbnail(self, image):
     attachment = Attachment(attachment=Mock())
     attachment.mimetype = "x/y"
     attachment.create_thumbnail(max_size=2)
     image.open.return_value.thumbnail.assert_called_once_with((2, 2), image.ANTIALIAS)
Example #31
0
def post(request,
         category,
         success_url=None,
         form_class=NewsForm,
         template_name='news/news_post.html',
         extra_context=None):
    if request.method == 'POST':
        form = form_class(data=request.POST, files=request.FILES)
        if form.is_valid():

            n = News(
                title=form.cleaned_data['title'],
                slug=form.cleaned_data['slug'],
                deliverer=form.cleaned_data['deliverer'],
                category=form.cleaned_data['category'],
                source=form.cleaned_data['source'],
                content=form.cleaned_data['content'],
                summary=form.cleaned_data['summary'],
            )

            n.save()

            kwargs = {}
            kwargs['title'] = _("%s Posted A new News: %s") % (
                self.deliverer.username, self.title)
            kwargs['content'] = _(
                "%s Posted A new News,go and view it now <a href='%s'> %s </a> "
            ) % (self.deliverer.username, self.get_absolute_url(), self.title)
            kwargs['slug'] = "news%d-%s" % (
                self.id, datetime.datetime.now().strftime("%Y%m%d%H%M%S"))
            latest_news_created.send(sender=self.__class__, **kwargs)

            for attachedfilefield in form.files:
                #attachment_file = form.files[attachedfilefield]
                #attach = Attachment()
                #attach.attached_by = request.user
                #attach.file        = attachment_file
                #attach.content_type = ContentType.objects.get_for_model(n)
                #attach.object_id   = n.id
                #attach.title       = attachment_file.name
                #attach.summary     = n.title
                #attach.file.save(attachment_file.name, attachment_file, save=False)
                #attach.save()

                attachment_file = form.files[attachedfilefield]
                attach = Attachment()
                attach.handle_uploaded_attachment(n,
                                                  attachment_file,
                                                  attached_by=request.user,
                                                  title=attachment_file.name,
                                                  summary=n.title)

                if attachedfilefield == u"file":
                    if not n.pic:
                        n.pic = attach.file_url()
                        n.save()

            return HttpResponseRedirect(n.get_absolute_url())

            #return HttpResponseRedirect(success_url or reverse('news-index'))
    else:
        c = get_object_or_404(Category, slug=category)
        form = form_class(initial={'category': c.id, 'title': 'ssss'})
        #form = form_class()

    if extra_context is None:
        extra_context = {}
    context = RequestContext(request)
    for key, value in extra_context.items():
        context[key] = callable(value) and value() or value
    return render_to_response(template_name, {'form': form},
                              context_instance=context)
Example #32
0
 def test_store_attachment_description(self):
     second = Second.objects.create(third_field="xyz")
     Attachment.objects.create(file_name="x.doc", attach_to=second, attachment="xxx",
                               attachment_type=Attachment.DOCUMENT, description="Three x's")
     self.assertEqual(Attachment.get_attachments_for(second).count(), 1)
Example #33
0
def newthread(request, forum):
    """
    Rudimentary post function - this should probably use
    newforms, although not sure how that goes when we're updating
    two models.

    Only allows a user to post if they're logged in.
    """
    if not request.user.is_authenticated():
        return HttpResponseServerError()

    f = get_object_or_404(Forum, slug=forum)

    if not Forum.objects.has_access(f, request.user.groups.all()):
    	return HttpResponseForbidden()

    if request.method == 'POST':
        form = CreateThreadForm(data=request.POST, files=request.FILES)
        #return HttpResponseRedirect('/POST'+str(request.FILES['file']))
        #return HttpResponseRedirect('/POST'+str(form.is_valid()))
        if form.is_valid():
            t = Thread(
                forum=f,
                author=request.user,
                title=form.cleaned_data['title'],
            )
            t.save()

            p = Post(
                thread=t,
                author=request.user,
                body=form.cleaned_data['body'],
                time=datetime.now(),
            )
            p.save()

            if form.cleaned_data.get('subscribe', False):
                s = Subscription(
                    author=request.user,
                    thread=t
                    )
                s.save()

            for attachedfilefield in form.files:
                #file_path = '%s%s' % (settings.MEDIA_ROOT, form.files[attachedfilefield])
                attachment_file = form.files[attachedfilefield]
                attach=Attachment()
                attach.handle_uploaded_attachment(p,
                    attachment_file,
                    attached_by = request.user,
                    title = attachment_file.name,
                    summary = t.title                   
                    )                  

            return HttpResponseRedirect(t.get_absolute_url())
    else:
        form = CreateThreadForm()

    return render_to_response('forum/newthread.html',
        RequestContext(request, {
            'form': form,
            'forum': f,
        }))
def create(request):
    form = MessageForm(request.POST)
    if form.is_valid():
        session = boto3.session.Session()
        s3_client = session.client(
            service_name='s3',
            endpoint_url='http://hb.bizmrg.com',
            aws_access_key_id='6Da62vVLUi6AKbFnnRoeA3',
            aws_secret_access_key=
            'gDYg4Bu15yUpNYGKmmpiVNGvLRWhUAJ3m1GGRvg8KTbU',
        )
        user = User.objects.get(username=request.POST['username'])
        chats = Member.objects.filter(user=user)
        for chat in chats:
            op = Member.objects.filter(chat=chat.chat).exclude(user=user).get()
            if op.user.username == request.POST['opponent']:
                result = chat.chat
                break
        message = Message(content=request.POST['content'],
                          chat=result,
                          user=user,
                          added_at=request.POST['date'])
        message.save()
        response = [message.id]
        result.last_message = '(' + request.POST['attach_type'] + ') ' +\
            request.POST['content'] if request.POST['attach_type'] != 'none' else request.POST['content']
        result.save()
        chat.last_read_message = Message.objects.filter(chat=result).filter(
            user=op.user).last()
        chat.save()
        attachs = {}
        attachs['type'] = request.POST['attach_type']
        if request.POST['attach_type'] == 'geolocation':
            attachment = Attachment(
                chat=result,
                user=user,
                message=message,
                attach_type='geolocation',
                url=request.POST['content'],
            )
            attachment.save()
            attachs['url'] = attachment.url
            response.append(attachment.id)
        else:
            attachs['url'] = []

        for file in request.FILES:
            key = 'attachments/' + request.POST[
                'attach_type'] + result.topic + '/' + str(hash(file))
            s3_client.put_object(
                Bucket='tsyrkov_messanger_bucket',
                Key=key,
                Body=request.FILES[file],
            )
            attachment = Attachment(
                chat=result,
                user=user,
                message=message,
                attach_type=request.POST['attach_type'],
                url=key,
            )
            attachment.save()
            attachs['url'].append(
                s3_client.generate_presigned_url(
                    'get_object',
                    Params={
                        'Bucket': 'tsyrkov_messanger_bucket',
                        'Key': attachment.url,
                    },
                    ExpiresIn=3600))
            response.append(attachment.id)

        # send message to client
        if attachs['type'] == 'audio':
            attachs['url'] = attachs['url'][0]
        avatar = s3_client.generate_presigned_url(
            'get_object',
            Params={
                'Bucket': 'tsyrkov_messanger_bucket',
                'Key': message.user.avatar,
            },
            ExpiresIn=3600)
        command_1 = {
            'method': 'publish',
            'params': {
                'channel': op.user.username + '_with_' + user.username,
                'data': {
                    'avatar': avatar,
                    'opponent': message.user.username,
                    'topic': result.topic,
                    'author': message.user.username,
                    'last_message': result.last_message,
                    'read': False,
                    'date': 'T'.join(message.added_at.split(' ')),
                    'message': message.content,
                    'attachments': attachs,
                }
            }
        }
        command_2 = {
            'method': 'publish',
            'params': {
                'channel': op.user.username,
                'data': {
                    'avatar': avatar,
                    'opponent': message.user.username,
                    'topic': result.topic,
                    'author': message.user.username,
                    'last_message': result.last_message,
                    'read': False,
                    'date': 'T'.join(message.added_at.split(' ')),
                    'message': message.content,
                    'attachments': attachs,
                }
            }
        }
        headers = {
            'Content-Type': 'application/json',
            'Authorization': 'apikey ' + API_KEY,
        }
        requests.post(url='http://localhost:9000/api',
                      data=json.dumps(command_1),
                      headers=headers)
        requests.post(url='http://localhost:9000/api',
                      data=json.dumps(command_2),
                      headers=headers)
        return JsonResponse({'success': response})
    return JsonResponse({'errors': form.errors}, status=400)
Example #35
0
def edit_article(request, title,
                 group_slug=None, bridge=None,
                 article_qs=ALL_WHITEBOARDS,
                 ArticleClass=Whiteboard, # to get the DoesNotExist exception
                 ArticleFormClass=WhiteboardForm,
                 template_name='edit_whiteboard.html',
                 template_dir='whiteboard',
                 extra_context=None,
                 check_membership=False,
                 is_member=None,
                 is_private=None,
                 *args, **kw):
    
    if group_slug is not None:
        try:
            group = bridge.get_group(group_slug)
        except ObjectDoesNotExist:
            raise Http404
        
        # permissions check
        if not group.user_is_member(request.user, admin_override=True):
            return HttpResponseForbidden()
        
        try:
            article = article_qs.get_by(title, group)
        except ArticleClass.DoesNotExist:
            article = None

        attach_forms = []
        if request.method == 'POST':
            try:
                attach_count = int(request.POST.get("attach_count", 0))
            except ValueError:
                attach_count = 0

            form = ArticleFormClass(request.POST, instance=article)
            attach_forms = [AttachmentForm(request.POST, request.FILES, prefix=str(x), instance=Attachment()) for x in range(0,attach_count)]
            
            # do not take blank attachment forms into account
            for af in attach_forms:
                if not af.is_valid() and not af['attachment_file'].data:
                    attach_forms.remove(af)
                    attach_count = attach_count - 1

            if form.is_valid() and all([af.is_valid() for af in attach_forms]):

                if request.user.is_authenticated():
                    form.editor = request.user
                    if article is None:
                        user_message = u"Whiteboard was created successfully."
                    else:
                        user_message = u"Whiteboard was edited successfully."
                    request.user.message_set.create(message=user_message)

                if ((article is None) and (group_slug is not None)):
                    form.group = group

                new_article, changeset = form.save()

                for af in attach_forms:
                    attachment = af.save(request, new_article)
            
                # FIXME: is there a more efficient way of finding the parent
                # than running these count() queries? 
                if new_article.topic.count():
                    url = new_article.topic.all()[0].get_absolute_url()
                elif new_article.event.count():
                    url = new_article.event.all()[0].get_absolute_url()
                else:
                    url = group.get_absolute_url() + "#group-whiteboard"
                    
                return redirect_to(request, url)

        elif request.method == 'GET':
            user_ip = get_real_ip(request)
    
            lock = cache.get(title, None)
            if lock is None:
                lock = ArticleEditLock(title, request)
            lock.create_message(request)
    
            initial = {'user_ip': user_ip}
            if group_slug is not None:
                # @@@ wikiapp currently handles the group filtering, but we will
                # eventually want to handle that via the bridge.
                initial.update({'content_type': get_ct(group).id,
                                'object_id': group.id})
    
            if article is None:
                initial.update({'title': title,
                                'action': 'create'})
                form = ArticleFormClass(initial=initial)
            else:
                initial['action'] = 'edit'
                form = ArticleFormClass(instance=article,
                                        initial=initial)
                
    
        template_params = {'form': form}
        template_params['attach_forms'] = attach_forms
    
        template_params['group'] = group
        if extra_context is not None:
            template_params.update(extra_context)
    
        return render_to_response(os.path.join(template_dir, template_name),
                                  template_params,
                                  context_instance=RequestContext(request))
Example #36
0
 def test_call_get_attachment_url_with_thumbnail_in_thumb(self):
     instance = Mock(spec=Attachment())
     Attachment.thumb(instance)
     instance.get_attachment_url.assert_called_once_with("thumbnail")
Example #37
0
def post(request, category, success_url=None,
			 form_class=NewsForm,
			 template_name='news/news_post.html',
			 extra_context=None):	
	
	c = get_object_or_404(Category.objects.all(), slug=category)
	if request.method == 'POST':
		form = form_class(data=request.POST, files=request.FILES)
		if form.is_valid():
		    			
			n = News(
				title     =form.cleaned_data['title'],
				slug      =form.cleaned_data['slug'],
				deliverer =form.cleaned_data['deliverer'],
				category  =form.cleaned_data['category'],
				source    =form.cleaned_data['source'],
				content   =form.cleaned_data['content'],
				summary   =form.cleaned_data['summary'],					   
			)
			
			n.save()

			kwargs = {}
			kwargs['title']   = _("%s Posted A new News: %s") % (self.deliverer.username,self.title)
			kwargs['content'] = _("%s Posted A new News,go and view it now <a href='%s'> %s </a> " ) % (self.deliverer.username,self.get_absolute_url(),self.title)
			kwargs['slug']    = "news%d-%s" % (self.id,datetime.datetime.now().strftime("%Y%m%d%H%M%S"))
			latest_news_created.send(sender=self.__class__,**kwargs)			


			for attachedfilefield in form.files:
				#attachment_file = form.files[attachedfilefield]
				#attach = Attachment()
				#attach.attached_by = request.user
				#attach.file        = attachment_file
				#attach.content_type = ContentType.objects.get_for_model(n)
				#attach.object_id   = n.id
				#attach.title       = attachment_file.name
				#attach.summary     = n.title
				#attach.file.save(attachment_file.name, attachment_file, save=False)
				#attach.save()

				attachment_file = form.files[attachedfilefield]
				attach = Attachment()
				attach.handle_uploaded_attachment(
					n,
					attachment_file,
					attached_by = request.user,
					title       = attachment_file.name,
					summary     = n.title
				)


				if attachedfilefield == u"file":
					if not n.pic:
						n.pic = attach.file_url()
						n.save()

			return HttpResponseRedirect(n.get_absolute_url())
			
			#return HttpResponseRedirect(success_url or reverse('news-index'))
	else:
		form = form_class(initial={'category':c.id, 'deliverer':request.user.id})
		#form = form_class()
	
	if extra_context is None:
		extra_context = {}
	context = RequestContext(request)
	for key, value in extra_context.items():
		context[key] = callable(value) and value() or value
	return render_to_response(template_name,
							  { 'form': form,
							    'category':c,
							  },
							  context_instance=context)
Example #38
0
def reply(request, thread):
    """
    If a thread isn't closed, and the user is logged in, post a reply
    to a thread. Note we don't have "nested" replies at this stage.
    """
    if not request.user.is_authenticated():
        return HttpResponseServerError()
    t = get_object_or_404(Thread, pk=thread)
    if t.closed:
        return HttpResponseServerError()
    if not Forum.objects.has_access(t.forum, request.user.groups.all()):
        return HttpResponseForbidden()

    if request.method == "POST":
        form = ReplyForm(data=request.POST, files=request.FILES)
        if form.is_valid():
            body = form.cleaned_data['body']
            p = Post(
                thread=t,
                author=request.user,
                body=body,
                time=datetime.now(),
                )
            p.save()

            sub = Subscription.objects.filter(thread=t, author=request.user)
            if form.cleaned_data.get('subscribe',False):
                if not sub:
                    s = Subscription(
                        author=request.user,
                        thread=t
                        )
                    s.save()
            else:
                if sub:
                    sub.delete()

            # Subscriptions are updated now send mail to all the authors subscribed in
            # this thread.
            mail_subject = ''
            try:
                mail_subject = settings.FORUM_MAIL_PREFIX
            except AttributeError:
                mail_subject = '[Forum]'

            mail_from = ''
            try:
                mail_from = settings.FORUM_MAIL_FROM
            except AttributeError:
                mail_from = settings.DEFAULT_FROM_EMAIL

            mail_tpl = loader.get_template('forum/notify.txt')
            c = Context({
                'body': wordwrap(striptags(body), 72),
                'site' : Site.objects.get_current(),
                'thread': t,
                })

            email = EmailMessage(
                    subject=mail_subject+' '+striptags(t.title),
                    body= mail_tpl.render(c),
                    from_email=mail_from,
                    to=[mail_from],
                    bcc=[s.author.email for s in t.subscription_set.all()],)
            email.send(fail_silently=True)

##            data={'title': t.title,
##                'summary': t.title,
##                'attached_timestamp':datetime.now()
##                }
##            attachment_form=AttachmentForm(data=data,files=form.files)
##            a=attachment_form.errors
##            content_type =ContentType.objects.get_for_model(Post)
##            object_id = p.id
##            ffs=form.files
##            debug()
##            if attachment_form.is_valid():
##                attachment = attachment_form.save(commit=False)
##                attachment.content_type = content_type
##                attachment.object_id = object_id
##                attachment.attached_by = request.user
##                attachment.save()

#            for attachedfilefield in form.files:
#                #file_path = '%s%s' % (settings.MEDIA_ROOT, form.files[attachedfilefield])
#                attachment_file = form.files[attachedfilefield]
#                file_path =os.path.join(ATTACHMENT_DIR, randomfilename(attachment_file.name))
#                (mimetype, encoding) = mimetypes.guess_type(file_path)
#
#                try:
#                    mime_type = mimetype
#                except:
#                    mime_type = 'text/plain'
#
#                attach=Attachment(
#                    content_type =ContentType.objects.get_for_model(Post),
#                    object_id = p.id,
#                    title = attachment_file.name,
#                    summary = t.title,
#                    attached_by = request.user,
#                    )
#                attach.save_uploaded_file(attachment_file)
#                attach.save()


            for attachedfilefield in form.files:
                #file_path = '%s%s' % (settings.MEDIA_ROOT, form.files[attachedfilefield])
                attachment_file = form.files[attachedfilefield]
                attach=Attachment()
                attach.handle_uploaded_attachment(p,
                    attachment_file,
                    request.user,
                    attachment_file.name,
                    t.title
                    )

            return HttpResponseRedirect(p.get_absolute_url())
    else:
        form = ReplyForm()

    return render_to_response('forum/reply.html',
        RequestContext(request, {
            'form': form,
            'forum': t.forum,
            'thread': t,
        }))