Author Archives: rukaya

Trump’s withdrawal from the Paris Climate pact

Trump retweets support for his insanity

Fascinated to learn today that US leaders think American companies and American workers don’t live on the same planet as the rest of us – they can’t do if they are “hurt” by the prevention of its destruction, can they? My brother’s favourite alien conspiracy theories are sounding more plausible by the second!

Of course if you subscribe to the Global Warming is a Chinese Conspiracy conspiracy it all makes sense.

The concept of global warming was created by and for the Chinese in order to make US manufacturing non-competitive - Donald Trump, November 2012

Chosen selects in the django admin

A frustrating hour wasted and I don’t have enough rep to contribute to this stackoverflow answer, so I’m posting here. To add the jquery chosen widget to the django admin panel (django 1.10, chosen 1.6.2), so that multi select widgets show up properly, you need to do the following:

models.py – Get your manytomany set up

class Threat(models.Model):
    name = models.CharField(max_length=300, unique=True)

    def __str__(self):
        return self.name

    class Meta:
        ordering = ['name']


class Species(models.Model): 
    threats = models.ManyToManyField(Threat, blank=True)

admin.py – Register it in the admin site and add custom js and css

class SpeciesAdmin(admin.ModelAdmin):
    class Media:
        # Put your jquery in - automatically included by django but it appears below the chosen.jquery.min.js, adding it again just seems to shift it above
        js = ('//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js', 
              'chosen/chosen.jquery.min.js',
              'chosen_admin.js')
        css = {'all': ('chosen/chosen.min.css','chosen_admin.css')}

admin.site.register(models.Species, SpeciesAdmin)

chosen_admin.js – Easy, just instantiate chosen as you want

$(document).ready(function() {
    options = {
      search_contains: true, // plus whatever else you want 
    }
    $('#id_threats').chosen(options);
});

chosen_admin.css – This was the annoying bit to pinpoint and fix

.field-threats, .field-threats .related-widget-wrapper {
	overflow: visible !important;	
}
.chosen-container-multi .chosen-results {
	width: 100%;
}

Thanks to radtek and bradmontgomery.

Reader, I didn’t marry him.

I had a very weird moment recently which completely threw me for a loop. My extremely left-wing liberal family asked me why I didn’t want to get married, and just weren’t able to understand or accept that for me marriage seems pointless and ridiculous and that I feel like the term ‘wife’ has negative connotations. I did make it clear that I wasn’t imposing my views on them and I understood that for most people ‘husband’ and ‘wife’ were perfectly neutral terms. Continue reading

TDWG 2016

I had the privilege of being sent to the Biodiversity Information Standards (TDWG) group annual conference a few weeks ago, in December 2016, held in Costa Rica. The event was hosted by what must be the most friendly and kind group of people in the country (either that or all Costa Ricans are incredibly kind), at the Tecnologico de Costa Rica.

logoFinalReducido

Costa Rica was as amazing as when I last visited, although La Fortuna (nearby which the conference was held) was extremely rainy. I’m talking pretty much constant rain; the whole week we were there I think I saw the sun perhaps twice? The conference included a really cool field trip into the rainforest, where my colleague and I got absolutely soaked (no surprise there!) but we did see an awesome tarantula in its hole and a beautiful waterfall.

The conference itself was one of the best experiences I’ve had of conservation biology since I completed my CB course at the Fitzpatrick Institute. Everyone I met was intelligent, knowledgeable, turned on, engaged and curious about the field.  It felt like a very meaningful conference: biodiversity data might not be the sexiest subject ever (and “Biodiversity Information Standards” sounds downright boring), but it’s absolutely crucial for solving our modern environmental problems and that’s something which was at the forefront of everyone’s minds.  Continue reading

Adjust or change hidden value in form before submit with JQuery does not work

So here’s something rather odd, if you have a form:

<form id="process" action="/form-submit-url" method="post">
  <input id="myinput" name="mydata" type="hidden" />
  <button type="submit">Submit</button>
</form>

And you try and populate the hidden mydata input using jquery before the form submits:

 $(document).ready(function() {
   $('#process').submit(function(event) {
     $('#myinput').val('hello world')
   });
 });

It won’t work. You can’t do any of these sort of things either:

 $('input[name=mydata]').val('hello world');
 $('#myinput').attr('value', 'hello world');
 etc 

