Categories
Uncategorized

Misc Django I – forms

Custom form errors

If you want to validate something in the view, and return with a custom error message in the same form, you can use the “Form.add_error(fieldname, errorstring)” method. And then, of course, return to the previous template.

class MyView(View): 
    def get(self, request): 
        data = form.cleaned_data
        if len(res) > 0:
            form.add_error( 'login', "Diese Personalnummer existiert bereits!")
        return render(request, 'my_template.html', {'form': form})

Dynamic choice fields in forms

You want a form which fills its choice field from the database? And if the database changes, if you reload the page, the form should change as well? Of course! Django got you covered.

class UserForm(forms.Form): 
    def __init__(self, *args, **kwargs):
        super(UserForm, self).__init__(*args, **kwargs) 
        self.fields['site'] = forms.ModelChoiceField( label="Site", queryset=Site.objects.all().order_by('name'), ) 
        for field in ('department', 'office', 'phone'):
            self.fields.move_to_end(field)

    login = forms.CharField(label="Login")
    email = forms.EmailField(label="Email")
    site = None # this is set in __init__() :)
    department = forms.CharField(label="Department")
    office = forms.CharField(label="Office")
    phone = forms.CharField(label="Phone")

… now, why the “for field in (‘department’ …)” line you ask?

Simple. The fields dict is an OrderedDict. If you replace a field it is appended to the end again. So in the form the “Site” input box would be displayed last, although it makes more sense to display it where it is in the original definition.

Using “.move_to_end()” you can re-adjust this. If someone knows a better method … feel free to tell me.

(Sources: here)

Categories
Uncategorized

Firefox close tab buttons on mouse hover

I used to have Firefox’s TabMix Plus addon installed. And the feature I missed most – surprisingly – is to have the “close tab” buttons appear on a tab when you hover the mouse over it.

Googling a little bit told me how to bring it back:

.tabbrowser-tab:not([selected]):not([pinned]) .tab-close-button {
    visibility: hidden !important;
    margin-left: -16px !important;
}
.tabbrowser-tab:not([selected]):not([pinned]):hover .tab-close-button {
    visibility: visible !important; 
    margin-left: 0px    !important; 
    display: -moz-box   !important;
}
Categories
Puppet

Quick puppet debugging snipper for Atom

Not sure how I could have lived without this until now (had it before in Sublime, never bothered porting, stooooopid as I realize now 😉 ):

'.source.puppet':
  'Puppet: fail inline template':
    'prefix': 'fit'
    'body': """
      $fail_me = inline_template('<%= require "yaml"; YAML.dump(@$1) %>')
      fail("\\\\n\\\\nVariable \\\\$$1:\\\\n\${fail_me}\\\\n\\\\n")
    """
Categories
Infrastructure

Testing logstash configs with Docker

Now this is really not rocket science, but since I might do this more often, I don’t want to google every time.

Prepare your directories

./tmp                   # THIS IS YOUR WORKING DIRECTORY
  |- patterns/          # optional
  |   |- patternfile1   # optional
  |   |- patternfile2   # optional
  |- logs.log
  |- logstash.conf

Prepare your logstash config

# logstash.conf
input {
  file {
    path => '/stash/logs.log'
  }
}

filter {
  # whatever config you want to test
  grok {
    match        => [ "message", "%{WHATEVER}" ]
    patterns_dir => '/stash/patterns'              # optional :)
  }
}

output {
  stdout { codec => rubydebug }
}

Run logstash

docker run --rm -ti -v $(pwd):/stash logstash logstash -f /stash/logstash.conf

Done.

Done. 🙂