Last active
August 29, 2015 14:25
-
-
Save leonardoo/75a14e5c4b8da9da4d11 to your computer and use it in GitHub Desktop.
instance
This file contains hidden or 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 InstanceMixin(object): | |
def add_arguments(self): | |
return {'empresa':self.request.empresa} | |
def get_form_kwargs(self): | |
""" | |
Returns the keyword arguments for instanciating the form. | |
""" | |
kwargs = super(InstanceMixin, self).get_form_kwargs() | |
kwargs.update(self.add_arguments()) | |
return kwargs | |
class CashCreateView(LoginMixin, InstanceMixin, CreateView): | |
model = models.CajaGeneral | |
template_name = "cash/cash/create.html" | |
success_url = reverse_lazy('caja_general_list') | |
def add_arguments(self): | |
if hasattr(self.request, 'store'): | |
return {"store": self.request.store} | |
else: | |
return {'empresa': self.request.empresa} | |
def get_form_class(self): | |
if hasattr(self.request, 'store'): | |
return forms.CajaGeneralStoreForm | |
else: | |
return forms.CajaGeneralForm | |
class CajaGeneralForm(forms.ModelForm): | |
# TODO: Define form fields here | |
class Meta: | |
model = models.CajaGeneral | |
fields = ("store", "fecha") | |
def __init__(self, *args, **kwargs): | |
empresa = kwargs.pop('empresa', None) | |
super(CajaGeneralForm, self).__init__(*args, **kwargs) | |
if empresa: | |
queryset = self.fields['store'].queryset.filter(empresa=empresa) | |
self.fields['store'].queryset = queryset | |
class CajaGeneralStoreForm(forms.ModelForm): | |
# TODO: Define form fields here | |
def __init__(self, *args, **kwargs): | |
store = kwargs.pop('store', None) | |
super(CajaGeneralStoreForm, self).__init__(*args, **kwargs) | |
if store: | |
self.instance.store = store | |
class Meta: | |
model = models.CajaGeneral | |
fields = ("fecha", ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment