Wednesday 12 February 2014

Using Comfortable Mexican Sofa's fixtures on Heroku

Hi all,
If you have been using Comfortable Mexican Sofa you might have been wondering "how do i backup the cms data?"
The team on CMS added a fixture capabilities, however these features does not really work on heroku:
CMS is creating all the content as files, even the files uploaded will be downloaded locally, the problem is that on heroku, local means files that will be deleted as soon as the task is over.
My way of working around it was to upload all the files to S3, and pulling it from S3 for restoring that content.
On my case, we have an heroku app for staging and production app, with fixtures i can move the CMS content from one app to the other.

I've created a two rake tasks, one for exporting, and one for importing.



namespace :fixtures do
  def prepare_folder!(path)
    FileUtils.rm_rf(path)
    FileUtils.mkdir_p(path)
  end

  desc "Run rule engine"
  task :export => :environment do
    #create the fixtures files
    from  = ENV['FROM'] || "appname"
    to    = "#{Date.today.strftime("%Y%m%d")}"
    ComfortableMexicanSofa::Fixture::Exporter.new(from, to).export!

    # create a connection
    connection = Fog::Storage.new({
      :provider                 => 'AWS',
      :aws_access_key_id        => GLOBAL['s3_key'],
      :aws_secret_access_key    => GLOBAL['s3_secret']
    })

    directory = connection.directories.get("fixtures")

    #GO OVER ALL FILES AND UPLOAD
    Dir.glob("db/cms_fixtures/#{to}/**/*.*") do |rb_file|
      # do work on files ending in .rb in the desired directory
      file = directory.files.create(
        :key    => rb_file,
        :body   => File.open(rb_file),
        :public => true
      )
    end
  end

  task :import => :environment do
    to  = ENV['TO'] || "appname"
    from    = "#{Date.today.strftime("%Y%m%d")}"
    path  = "db/cms_fixtures/#{from}/"

    prepare_folder!(path)

    # create a connection
    connection = Fog::Storage.new({
      :provider                 => 'AWS',
      :aws_access_key_id        => GLOBAL['s3_key'],
      :aws_secret_access_key    => GLOBAL['s3_secret']
    })

    directory = connection.directories.get("fixtures", prefix: "db/cms_fixtures/#{from}/")

    directory.files.each do |file|
      url = file.public_url
      folder = url.match("db/cms_fixtures/#{from}/.+/")[0]
      prepare_folder!(folder) unless Dir.exist?(folder)
      open(url.match("db/cms_fixtures/#{from}/.*")[0], 'wb') do |f|
        open(url) { |src| f.write(src.read) }
      end
      puts "Copied #{url}"
    end

    ComfortableMexicanSofa::Fixture::Importer.new(from, to, :force).import!
  end
end

For that to work you are going to need to state as the "TO" or "FROM" parameter the name of the site (the identifier) in the CMS, also you will need to create the bucket named fixtures as well as credentials for S3.
this code is creating under the fixtures bucket a "directory"(there are no directories in S3, the file names are just directory like e.g. "folder/file.txt") with the date and stores all data there.

Thursday 19 January 2012

Outputing the url value of a SharePoint hyperlink field

So as i was working on a SharePoint project i had, i needed to display an image i get from the a hyperlink field inside a repeater.
The problem was that i kept getting the link twice, and i was burning all my brain cell and Google capabilities trying to find a normal solution(parsing the string is not an optimal solution obviously), after a long long search, i came upon this lovely link.
And from there, it was fairly simple, despite my dislike for doing so many unnecessary casting, the end code is as follows:

<img src="<%# (new SPFieldUrlValue(((SPListItem)Container.DataItem)["ChildPicture"].ToString()).Url) %>" alt="ChildPicture" />

Thursday 15 December 2011

How to open office files inside current window

I had a bug today when a program i was working on didn't work, after fixing the file associations, it worked for windows xp, but one of my clients need it to work for windows 7, however in windows 7 there is no real file association menu.
This fix will help you mostly if you are trying to open office files in the same browser window, and:
a) it opens in new office document.
b) it prompt is not displayed(it might be displayed after a few alt+tab)

This will fix it, there will be no prompt of "what do you want to do with", or "do you want to save or open this file", nor will it attempt to open it in a word window, instead it will open in the browser / WebBrowser window.