You have to do:

$('input[name=mydata]').val('hello world'); 

Quite strange. See http://stackoverflow.com/questions/2979772/set-value-of-hidden-field-in-a-form-using-jquerys-val-doesnt-work

The non-jquery method works fine!

document.getElementById("mydata").value = 'hello world';

Venetia, by Georgette Heyer

This is one of the most highly rated Heyer books on Goodreads, but I just don’t understand why.

The heroine is an ingenue with a little bit of spirit to her, and generally quite a nice and intelligent character. She is sexually molested (kissed and held down) by the hero on their first encounter, in something that feels very much like it could easily have turned into a rape scene. She’s outraged, but seems to go straight to being friends with the hero without thinking much about what he did. There’s quite a lot of moralising about how it’s in men’s natures to cheat and they can’t help themselves and boohoo poor men they are always so misused and noble, whereas the woman who cheats is such a pathetic slut.

Everything he had done since he had seen himself as a laughing-stock (and she neither knew nor cared to what depths he might have sunk) she perceived to be part of a pattern made inevitable by a wanton’s betrayal.

And all for a little, plump, black-eyed slut, older than himself, whose marriage-ring and noble degree hid the soul of a courtesan!

That passage is from the point of view of the heroine as she thinks about the hero. This is the second time I’ve tried to read Venetia, and the first time I couldn’t stand it and had to put it away because of that very passage as far as I remember. I suppose it must be an historically accurate representation of that society’s prevalent opinions, but I just don’t like it.

“Do you imagine he would be faithful to you?”
“I don’t know,” said Venetia. “I think he will always love me. You see, we are such dear friends.”

I feel like the book tries to be a bit more realistic and nuanced than your standard Heyer, with Venetia having to come to terms with the hero’s nature and having to accept it. I think it’s the fact that it takes itself a bit more seriously which really puts me off and makes it harder to dismiss the slut shaming and constant soothing of men’s egos which occurs throughout the book. It’s infuriating.

I like Georgette Heyer’s heroines because generally they are sparky, don’t take any bullshit (especially the Grand Sophy!), are strong-willed and generally fun to spend book-time with. Venetia wasn’t, and the situation in the book was horrible.

Validation examples for handsontable

In my current project, I have users who want to regularly copy and paste stuff from excel and save it into a database via a web app. At first I tried an elaborate system where they would download a spreadsheet template, paste their data into it, upload it and get back a new spreadsheet with validation errors. It started getting quite complicated, and I was very happy to find the handsontable library (an awesome lightweight spreadsheet library) to use instead. The problem is that there aren’t many examples on how to add validation on the web. So I hope this helps some other poor soul like me.

First thing is to stop trying to use handsontable’s methods via jquery. I spent ages trying to get it working and getting terribly confused because examples on the internet were all using jquery but the documentation on the site was all just pure js. Definitely easiest to go the pure js way, don’t forget you can include pure js code in your jquery $(document).ready(function(){}) – it will work just fine. JQuery is just javascript after all!

So, here is an example on a few different types of validation methods:

 HTML:
<div id="exampleGrid" class="dataTable"></div>
<button id="submitData" type="button" class="btn btn-primary">Upload</button>

