Skip to content

Instantly share code, notes, and snippets.

View bencleary's full-sized avatar
💭
I may be slow to respond.

Ben Cleary bencleary

💭
I may be slow to respond.
View GitHub Profile
class MyCustomUser(models.Model):
# user fields
def save(self, *args, **kwargs):
super(MyCustomUser, self).save(*args, **kwargs)
profile = MyUserProfile(user=self)
profile.save()
from django.dispatch import receiver
from django.db.models.signals import post_save
from .models import *
@receiver(post_save, sender=MyCustomUser)
def build_profile_on_user_creation(sender, instance, created, **kwargs):
if created:
profile = MyProfile(user=instance)
profile.save()
@bencleary
bencleary / email_as_username_admin.py
Created December 5, 2017 11:07
An example of building a user model which uses email as username rather than username
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserCreationForm
from .models import *
class UserAdmin(UserAdmin):
add_form = BlogUserCreationForm
from django.contrib import admin
from .models import *
class AuctionAdmin(admin.ModelAdmin):
list_display = ['title', 'current_bid_display', 'bid_count', 'expiry_date', 'active']
def current_bid_display(self, obj):
return "£{0}".format(obj.current_bid)
from django.core.management.base import BaseCommand, CommandError
from crunchy_waffles_app.models import *
from django.utils import timezone
class Command(BaseCommand):
help = 'Closes or opens auctions based their individual DateTime values'
def add_arguments(self, parser):
parser.add_argument('--id')
class EmailAdmin(admin.ModelAdmin):
list_display = ['auction', 'email_to', 'created_at']
list_filter = (
('auction',)
)
def email_to(self, obj):
return obj.owner.email
from crunchy_waffles_app.models import EmailQueue
class BiddingActions():
auction = None
def __init__(self, instance):
self.auction = instance
def update_auction(self):
from django.core.management.base import BaseCommand, CommandError
from crunchy_waffles_app.models import *
class Command(BaseCommand):
help = 'Runs through email queue and sends emails'
def add_arguments(self, parser):
parser.add_argument('--id')
from django.contrib import admin
from .models import *
class ManagementAdmin(admin.ModelAdmin):
list_display = ['task', 'count', 'started', 'finished', 'state']
admin.site.register(ManagementLog, ManagementAdmin)
from crunchy_waffles_ops.models import *
from django.utils import timezone
class Management:
log = None
def __init__(self, title):
self.log = ManagementLog(task=title, count=0, started=timezone.now(), state=1)