Ejemplo n.º 1
0
 def handle(self, *args, **options):
     bleeps = Bleep.objects.filter(bleep_status='qued')
     for bleep in bleeps:
         # Dispatch the bleep
         BleepService.dispatch(bleep)
         self.stdout.write('dispatched bleep %s to %s service\n' %
                           (str(bleep.id), bleep.bleep_service))
Ejemplo n.º 2
0
 def handle(self, *args, **options):
     bleeps = Bleep.objects.filter( bleep_status='qued' )
     for bleep in bleeps:
         # Dispatch the bleep
         BleepService.dispatch(bleep)
         self.stdout.write('dispatched bleep %s to %s service\n' 
                           % (str(bleep.id), bleep.bleep_service))
Ejemplo n.º 3
0
def send(request, bleep_id):
    print 'debuggery: dispatching %s to external service' % bleep_id
    """
    Dispatch the bleep 

    """
    b = get_object_or_404(Bleep, pk=bleep_id)
    BleepService.dispatch(b)        
    # return to page
    return render_to_response('bleep/detail.html', { 'bleep': b},
                              context_instance=RequestContext(request))
Ejemplo n.º 4
0
def show(request, service_type):
    """
    Show the service 

    """
    service = BleepService.get_service(service_type)
    form_class = BleepService.form_class(service_type)
    form = form_class()
    return render_to_response('services/detail.html',
                              {'service': service,
                               'form':form},
                              context_instance=RequestContext(request))
Ejemplo n.º 5
0
 def handle(self, *args, **options):
     # get the list of bleeps
     digest_date = datetime.date.today()
     bleep_list = Bleep.objects.filter(bleep_pub_date__gte=digest_date)
     # get the subscribers
     subscribers = UserProfile.objects.filter(
         daily_digest_subscription=True)
     # get an email serivce
     service = BleepService.get_service('email')
     # process the subscribers
     for subscriber in subscribers:
         # Look up the user
         user = subscriber.user
         self.stdout.write('creating digest for user: %s\n' % user.username)
         # set up the context_data
         context_data = {
             'bleep_message': 'Daily bleep digest',
             'digest_date': digest_date,
             'email_template_html': 'email/digest.html',
             'email_template_text': 'email/digest.txt',
             'email_recipients': subscriber.user.email,
             'email_from': '*****@*****.**',
             'bleeps': bleep_list
         }
         service.perform(context_data)
Ejemplo n.º 6
0
def perform(request, service_type):
    if request.method == 'POST':
        # get the form class
        form_class = BleepService.form_class(service_type)
        form = form_class(request.POST)
        service = BleepService.get_service(service_type)
        form_dict = form.data.copy()
        if form_dict.has_key('data'):
            print 'debuggery: parsing form dictionary data...'
            parsed_dict = PyDictParser.string_to_dict(form_dict['data'])
            form_dict.update(parsed_dict)
            del form_dict['data']
        print 'calling perform()...'
        result = service.perform(form_dict)
        return render_to_response('services/detail.html',
                                      {'service': service,
                                       'result':result.get_msgs(),
                                       'form':form},
                                      context_instance=RequestContext(request))
