Django: Using a custom date/time picker in forms

The Django admin form has a pretty neat date/time picker, but it's troublesome to implement for every non-admin form you've got, especially when you don't want to expose your admin files to the public.

So how do you go about splitting the date and time values so you can use your own jQuery date pickers? Easy.

class SomeForm(forms.Form):
date_posted = forms.DateTimeField(widget = forms.widgets.SplitDateTimeWidget())

And that's all!

The rest of it depends on whatever you picked for the date picker.

wekwe
Don't choose something silly...

Sources

Git: Revert commit that has been pushed to origin

To bump off an accidental commit to origin, you'll have to sync your branch with the remote one.

Once that's done, we pop off the latest commit locally.

git reset HEAD^

The ^ means we're going back to the commit before "HEAD". Optionally you can use the option --hard to delete any changes made in that commit.

Now that we're back one commit, force it onto the remote master branch with +

git push origin +master

And that's it!

XJyGYChow Yun Fat approves of this method.

Source

Firefox: Firebug crashes when visiting sites Gawker sites

Man this one is annoying! Causes the browser to crash once you accidentally load the page. Makes you think twice about clicking on links from Google search results from Gizmodo or LifeHacker.

  • First the browser will freeze up.
  • If you wait long enough, it'll display a dialog

image

A script on this page may be busy, or it may have stopped responding. You can stop the script now, or you can continue to see if the script will complete.

 

Script: resource://firebug/firebug-service.js:3881

 

[ Continue ] [ Stop script ]

To prevent the crashes, you'll have to be mindful, BEFORE you click on that link.

If you've got Firebug toolbar button enabled, click on "Clear activation list". As long as the scripts panel is disabled for those sites, it should work fine.

image

If you can't find the button:

  • Right click on the toolbar
  • Click Customize
  • Find the Firebug icon
  • Drag it somewhere you can click on it
  • Remember to click Done!

I've created an issue in the bug tracker. Hopefully it'll get fixed soon!

tumblr_lrfglxXqVz1qhhrweo1_500
Stay tuned!

Git: Uninstall git-cheetah from menu

It's pretty neat to have but it wasn't the best user interface for me.

It was bloating my context menu with far too many options across my whole computer so I had to tranquilise it.

To do that, just open up a command prompt or the "Run" window (with Windows+R).

Assuming that you installed Windows Git to C:\Program Files\Git\

Type in:

cd %PROGRAMFILES%\git\git-cheetah && regsvr32 /u git_s
hell_ext.dll

And menu be-gone!

Now go and install something more intuitive like TortoiseGit.

tumblr_lydsbo5pcq1r1y5auo1_500

Source

GitHub: Committing code to your public repository without "Unknown" author name in commits

GitHub was easy to get up and running, but a few little quirks here and there got me.

Assuming you've:

  • Already created your repository in GitHub
  • Created an account in GitHub
  • Set up git for Windows and using git-bash

Create an SSH key

This bit allows to you check out and commit code securely to github.

To check if it already exists, type in:

cd ~/.ssh

If it works, skip to the next step.

If you get "No such file or directory", then you'll have to create one.

ssh-keygen -t rsa -C "your_email@youremail.com"

The email doesn't have to be your github email.

Let GitHub know your public key

Type in:

notepad ~/.ssh/id_rsa.pub

Copy everything from that file into the clipboard.

On the GitHub site click:

  • Account Settings
  • SSH Public Keys
  • Add another public key
  • Paste in everything from that file and give it a name of your computer (so you remember which computer can commit to the repo)

