Skip to content

Instantly share code, notes, and snippets.

@db0company
Last active May 27, 2017 09:23
Show Gist options
  • Save db0company/819ec1900fb207f865be69b92ce62c8e to your computer and use it in GitHub Desktop.
Save db0company/819ec1900fb207f865be69b92ce62c8e to your computer and use it in GitHub Desktop.
Examples to illustrate the MagiCircles Documentation
def filterCards(queryset, parameters, request):
if 'promo_only' in parameters:
queryset = queryset.filter(is_promo=True) if 'promo_only'
return queryset
############################################################
def FilterCardsForm(MagiFilter):
class Meta:
model = models.Card
fields = ('rarity', 'attribute', 'level')
############################################################
def cardsExtraContext(context):
context['color'] = 'blue'
############################################################
def foreachCards(index, item, context):
item.enabled = item.owner == context['request'].user
############################################################
class FormSaveOwner(FormWithRequest):
def save(self, commit=True):
instance = super(FormSaveOwner, self).save(commit=False)
instance.owner = self.request.user if self.request.user.is_authenticated() else None
if commit:
instance.save()
return instance
class CardForm(FormSaveOwner):
class Meta:
model = models.Card
fields = ('rarity', 'attribute')
class CardTypeForm(FormSaveOwner):
def __init__(self, *args, **kwargs):
self.type = kwargs.pop('type', None)
super(FilterCardsForm, self).__init__(*args, **kwargs)
def save(self, commit=True):
instance = super(CardTypeForm, self).save(commit=False)
instance.rarity = self.type
if commit:
instance.save()
return instance
class Meta:
model = models.Card
fields = ('name', 'attribute')
############################################################
class Idol(models.Model):
"""
This model has an actual owner field
"""
owner = models.ForeignKey(User, related_name='idols')
class Score(models.Model):
"""
This model doesn't have an actual owner field but uses @property to return the owner info.
Note: In that case it's recommended to cache the account in the model.
"""
account = models.ForeignKey(models.Account, related_name='scores')
@property
def owner(self):
return self.account.owner
@property
def owner_id(self):
return self.account.owner_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment