This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SoftDeleteQuerySet(models.QuerySet): | |
@transaction.atomic | |
def delete(self, snapshot_id=None): | |
[x.delete(snapshot_id=snapshot_id) for x in self] | |
class SoftDeleteQueryManager(models.Manager): | |
def __init__(self, *args, **kwargs): | |
self.with_deleted = kwargs.pop('with_deleted', False) | |
super(SoftDeleteManager, self).__init__(*args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User(SoftDeleteModel): | |
name = models.CharField(null=False, blank=False) | |
class Cat(SoftDeleteModel): | |
owner = models.ForeignKey( | |
User, | |
null=True, | |
blank=True, | |
on_delete=models.SET_NULL) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Snapshot(models.Model): | |
# unique identifier for snapshots | |
id = models.Charfield(max_length=32) | |
# the record which initiated the snapshot | |
root_id = models.CharField(max_length=32) | |
@transaction.atomic | |
def restore(self): | |
[x.restore() for x in self.snapshot_records.all()] | |
self.delete() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Car(models.Model): | |
# CASCADE is the default behavior. When this car's manufacturer is deleted, | |
# this car will also be deleted | |
manufacturer = models.ForeignKey(Manufacturer, on_delete=models.CASCADE) | |
# SET_NULL makes it so when this car's owner gets deleted, it will continue | |
# to exist without an owner. | |
owner = models.ForeignKey(User, on_delete=models.SET_NULL) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SoftDeleteManager(models.Manager): | |
def __init__(self, *args, **kwargs): | |
self.with_deleted = kwargs.pop('deleted', False) | |
super(SoftDeleteManager, self).__init__(*args, **kwargs) | |
def _base_queryset(self): | |
return super().get_queryset().filter(deleted_at=None) | |
def get_queryset(self): | |
qs = self._base_queryset() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# horrible! | |
User.objects.filter(first_name='Ian', is_deleted=False).first() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SoftDeleteModel(BaseModel): | |
class meta: | |
abstract = True | |
is_deleted = models.DateTimeField(null=False, default=False) | |
def delete(self): | |
self.is_deleted = True | |
self.save() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BaseModel(models.Model): | |
created_at = models.DateTimeField(auto_now_add=True) | |
modified_date = models.DateTimeField(auto_now=True) | |
class Meta: | |
abstract = True | |
# other fields / methods... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"fmt" | |
"reflect" | |
) | |
func pipe(fns []interface{}, args ...interface{}) (interface{}, error) { | |
for i := range fns { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <pthread.h> | |
#define THREAD_COUNT 30 | |
// Each thread will run an | |
// instance of this function | |
void *worker(void *arg) { | |
printf("I'm worker #%d\n", (int)arg); | |
return NULL; |
NewerOlder