The fix is simple, make a text file, copy the next content, rename to something.reg, and run. you can incorporate it into the installer of you program to make sure the clients run it.
content:


Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Word.Document.8]
"BrowserFlags"=dword:80000024

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Word.RTF.8]
"BrowserFlags"=dword:80000024

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Word.Document.12]
"BrowserFlags"=dword:80000024

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Word.DocumentMacroEnabled.12]
"BrowserFlags"=dword:80000024

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Excel.Sheet.8]
"BrowserFlags"=dword:80000A00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Excel.Sheet.12]
"BrowserFlags"=dword:80000A00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Excel.SheetMacroEnabled.12]
"BrowserFlags"=dword:80000A00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Excel.SheetBinaryMacroEnabled.12]
"BrowserFlags"=dword:80000A00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\PowerPoint.Show.8]
"BrowserFlags"=dword:800000A0

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\PowerPoint.Show.12]
"BrowserFlags"=dword:800000A0

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\PowerPoint.ShowMacroEnabled.12]
"BrowserFlags"=dword:800000A0

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\PowerPoint.SlideShow.8]
"BrowserFlags"=dword:800000A0

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\PowerPoint.SlideShow.12]
"BrowserFlags"=dword:800000A0

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\PowerPoint.SlideShowMacroEnabled.12]
"BrowserFlags"=dword:800000A0


[HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\AttachmentExecute\{0002DF01-0000-0000-C000-000000000046}]
"Word.Document.8" =dword:0
"Word.RTF.8" =dword:0
"Word.DocumentMacroEnabled.12"=dword:0
"Word.Document.12"=dword:0
"Word.DocumentMacroEnabled.12"=dword:0
"Excel.Sheet.8"=dword:0
"Excel.Sheet.12"=dword:0
"Excel.SheetMacroEnabled.12"=dword:0
"Excel.SheetBinaryMacroEnabled.12"=dword:0
"PowerPoint.Show.8"=dword:0
"PowerPoint.Show.12"=dword:0
"PowerPoint.ShowMacroEnabled.12"=dword:0
"PowerPoint.SlideShow.8"=dword:0
"PowerPoint.SlideShow.12"=dword:0
"PowerPoint.SlideShowMacroEnabled.12"=dword:0




Of course you can expand this for other files types, just add the names they are defined under in the registry.
If you are wondering, the first half ( up to [HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\AttachmentExecute\{0002DF01-0000-0000-C000-000000000046}]) is about the new window, the other is fixing the prompt.

The following KB resources were used:
http://support.microsoft.com/kb/927009
http://www.winhelponline.com/blog/reset-the-always-ask-before-opening-this-type-of-file-setting/

Sunday 22 May 2011

Quick post: png in IE7

