Skip to content

Instantly share code, notes, and snippets.

@nandak522
Created December 23, 2009 02:42
Show Gist options
  • Save nandak522/262276 to your computer and use it in GitHub Desktop.
Save nandak522/262276 to your computer and use it in GitHub Desktop.
from django.db import models
from utils.models import BaseModel
class ActiveQuestionManager(models.Manager):
def get_query_set(self):
return super(ActiveQuestionManager, self).get_query_set().filter(is_active=True)
class PublicQuestionManager(models.Manager):
def get_query_set(self):
return super(PublicQuestionManager, self).get_query_set().filter(visible=True)
class QuestionManager(models.Manager):
def create_question(self, title, explanation, code, visible=True, is_active=True):
from django.template.defaultfilters import slugify
question = Question(title=title, slug=slugify(title), explanation=explanation, code=code, visible=visible, is_active=is_active)
question.save()
return question
def update_question(self, question, **kwargs):
for key, value in kwargs.items():
setattr(question, key, value)
question.save()
return Question.objects.get(id=question.id)
class Question(BaseModel):
title = models.CharField(max_length=100)
slug = models.SlugField(max_length=100)
explanation = models.TextField(max_length=200)
code = models.TextField(max_length=1500)
visible = models.BooleanField(default=True)
is_active = models.BooleanField(default=True)
publicquestions = PublicQuestionManager()
activequestions = ActiveQuestionManager()
objects = QuestionManager()
class Meta:
ordering = ['-created_on']
def __unicode__(self):
return self.title
from django.db import models
class BaseModel(models.Model):
id = models.AutoField(primary_key=True)
created_on = models.DateTimeField(auto_now_add=True)
modified_on = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment