Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Harlin Seritt hseritt

🏠
Working from home
View GitHub Profile
View Dynamic multidimensional dictionary keys based on script arguments
#!/usr/bin/env python
import os
import sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import myproject.settings as settings # noqa E402
info = settings.__dict__
@hseritt
hseritt / example.py
Created Jun 1, 2019
If I run example.py, I get "8" ... if run example.rb I get error: Traceback (most recent call last): 1: from ./example1.rb:12:in `<main>' ./example1.rb:3:in `times2': wrong number of arguments (given 0, expected 1) (ArgumentError)
View example.py
#!/usr/bin/env python
def times2(num):
return num * 2
def add(num1, num2, fn=None):
sum = num1 + num2
return fn(sum)
@hseritt
hseritt / log_demo.py
Last active Oct 4, 2017
A logging demo that shows most of all you'll ever need to know concerning Python and logging.
View log_demo.py
#!/usr/bin/env python
"""
A logging demo that shows most of all you'll ever need to know
concerning Python and logging.
"""
import logging
import sys
logger = logging.getLogger(__name__)
@hseritt
hseritt / urls.py
Created Feb 25, 2017
Django urls - examples of how to write them (permissions, class views, etc.)
View urls.py
"""
URL patterns for common views
"""
from django.conf.urls import url
from django.contrib.auth.decorators import permission_required
from django.contrib.auth.views import login, logout
from django.views.generic.edit import DeleteView
from apps.business.models import SalesPhase, SeverityLevel
from apps.cases.models import CaseCategory, CaseStatus
@hseritt
hseritt / forms.py
Created Feb 25, 2017
Django forms - adding request object into form for evaluation
View forms.py
class ProjectUpdateForm(ModelForm):
"""
Form for updating projects.
"""
members = forms.ModelMultipleChoiceField(
queryset=User.objects.all().order_by('username'), required=False)
lead_user = forms.ModelChoiceField(
queryset=User.objects.all().order_by('username'), required=False)
@hseritt
hseritt / forms.py
Created Feb 25, 2017
Django ModelMultipleChoiceField and ModelChoiceField and not required
View forms.py
members = forms.ModelMultipleChoiceField(
queryset=User.objects.all().order_by('username'), required=False)
lead_user = forms.ModelChoiceField(
queryset=User.objects.all().order_by('username'), required=False)
@hseritt
hseritt / forms.py
Last active Feb 25, 2017
Django forms - setting help text for fields
View forms.py
# In the __init__ method:
self.fields['description'].help_text = mark_safe(FIELD_REQUIRED_MSG)
self.fields['issue_type'].help_text = mark_safe(FIELD_REQUIRED_MSG)
self.fields['priority'].help_text = mark_safe(FIELD_REQUIRED_MSG)
self.fields['project'].help_text = mark_safe(FIELD_REQUIRED_MSG)
self.fields['title'].help_text = mark_safe(FIELD_REQUIRED_MSG)
# and in class Meta:
@hseritt
hseritt / forms.py
Created Feb 25, 2017
Django form field: somewhat complex queryset filter
View forms.py
self.fields['watchers'] = forms.ModelMultipleChoiceField(
queryset=User.objects.filter(
Q(contact__organization=self.request.user.contact.organization)|
Q(contact__organization__org_type__in=['Owner',])|
Q(groups__name__in=['issues_admin', 'cases_admin'])
).distinct().order_by('username')
)
@hseritt
hseritt / forms.py
Created Feb 25, 2017
Django form using super() and queryset
View forms.py
class ArticleUpdateForm(ModelForm):
"""
Form for updating an article.
"""
def __init__(self, *args, **kwargs):
super(ArticleUpdateForm, self).__init__(*args, **kwargs)
self.fields['attachments'] = forms.ModelMultipleChoiceField(
queryset=self.instance.attachments.all(), required=False
)
@hseritt
hseritt / forms.py
Created Feb 25, 2017
Form widget customizations
View forms.py
...
class ArticleAddForm(ModelForm):
"""
Form for creating a new article.
"""
content = forms.CharField(widget=forms.Textarea(attrs={'class': 'content_textarea'}))
project = forms.ModelChoiceField(queryset=Project.objects.all(), required=False)
class Meta: