Django: Using the ORM to (automatically) create custom database field types

When creating the full text index search layer in Django, I had a little issue when new models were created.

Luckily, it's quite easy to create custom database types in your models.

class TSearchVectorField(models.fields.TextField):
"""
This lets Django know how to create a field with the "tsvector" type.
"""
description = "Tsearch Vector"

def db_type(self, connection):
return 'tsvector'


class TSearch(models.Model):
fti = TSearchVectorField()

flock-of-seagals
Elegant, like a flock of Seagals.

Source

Python: Find all subclasses of a class

I would have thought there was some trickery or magic to this, or some sort of hidden Django function/signal to use.

But NOPE! With Python, it's a one line wonder!

# Get all subclasses for class
type.__subclasses__(Subscription)

That's it! Best part is there's no special imports to do, "type" is already a built-in, so it's available anywhere at any time.

Cpv5G
High 5's all round, fuck yeah!

Windows 8.1 or 8 (Consumer Preview): Err, how to shut down?


This blog post is intended to express my initial impressions of the Windows 8 Consumer Preview.
  • Move your mouse cursor to the bottom right corner over the magnifying glass
  • Wait for the menu to appear.
  • Click on "Settings"
  • Click on "Power"
  • Click on "Shut down"
*edit 20/1/2014* I've updated this post for Windows 8.1.
  • Right click on the start button
  • "Shut down or sign out"
  • Click "Shutdown"
For those who are only after a solution for shutting down, you can skip the rest of this post.

Initial Impression

I know, sounds silly right? I had to Google "How to shut down Windows 8". Well that was my first experience with Windows 8.
Windows 8-2012-03-11-11-26-39 copy 
Had to scroll, but I joined 3 screenshots together.
The first impression I got from this whole metro experience?
"The fuck is this shit?"
Microsoft seems to have a bit of difficulty understanding that you can't shoehorn a desktop OS into a tablet. But that's ok, they've shoehorned a tablet OS into a desktop.
The touch-screen oriented user interface is now perfect for my keyboard and mouse. Making me scroll from left to right with the mouse to search for apps organised in a seemingly random order is extremely easy, that's why majority of the websites scroll horizontally!
For those who didn't spot it, that previous paragraph was intended to be extremely sarcastic.
image
After the initial metro culture shock, I looked around for an out. I wanted the desktop. Glad to see they didn't destroy that yet.
 Windows 8-2012-03-11-11-26-55
So the desktop looks just like Windows 7, without the start menu. Sure, that's fine. I think I can do without that...
But my heart sunk even lower after clicking on Windows Explorer.
Windows 8-2012-03-11-11-28-44
Lo and behold, ribbons. Yep, transformation complete. Those "love it or hate it" ribbons from Office 2007 have been ported into the Windows 8 Windows Explorer. This sealed the deal, I am not upgrading to this cursed odd cycle OS.

Hybrid user interface

So far, I don't think this OS should be shipped. Why? Plainly because it's a filthy mongrel.
The user interface (outside of metro) is not geared for touch-interface users. The icons and buttons are too small for fingers so it's too easy to click the wrong screen elements.
However, for keyboard and mouse users, the whole touch-centric interface is nothing short of frustrating. You can't swipe or pinch, so you're forced to work with the mouse in an unnatural way.
I'm no fan of Apple products but at least they got something clear from the get-go. Keep the touch and desktop operating systems as separate software entities!

Shutting down...

I'll play around a bit more with Win8 later. But because I installed this at 5am in the morning, I needed to find the shutdown button. Wait, what shutdown button!?
Windows 8-2012-03-11-11-27-23
Clicking on the magnifying glass on the bottom right corner gave me an overview of all the app icons. Too small to be legible, too empty to be useful.
I clicked randomly to zoom back in. I took a second look through all the apps to find something that may lead to a shutdown. Nothing.
After moving my mouse cursor back to the magnifying glass I realised it was a futile attempt to click it again in hope something different would happen, so I stopped to think.
And that's when it happened. Rather than displaying a tooltip of what the magnifying glass is supposed to do, a menu appeared!
Windows 8-2012-03-11-11-27-51 
Oh, so NOW you want to show me the time?
Thanks Mr Wolf!
But what's this? Settings? Sure it's 5am, I don't mind browsing through some settings quickly.
Windows 8-2012-03-11-11-28-02 image
Sup dawg I heard you like settings.
So after clicking on Settings I had to click into "Settings" again. Somebody at Microsoft had just discovered memes and this was their sick interpretation of an Easter egg... There were only two options, just enough for the pluralisation of "Settings" to be valid.
image
So I tried again and found the "More PC Settings" label below.
Sadly, I completely missed the part which says "Power" above it. Partly because I was looking for "settings" (I mean, why else would I be there right?) and also due to the fact that ALL THE ICONS ARE WHITE.
After years of using Windows you've come to learn that power or shutdown buttons are red, easy to spot out in a big sea of icons. Hipster Microsoft said, "Fuck that! With our Metro styling everything will look the same."
image
After messing with all the options, something dawned upon me...
Windows 8-2012-03-11-12-22-23
There's no way out. No "Back", "Apply" or "OK" buttons of sorts. FUCK Ballmer, what is this half baked shit? What happened to eating your own dog food?
After minutes of frustration (in computing time it feels like hours) trying all possible menus and the escape button, my last resort was to press the Windows button.
Sweet mother of God, it worked. I was thrown back to the shitty Metro home screen.
I clicked on the "Settings" button again, but thought "Do I really wanna go there again?". And that's when I noticed the subtle "Power" icon.
Windows 8-2012-03-11-11-28-06
Screw this, I'm outta here.
Microsoft, you've really dropped the ball here. Your competitors are gaining ground, what you gonna do?
  • You're losing desktop systems to Mac OSX
  • Windows 7 phones is barely picking up traction
  • You're losing browser turf to Chrome and Firefox
  • And Nintendo is still decimating the gaming market
Think clearly before dropping Windows 8 onto shelves.

Django: Sending out a HTML email... with attachments

The usual django mail.send_mail() is sufficient for most cases if you're sending by plain text. It does leave a little to be desired, such as changing it to a HTML format or something.

However, if you're after an email with attachments, you'll have to get a little busy.

from django.core.mail.message import EmailMessage

email = EmailMessage()
email.subject = "New shirt submitted"
email.body = message
email.from_email = "ThatAwesomeShirt! <noreply@thatawesomeshirt.com>"
email.to = [ "whoever@whatever.com", ]

# Save the file to a temporary file because that's how email attachments work
output = StringIO.StringIO()
image.save(output, format = "PNG")
email.attach(filename = "test.png", mimetype = "image/png", content = output.getvalue())
output.close()

email.send()

Optionally, you can add email.content_subtype = "html" to set the email to be a HTML email.

tumblr_lyegh0j09a1qdlh1io1_400

No more trying to force people into reading plain text emails!

Source

Python: Use dictionary as argument to create class or model instance

Sometimes you have to dynamically generate the initial arguments for an object but not sure which ones to pass.

Instead of initialising each arg as "None" and setting them all, just pass it a dict instead. That way, anything not specified is assumed to be default value.

data = {
'group': group,
'property': property,
'node': product.node.get(),
'rendered_data': None,
'data': None,
'sort_text': None,
'sort_number_asc': None,
'sort_number_desc': None,
}

# Do whatever logic you need here
if value:
data['rendered_data'] = value
data['data'] = int(value)

# The ** allows data dict to be used as keyword args.
property = Property(**data)
property.save()

Now you can instantiate LIKE A (Tommy Lee Jones) BOSS!

wtf-photos-videos-mr-super-jones

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