Ejemplo n.º 7
0
  def perform(self, reqdata):
    print 'debuggery: listing items in reqdata...'
    for k,v in reqdata.iteritems():
      print 'debuggery: reqdata: '+k+"="+str(v)

    # Verify required items in reqdata exists
    for keyname in ['email_from','email_recipients']: 
      if not keyname in reqdata:
        raise ValueError("missing item from reqdata: %s " % keyname)
    
    # Get the parent directory for this source file. 
    #  Needed for the template location.
    parent_dir = os.path.join(os.path.dirname(__file__), "..")

    reqdata['site_url'] = BleepService.get_site_url()
    # Compose the email message
    msg = MIMEMultipart('related')
    msg['Subject'] = "{0}".format(
      reqdata['bleep_message'] if 'bleep_message' in reqdata else 'no subject')
    msg['From']    = reqdata['email_from'] 
    msg['To']        = reqdata['email_recipients']
    msg.preamble = 'This is a multi-part message in MIME format.'
    msgAlt = MIMEMultipart('alternative')
    msg.attach(msgAlt)

    # Create the alternative text format
    # retrieve the text template
    template_name = reqdata['email_template_text'] if reqdata.has_key('email_template_text') else 'email/email.txt'
    text_template = self.__load_template(template_name)
    text = self.__expand_message_content(reqdata, text_template)
    part1 = MIMEText(text, 'text')
    msgAlt.attach(part1)
    # Create the alternative html format (preferred)
    template_name = reqdata['email_template_html'] if reqdata.has_key('email_template_html') else 'email/email.html'
    html_template = self.__load_template(template_name)
    html = self.__expand_message_content(reqdata, html_template)
    part2 = MIMEText(html, 'html')
    msgAlt.attach(part2)
    # Read the image data and define the image's ID
    msgImage = self.__logo_attachment(parent_dir)
    msg.attach(msgImage)
    try:
      smtp = self.__connect(parent_dir)
      # Send the email !
      print 'debuggery: sending the email message'
      smtp.sendmail(reqdata['email_from'], 
                    reqdata['email_recipients'], msg.as_string())
      smtp.quit()
    except smtplib.SMTPException as (err):
      raise BleepServiceError("Unable to send email. " + err.message)
    except Exception as (exc):
      raise BleepServiceError("Unexpected SMTP error. " + str(exc))
    self.get_result().add_msg('Succesfully sent email to ' + reqdata['email_recipients'])
    return self.get_result()
Ejemplo n.º 8
0
def list(request):
    """
    List the bleep services

    """
    services = BleepService.list()
    parsers = BleepParser.list()
    return render_to_response('services/list.html',
                              {'services_list': services,
                               'parsers_list': parsers},
                              context_instance=RequestContext(request))
Ejemplo n.º 9
0
def add(request):
    """
    Add a bleep to the bleepdom

    """
    print 'debuggery: inside add function...'
    if request.method == 'POST':
        # merge the GET and POST params
        reqParams = request.POST.copy()
        reqParams.update(request.GET)
        form = BleepForm(reqParams)
        if form.is_valid():
            print 'debuggery: add form is valid'
            # Initialize the date and status            
            new_instance = form.save(commit=False)
            new_instance.bleep_status='qued'
            new_instance.bleep_pub_date = datetime.datetime.now()
            # Collect the input params
            new_instance.bleep_get_data = request.META['QUERY_STRING']
            if request.raw_post_data:
                new_instance.bleep_post_data = request.raw_post_data
            # Save the data to the model
            new_instance.save()
            if not settings.NO_AUTO_SEND_MESSAGE:
                # send it!
                print 'debuggery: Automatically sending the bleep...'
                result = BleepService.dispatch(new_instance)
            else:
                print 'debuggery: bleep is being held in queue for later processing'
            # Return to the new bleep page
            return HttpResponseRedirect('/bleeps/'+str(new_instance.id))
        else:
            logger.debug('the add form was NOT valid')
    else:
        return render_to_response('bleep/create.html', {
                'form':BleepForm(
                    initial={'bleep_status':'qued',
                             'bleep_pub_date':datetime.datetime.now()})},
                                  context_instance=RequestContext(request))        
Ejemplo n.º 10
0
 def handle(self, *args, **options):
     # get the list of bleeps
     digest_date = datetime.date.today()
     bleep_list = Bleep.objects.filter(bleep_pub_date__gte=digest_date)
     # get the subscribers
     subscribers = UserProfile.objects.filter(daily_digest_subscription=True)
     # get an email serivce
     service = BleepService.get_service("email")
     # process the subscribers
     for subscriber in subscribers:
         # Look up the user
         user = subscriber.user
         self.stdout.write("creating digest for user: %s\n" % user.username)
         # set up the context_data
         context_data = {
             "bleep_message": "Daily bleep digest",
             "digest_date": digest_date,
             "email_template_html": "email/digest.html",
             "email_template_text": "email/digest.txt",
             "email_recipients": subscriber.user.email,
             "email_from": "*****@*****.**",
             "bleeps": bleep_list,
         }
         service.perform(context_data)