Test it out by typing (don't change it to your repo)

ssh -T git@github.com

Link your git to your github account

This step will link your github account to your commits.

On the site, go to:

  • Account Settings (top menu)
  • Account Settings (on the left menu)
  • Copy your API token.

Back in the git-bash console:

git config --global github.user your_github_username
git config --global github.token 0123456789yourf0123456789token

Okay, this time you enter in your github username. Replace the token with your token accordingly.

Set up your user details

To ensure that your commits show up correctly with your name, make sure you set your author name. Otherwise your commits will show up with "Unknown" as author name like here.

git config --global user.name "Firstname Lastname"
git config --global user.email "your_email@youremail.com"

Again, this doesn't have to be your github email.

Your name should not match the github username either.

Getting the code

Finally eh? Don't worry, all of the stuff above you only need to do once.

Go to your project page and just below the description, make sure "SSH" is highlighted.

imageNow click on the little clipboard icon to copy that URL (or you could do it manually)

Go to your coding folder (the one that holds all of your projects) and type:

git clone <paste> [optional_checkout_name]

In my example it'd be:

cd ~/android/projects/
git clone git@github.com:twig/Android-File-Dialog.git FileExplorer

That's because I wanted the directory name "FileExplorer" rather than "Android-File-Dialog" in my projects folder.

Commitment issues?

Make some changes to the code (like whitespaces or edit the readme) and type:

git add whatever_you_edited.txt
git commit -m"Test commit message"

That has committed the code locally.

To push it off to github, you'll have to type:

git push

It'll take whatever commits you've made and dump it onto github.

Refresh your project page to check that:

  1. You've committed code properly (yes, it DOES show up straight away!)
  2. Your author name is showing up correctly.
  3. You haven't broken anything ;)

Happy coding!

funny-hilarious-awesomeness-0

Sources

git: How to push a tag to remote origin

If you've created a tag on the local repository and want to push it to the origin repo, it's quite easy!

To update all the tags, just type:

git push --tags

Otherwise, type the following to update a specific tag:

git push --tags stable

Viola!

Source

Android: How to trigger Button View OnClickListener()

This method will work on any view, not just the Button view since it exists on the View base class.

All you have to do is call the View.performClick() method and it'll trigger whatever OnClickListener you've set in View.setOnClickListener().

Source

Javascript: String starts with snippet

I was surprised to find out that Javascript didn't come with such a function.

It's pretty easy to monkey patch though, it doesn't even need jQuery!

if (typeof String.prototype.startsWith != 'function') {
String.prototype.startsWith = function (input){
return this.substring(0, input.length) === input
};
}

After that you can just use it like any other string function.

var hash = "#someAnchor";

if (hash.startsWith('#img_')) {
$('a[href="' + hash + '"]').trigger('click');
}

SsgD9
Now we've trained Javascript well!

Source

Windows 7: Fix thumbnail previews not showing in Windows Explorer

Man, what a pain in the ass this was to debug. The image file previews weren't showing up at all, no matter what the settings were.

(Note that this is a guide for fixing image thumbnails, not videos)

Well, they sorta did. The image/picture thumbnail would flicker and appear then disappear, showing only the file icon instead.

I've tried all of the following:

  • Changing the settings in Windows Explorer > Folder View options
  • Changing the settings in Local Group Policy Editor (gpedit.msc) > User Configuration > Administrative Templates > Windows Components > Windows Explorer
  • Changing the settings in Advanced System Settings > Performance
  • Rebuilding of icon cache (requires reboot)
  • Using Disk Cleanup to delete thumbnail cache.
  • Checking the registry values for the file types.

*update 6/6/2013* Some additional tips from readers are:

  • Clear your recycle bin
  • Right click on Computer > Properties > Advanced System Settings > Performance > Settings > "Let Windows choose what's best for my computer" > OK

None of them worked. And I am unwilling to:

  • Format the drive (due to the time it'd take to reinstall and configure everything!)
  • Install a codec pack as an alternate method of rendering thumbnails (extra bloat)

But then I found a solution by accident... without having to format. Here's a clue.

image

Yep. That's right! If you're low on disk space then it wont generate thumbnails for you.

I moved some big files off my desktop onto another drive and the thumbnails immediately started working again!

I think the minimum space you need on your Windows drive is 500mb. I could be wrong, but after moving the files everything was fine again.

*update 21/6/2012* Adding link to previous post: Windows: Regain disk space on C drive with some uncommon methods

DADT-repealed 
I think this sums up the hidden side-effect of low disk space fairly well...

Sources

These sources didn't work for me, but in the case clearing up disk space didn't work for you, here were some of the more useful links I found.

Javascript: Render file size in a human readable format to two decimal places

Displaying the size of files is one thing but formatting them so they're actually understandable by humans, Another.

I've found this useful snippet by inkdeep but corrected the bit/byte units, used a different rounding function and cleaned up the if blocks.

function humanize_filesize(fs) {
if (fs >= 1073741824) { return round_number(fs / 1073741824, 2) + ' GB'; }
if (fs >= 1048576)    { return round_number(fs / 1048576, 2) + ' MB'; }
if (fs >= 1024)       { return round_number(fs / 1024, 0) + ' KB'; }
return fs + ' B';
};

This snippet uses round_numbers() which I've written in another post.

Source

Javascript: Round numbers off to X decimal places

Simple little snippet which I was surprised wasn't part of the Math class.

function round_number(num, dec) {
return Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
}

Source

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog