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 / settings.py
Created February 25, 2017 12:43
Handling static files and dirs
...
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_URL = '/static/'
@hseritt
hseritt / settings.py
Created February 25, 2017 12:44
Logging setup for a Django project
...
# Logging settings
LOGGING = {
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format' : "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
'datefmt' : "%d-%m-%Y %H:%M:%S"
},
@hseritt
hseritt / models.py
Created February 25, 2017 12:46
Django model class using upload directories with a date/time structure hierarchy
...
class ArticleAttachment(models.Model):
"""
Attached files for articles
"""
att_file = models.FileField(
upload_to='content/uploads/articles/%Y/%m/%d/%H/%M/%S')
class Meta:
@hseritt
hseritt / models.py
Created February 25, 2017 12:48
Model class fields for handling foreign key deletions
...
severity_level = models.ForeignKey(
'business.SeverityLevel', null=True, blank=True,
on_delete=models.SET(
None
)
)
status = models.ForeignKey(
'CaseStatus', null=True, blank=True,
@hseritt
hseritt / filename.py
Created February 25, 2017 12:49
Django send mail function
from django.core.mail import send_mail
...
send_mail(
NEW_CASE_EMAIL_SUBJECT.format(case.title),
NEW_CASE_EMAIL_MESSAGE.format(
case.title,
case.description,
case.id
@hseritt
hseritt / group_membership.py
Created February 25, 2017 12:51
Custom tag library to test group membership.
"""
Custom tag library to test group membership.
"""
from django import template
from django.contrib.auth.models import Group
from apps.contacts.models import Contact
register = template.Library()
@hseritt
hseritt / forms.py
Created February 25, 2017 12:52
Form widget customizations
...
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:
@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
)
@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
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: