Automation is supposed to let you slack more.

Friday, October 13, 2017

Verifying OpenSSL certificates

Verifying that a certificate is issued by a CA

How to use OpenSSL on the command line to verify that a certificate was issued by a specific CA, given that CA's certificate:
$ openssl verify -verbose -CAfile cacert.pem  server.crt
server.crt: OK

Verify that a private key matches a certificate

$ openssl x509 -noout -modulus -in server.crt | openssl md5
$ openssl rsa -noout -modulus -in server.key | openssl md5
The resultant hashes should match.

Thursday, September 28, 2017

Chaining class decorators

The last (i.e. outer) decorator's behavior gets executed first.
def require_authentication_permission(permission):
    def decorator(cls):
        cls.dispatch = method_decorator(
            permission_required(
                permission, raise_exception=True)
            )(cls.dispatch)
        cls2 = class_login_required(cls)
        return cls2

    return decorator
In this case, the execution order is:
  1. class_login_required
  2. permission_required

Monday, September 25, 2017

Logging in Django

To log to console in Django. Source: Logging | djangoproject.com

settings.py 

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),
        },
    },
}

File that uses logging

import logging
logger = logging.getLogger('django')

Write to log

logger.info('Hello')

Tuesday, September 19, 2017

Wednesday, September 13, 2017

Tuesday, September 12, 2017

Friday, September 08, 2017

Django Forms

Some common uses of Django Forms.

f = Form(request.POST)
Instantiating a form with data a dictionary, e.g. from POST, creates a form populated with the field-name & field-value pairs in the dictionary.

f.errors
Returns dictionary of errors with keys being field names.

f['field_name'].errors
Returns list of errors for the specified field.

bound_form = self.form_class(request.POST)
return render(request, self.template_name, {'form': bound_form})
In a class-based view, renders a form with data bound from POST. bound_form.is_bound returns True.

model_instance = get_object_or_404(
            self.form_class.Meta.model, slug__iexact=slug)

bound_form = self.form_class(request.POST, instance=model_instance)
As with previous line, renders a form with data bound from POST but also associated with a model instance so that it can be modified with a subsequent:
model_instance = bound_form.save()

form_class(initial={'title': 'Something'})
Creates a form with a field filled in already. However, the form is not bound.

Popular Posts

Recent Posts

Unordered List

Text Widget

Pages

Powered by Blogger.
Scroll To Top