After i lost the last hour and a half trying to figure out why my site was crawling to a halt and using 100% of a CPU(actually 25% as i'm using a quad core), i figured out the problem.

NEVER. EVER. use a background repeat with a .png image, it seems that using the .png repeat made the .png engine in IE7 render each repeated item individually, making a large transparent background a living hell for the CPU.

the best method to avoid it is not using a smaller image with repeat, and resizing the image to full size, or just use .gif, as it provide transparency, and also works on the old(and bad) IE6.

p.s.
i hate you Microsoft.

Tuesday 22 February 2011

Why is my span tag acting all freakishly?

When you try to design component, you will sometimes need to use the design tag as inline, and sometimes as block, for that purpose there are two tags, the span and the div.

The <span> is used for inline elements, elements you want to use while inline, meaning they do not break new line after.
The <div> is used for block elements, usually used for dividing the page into seperate parts, such as header, main zone, footer etc. as such, it will start a new line after it is completed.
There are other differences, such as inline not receiving height properties.

Of course both can be turned into the other type of display, simply by using the css property "display" to inline or block, or even better, the display type of inline-block, letting your inline items have a height and enjoy the best of both world(and enjoying the havoc of trying to fix the design for IE, as it tends to display it differently for each version).
For example: <div style="display:inline;">.

However, you should note, span is defaulted for inline, as such, it is supposed to contain some content, so if you use it for design(such as using it for a background, with no text inside) it will act weird, the best way to fix it is by adding some content, and the best content? a space that will not be ignored, achieved by the the " &nbsp; " key-word(without the quotation).
Oh, and do take note, if your span is across several lines(not starting and ending in the same line), the line break may cause some chaos, so it is best to keep it in one line, or commenting out the new line(simply start a comment at the end of the line, and end it at the beginning of the first element in the next line).

Sunday 20 February 2011

fixing asp:menu for google chrome asp.net

As some of you using asp.net might notice, Google Chrome has a quirk, it likes simplifying the html code. it isn't very noticeable, most of you will never notice, but when you use the control, the css just doesn't seem to work right for Chrome, the fix for this is to add the following code to your base.master code, or if you dont have a master, to the page's code.

protected override void AddedControl(Control control, int index)
{
if (Request.ServerVariables["http_user_agent"]
.IndexOf("Safari", StringComparison.CurrentCultureIgnoreCase) != -1)
{
this.Page.ClientTarget = "uplevel";
}
base.AddedControl(control, index);
}

In case you are wondering why i use the safari as the user agent, that is because Safari and Chrome use the same rendering engine, the WebKit, thus making this fix good for both browsers.
In case you are using Safari and annoyed about only talking about Chrome, don't be insulted that i only talk about Google's Chrome, after all, chrome is 5 times more poplar (Chrome's market share is at around 13% of all browsers usage, while Safari is only at around 2%)

Friday 8 January 2010

Stocks Displayer c#

I decided i wanted to be able to know what the stocks are for the yen to my local currency(shekel), but didn't like the bank`s graph(they put days that do not have trade on the graph), so i took the raw data, and made a graph.
Had to create a list for easy checking of the parsed data.

It might be interesting to someone that wants to know how to make a graph on c# or how to parse data without regex.
public partial class Form1 : Form
{
List<data> lst=new List<data>();

public Form1()
{

InitializeComponent();
string url="http://www.bankisrael.gov.il/eng.shearim/select_cur/cur4sdh.php?curname%5B%5D=_31%2B1977-10-31%2B%2BYen+-+Japan%A0&numselect=1&curfile=htm&fyear=2008&fmonth=12&fday=31&lyear=2009&lmonth=12&lday=31&cboday=01&cboyear_from=2008&cboyear_to=2009&lastwom=93&whosend=last";
string shitload;
System.Net.WebClient wc=new System.Net.WebClient();
shitload=wc.DownloadString(url);

//parsing
shitload=shitload.Remove(0,shitload.IndexOf("Exchange rates - Last 3 months"));
shitload=shitload.Remove(0,shitload.IndexOf("<TABLE width"));
shitload=shitload.Remove(shitload.IndexOf("</table>"));
string date;
try{
while (shitload.Length>100)
{
shitload=shitload.Remove(0,shitload.IndexOf("nowrap>")+7);
date=shitload.Substring(0,10);

shitload=shitload.Remove(0,shitload.IndexOf("nowrap>")+7);

lst.Add(new data(date,shitload.Substring(0,8)));

}
}
catch{}

label1.Text=shitload;

foreach (data dta in lst)
listBox1.Items.Add(dta.date+" "+dta.value);


this.Paint += new PaintEventHandler(paintgraph);


}
private void paintgraph(object sender,PaintEventArgs e)
{
Graphics g=e.Graphics;
g.Clear(Color.Wheat);
Pen pn=new Pen(new SolidBrush(Color.Red),2);
float min,max;
int mini=0,maxi=0;
min=100;max=0;
for (int i=0;i<lst.Count;i++)
{
if (lst[i].value<min) {min=lst[i].value;mini=i;}
if (lst[i].value>max) {max=lst[i].value;maxi=i;}
}
int inc=this.Width/lst.Count;
float dif=max-min;
float incy=(this.Height-40)/dif;
for(int i=0;i<lst.Count-1;i++)
{
g.DrawLine(pn,i*inc,(-lst[i].value+max)*incy,(i+1)*inc,(-lst[i+1].value+max)*incy);
Application.DoEvents();
}
if (lst.Count-1!=maxi)
g.DrawString(lst[maxi].date+" "+lst[maxi].value,new Font("Verdana",10),new SolidBrush(Color.Tomato),maxi*inc,(-lst[maxi].value+max)*incy);
else
g.DrawString(lst[lst.Count-1].date+" "+lst[lst.Count-1].value,new Font("Verdana",10),new SolidBrush(Color.Tomato),(lst.Count-1)*inc-130,(-lst[lst.Count-1].value+max)*incy);
if (lst.Count-1!=mini)
g.DrawString(lst[mini].date+" "+lst[mini].value,new Font("Verdana",10),new SolidBrush(Color.Tomato),mini*inc,(-lst[mini].value+max)*incy-20);
else
g.DrawString(lst[lst.Count-1].date+" "+lst[lst.Count-1].value,new Font("Verdana",10),new SolidBrush(Color.Tomato),(lst.Count-1)*inc-130,(-lst[lst.Count-1].value+max)*incy-20);
//if ((lst.Count-1!=maxi)&&(lst.Count-1!=mini))


}


void Form1ResizeEnd(object sender, EventArgs e)
{
this.Refresh();
}
}

public class data
{
public string date;
public float value;
public data(string d,string v)
{
date=d;
value=(float)System.Convert.ToDouble(v);
}
}