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 Customer(models.Model):
...
customer_types = (
(1, "Prospect"),
(2, "Lead"),
(3, "Customer"),
)
type = models.IntegerField(choices=customer_types, default=1)
...
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
@bencleary
bencleary / clean_tables.py
Last active February 22, 2019 14:10
A quick handy tool to truncate tables without using SQL but could be changed to execute SQL code, tracks progress and allows you to use an --app argument to delete data from all models given an app name. requires Django >= 1.11, tqdm
from django.core.management.base import BaseCommand
from django.apps import apps
from tqdm import tqdm
class Command(BaseCommand):
help = 'Drops table data from a specified app name'
def add_arguments(self, parser):
parser.add_argument('--app', type=str, help='The app name you wish to truncate the data from all models!')
myApp.component('users', {
bindings: {
users: '<'
},
template: '<ul class="list-group"><user ng-repeat="user in ctrl.users" user="user"></user></ul>',
controllerAs: 'ctrl',
controller: function(userService) {
if (userService.checkRole()) {
this.admin = true;
} else {
from django.db.models.signals import post_save
from django.dispatch import receiver
from .models import *
import pika
import json
@receiver(post_save, sender=Customer)
def send_welcome_email(sender, instance, created, *args, **kwargs):
if created:
from django.contrib import admin
from .models import *
class CustomerAdmin(admin.ModelAdmin):
list_display = ['first_name', 'last_name', 'email', 'welcome_email_sent']
list_display_links = ['first_name', 'last_name']
admin.site.register(Customer, CustomerAdmin)
def csv_file_from_list(self, headers: list, data: list, single_record_indicator=False):
"""
Creates and stores a CSV, rather than use a queryset it uses a list
:param headers
:param data
:param single_record_indicator
:return: list
"""
file_path = os.path.join(os.path.abspath(os.path.dirname("__file__")), "store", "csv", self.file_name)
with open(file_path, 'w') as file:
from django.views.generic.base import View
from django.db.models import QuerySet
from django.http import HttpResponse
from .generate import Generate
from io import BytesIO
from zipfile import ZipFile, ZIP_DEFLATED
from django.db.utils import DEFAULT_DB_ALIAS
from django.contrib.admin.utils import NestedObjects
from random import randint
import os
def generate_csv(self, request, columns: list=None):
"""
View to handle logic to determine if mixin properties are set and perform actions on said properties,
kept in seperate method to keep "method" methods cleaner
:param request:
:return:
"""
if self.custom_queryset:
if columns is None:
csv_props = Generate(request, cls=None).csv_file(headers=self.custom_headers, data=self.custom_queryset)
def csv_file_custom_columns(self, headers: list, data: QuerySet, columns: list=None):
"""
Creates and stores a CSV, with a custom selection of columns
:param headers
:param data
:param columns
:return: list
"""
file_path = os.path.join(os.path.abspath(os.path.dirname("__file__")), "store", "csv", self.file_name)
with open(file_path, 'w') as file: