This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class DjangoModel(models.Model): | |
| @classmethod | |
| def from_db(cls, db, field_names, values): | |
| instance = super().from_db(db, field_names, values) | |
| instance._state.adding = False | |
| instance._state.db = db | |
| instance._old_values = dict(zip(field_names, values)) | |
| return instance | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # the mixin | |
| class JSONFieldFormMixin(object): | |
| """ | |
| Given that a model has some kind of TextField ``json_storage_field`` with | |
| a string of JSON formatted data inside, this mixin adds handling of this | |
| field to a ModelForm. | |
| It will read the text field, convert to Python dict, fill the form fields | |
| that are given via ``json_fields`` and on saving will write this back to |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django.db import transaction | |
| from django.apps import apps | |
| from django.contrib.contenttypes.fields import GenericForeignKey | |
| from django.db.models.fields.related import ManyToManyField | |
| @transaction.atomic() | |
| def merge(primary_object, *alias_objects): | |
| """Merge several model instances into one, the `primary_object`. | |
| Use this function to merge model objects and migrate all of the related |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Read more at http://www.pindi.us/blog/migrating-cross-domain-cookies-django | |
| from django.conf import settings | |
| from importlib import import_module | |
| class UpdateSessionCookieMiddleware(object): | |
| """ | |
| Migrates session data from an old (hardcoded) session cookie name and domain to the name and | |
| domain currently defined in the Django settings. | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| sudo wget http://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo -O /etc/yum.repos.d/epel-apache-maven.repo | |
| sudo sed -i s/\$releasever/6/g /etc/yum.repos.d/epel-apache-maven.repo | |
| sudo yum install -y apache-maven | |
| mvn --version |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # (optional) You might need to set your PATH variable at the top here | |
| # depending on how you run this script | |
| #PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin | |
| # Hosted Zone ID e.g. BJBK35SKMM9OE | |
| ZONEID="enter zone id here" | |
| # The CNAME you want to update e.g. hello.example.com |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def not_in_student_group(user): | |
| """Use with a ``user_passes_test`` decorator to restrict access to | |
| authenticated users who are not in the "Student" group.""" | |
| return user.is_authenticated() and not user.groups.filter(name='Student').exists() | |
| # Use the above with: | |
| @user_passes_test(not_in_student_group, login_url='/elsewhere/') | |
| def some_view(request): | |
| # ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| apt-get install python-pygraphviz | |
| pip install django-extensions | |
| # add 'django_extensions' to INSTALLED_APPS in settings.py | |
| python manage.py graph_models trees -o test.png |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [...] | |
| # Fake DNS entries for testing the subdomain middleware | |
| 127.0.0.1 test1.django.local | |
| 127.0.0.1 test2.django.local | |
| 127.0.0.1 djangotest.christopher-williams.net | |
| 127.0.0.1 django.local |