Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

http://kevinnmurphy.com

Django and Open-flash-charts

// Found this on the django-users mailing list, need to test
ok, I finally got open-flash-chart to work.

1. create an xhtml-file and insert (something like) this:

    <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
        codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/
flash/swflash.cab#version=8,0,0,0"
        width="600"
        height="400"
        id="graph-2"
        align="middle">
    <param name="allowScriptAccess" value="sameDomain" />
    <param name="movie" value="/media/site/chart/open-flash-chart.swf?
width=600&height=400&data=/chart_data/" />
    <param name="quality" value="high" />
    <param name="bgcolor" value="#FFFFFF" />
    <embed src="/media/site/chart/open-flash-chart.swf?
width=600&height=400&data=/chart_data/"
        quality="high"
        bgcolor="#FFFFFF"
        width="600"
        height="400"
        name="open-flash-chart"
        align="middle"
        allowScriptAccess="sameDomain"
        type="application/x-shockwave-flash"
        pluginspage="http://www.macromedia.com/go/getflashplayer"; />
    </object>

2. define the url for the chart, eg: (r'^chart/$',
'www.views.charts.chart'),

3. define the url for the chart-data, e.g.: (r'^chart_data/$',
'www.views.charts.chart_data'),

4. the views:

def chart(request):

   ### nothing really required. whatever you want to do here. points
to the html-file created above

    return render_to_response('site/charts/chart.html', {
        'var': 'var',
    }, context_instance=RequestContext(request) )


def chart_data(request):

    import random

    g = graph()

    data_1 = []
    data_2 = []
    data_3 = []
    for i in range(12):
      data_1.append( random.randint(14,19) )
      data_2.append( random.randint(8,13) )
      data_3.append( random.randint(1,7) )

    g.title('PageViews (2007)', '{color: #999999; font-size: 16; text-
align: center}' );
    g.bg_colour = '#ffffff'

    # we add 3 sets of data:
    g.set_data( data_1 )
    g.set_data( data_2 )
    g.set_data( data_3 )

    # we add the 3 line types and key labels
    g.line_dot( 3, 5, '#333333', 'Page views', 10 )
    g.line_dot( 3, 5, '#666666', 'Visits', 10)    # <-- 3px thick +
dots
    g.line_hollow( 2, 4, '#999999', 'Unique visitors', 10 )


g.set_x_labels( 
'Jänner,Februar,März,April,Mai,Juni,Juli,August,September,Oktober,November,Dezember'.split(',')
 )
    g.set_x_label_style( 11, '0x666666', 2, 1)

    g.set_y_max(20)
    g.y_label_steps(4)
    g.set_y_label_style( 10, '0x666666')

    return HttpResponse(g.render())


Hack for Expression Engine for file attachments

// description of your code here

Hm- not out of the box.  There may be a plugin/extension floating around for it.  I’ve hacked it- was pretty easy.  The core email class already has the code you need for attaching files.  So you pretty much just have to modify the mod.email.php file- around 207 I changed the content type:
$data['enctype']                 = 'multipart/form-data';

Around 530 I added an array of uploaded files:
// hack for uploads
        $att = array();
        

        if (isset($_FILES) AND (count($_FILES) > 0))
        {
            $att_ret = $this->upload_file();
            foreach ($att_ret as $key=> $val)
            {
            $att[] = '/home/whatever/mail_attach/'.$val['name'];
            }
        }

Around 865 you need to tie the attached files to the email:
// hack for attachments
        if (count($att) > 0)
        {
        foreach($att as $val)
        {
                $email->attach($val);
        }
        }

After the emails are sent, I deleted the files:
// hack- delete the attached files from server
        
        if (isset($att) AND count($att) > 0)
        {
            foreach ($att as $val)
            {
                @unlink($val);
            }
        }

Then you need to add a function to handle the actual uploading- it’s easy because you can use the upload class- I think I modeled it after the file upload module because I needed to be able to upload multiple files.  Anyway- this part is a bit dangerous as you’re letting any idiot upload stuff.  So watch that you keep things as secure as possible- be sure everything is gettingscrubbed down’ and such. 

JavaScript Roll Overs

// JavaScript roll overs

<script type="text/javascript">
<!--
function MM_swapImgRestore() { //v3.0
 var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
 var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
 if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
 var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
 d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
 if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
 for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
 if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
 var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
 if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>

// html code
<a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image1','','2.png',1)"><img src="1.png" name="Image1" width="155" height="71" border="0" id="Image1" /></a>