Example #1
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 #2
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 #3
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()