Skip to content

Instantly share code, notes, and snippets.

View hseritt's full-sized avatar
🏠
Working from home

Harlin Seritt hseritt

🏠
Working from home
View GitHub Profile
@hseritt
hseritt / example.py
Created April 15, 2023 10:59
Using namedtuple for constants in python
from collections import namedtuple
Socket = namedtuple("Socket", ["DEFAULT_TIMEOUT"])
# Set a default timeout for sockets
SOCKET = Socket(DEFAULT_TIMEOUT=10)
print(SOCKET.DEFAULT_TIMEOUT)
try:
#!/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 June 1, 2019 03:09
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)
#!/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 October 4, 2017 01:10
A logging demo that shows most of all you'll ever need to know concerning Python and logging.
#!/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 February 25, 2017 13:05
Django urls - examples of how to write them (permissions, class views, etc.)
"""
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 February 25, 2017 13:01
Django forms - adding request object into form for evaluation
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 February 25, 2017 13:00
Django ModelMultipleChoiceField and ModelChoiceField and not required
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 February 25, 2017 12:58
Django forms - setting help text for fields
# 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 February 25, 2017 12:55
Django form field: somewhat complex queryset filter
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 February 25, 2017 12:53
Django form using super() and queryset
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
)