JS: (remember to include jquery for your bits and bobs, as well as handsontable's js and css)
var myData = [
 ["cat", '10/11/2015', ''],
 ["invalidvalue", '10/11/2015', ''],
 ["dog", '10/11/2015', ''],
 ["", '10/11/2015', '']
];

// Empty validator
emptyValidator = function(value, callback) {
 if (!value || 0 === value.length) {
 console.log('false');
 callback(false);
 } else {
 console.log('true');
 callback(true);
 }
};

// Handsontable options
hotOptions = {
 colHeaders: ['autocomplete','date', 'string'],
 minSpareRows: 0, // This must be 0 or else it tries to add a blank row as soon as you remove it for validation
 rowHeaders: true,
 contextMenu: true,
 colWidths: [240, 100, 100],
 columns: [
 { // Note, whenever you put "type: something", handsontable does its own validation and if you add a "validator: yourfunction" it seems to go haywire.
 type: 'autocomplete',
 source: ['cat', 'dog'],
 strict: true
 },
 { // It is more obvious with the date that some kind of validation is going on and it will get confused with your own custom validator. I don't know how you would add custom date validation, I haven't had to do that yet thank goodness
 type: 'date',
 dateFormat: 'DD/MM/YYYY',
 correctFormat: true,
 defaultDate: '01/01/2000'
 },
 { // Here is the empty column with your validator forcing text in there
 validator: emptyValidator
 }
 ],
 data: myData,
};

// Instantiate
var ht = new Handsontable(document.getElementById('exampleGrid'), hotOptions);

// Trigger more validation with the click
$('#submitData').click(function() {
 // Get the data in the cells
 var myTableData = ht.getData();

 // If the last row is empty, remove it before validation
 if(myTableData.length > 1 && ht.isEmptyRow(myTableData.length - 1)) {
 // Remove the last row if it's empty
 ht.alter('remove_row', parseInt(myTableData.length - 1), keepEmptyRows = false);
 }

 // Validate the cells and submit the form via ajax or whatever
 ht.validateCells(function(result, obj) {
 if(result == true) {
 console.log('submitted');
 }
 else {
 console.log('bad form data')
 }
 });
});

And here is a link to a jsFiddle: https://jsfiddle.net/sysb6wvd/2/

Django-allauth and modelforms

It took me a ridiculously long and frustrating time to work out how to do this, so I’m documenting what I’ve done in the hope that it helps someone else. If you want to extend the Django user model (to create a user profile, as described in the docs https://docs.djangoproject.com/en/1.9/topics/auth/customizing/#extending-the-existing-user-model) to create a user profile, and to still use Django-allauth you might as well give up on the documentation: it’s not going to help you. But what you need to do is fairly simple:

models.py

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    phone = models.CharField(max_length=100)

forms.py

class SignupForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = ('first_name', 'last_name', 'phone', 'type')

    # A custom method required to work with django-allauth, see https://stackoverflow.com/questions/12303478/how-to-customize-user-profile-when-using-django-allauth
    def signup(self, request, user):
        # Save your user
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.save()

        # Save your profile
        profile = Profile()
        profile.user = user
        profile.phone = self.cleaned_data['phone']
        profile.type = self.cleaned_data['type']
        profile.save()

settings.py

# Required by django-allauth to extend the sign up form to include profile data
ACCOUNT_SIGNUP_FORM_CLASS = 'core.forms.SignupForm'

I’m not sure why I thought that django-allauth would take care of saving user profiles when it very explicitly says it doesn’t do anything apart from authentication. I suppose we should count ourselves lucky that it’s easy to extend the form to include additional profile fields.

Django multi select multiple choice model field

To me, it doesn’t really make sense to store static data in a database – especially if there are only 5 or 6 errr datum you are wanting to store. Django allows you to use a Charfield with choices, i.e.

WIND = 'W'
SOLAR = 'S'
ENERGY_TYPE_CHOICES = (
    (WIND, 'Wind turbine'),
    (SOLAR, 'Solar panels')
)
energy_type = models.CharField(max_length=1, choices=ENERGY_TYPE_CHOICES, default=WIND)

But unfortunately you can’t select multiple choices, so in this case it wouldn’t be possible to associate both wind & solar with one of my models. It is suggested that developers store this info in a separate table and associate the choices with models using a manytomanyfield. Perhaps this makes sense if you want to associate the field options with multiple models, but it really seems overkill if you just want to have multiple choices for a single model.

Strangely there don’t seem to be many other people who have felt this to be too much (I mean, going by google anyway). As I have frequently found with Django, a few people have discussed it on stackoverflow and there are a million ways of doing the same thing with no clear indiciation as to which is most widely used and most sensible. Well, there are 3 different modules/alternatives:

  • Multiselectfield: https://github.com/goinnn/django-multiselectfield
  • Multiple_select_field: https://gist.github.com/kottenator/9a50e4207cff15c03f8e
  • Select_multiple_field: https://github.com/kelvinwong-ca/django-select-multiple-field

This is ridiculous. Django’s “do-it-yourself” attitude is refreshing after coming from PHP and Drupal, but why isn’t something this ubiquitous integrated into Django itself?  This is something many projects are going to want. Or do I have it wrong and is it better to have a separate db table for like 2 or 3 fields?