我目前有一个个人CRM应用程序,允许用户创建联系人,然后为这些联系人创建日志。代码如下所示。
views.py
class CreateContact(LoginRequiredMixin, CreateView):
model = Contact
template_name = 'network/contacts_list.html'
form_class = ContactForm
def get_success_url(self):
return reverse('home')
def form_valid(self, form):
form.instance.contact_owner = self.request.user
return super(CreateContact, self).form_valid(form)
def get_context_data(self, **kwargs):
context = super(CreateContact, self).get_context_data(**kwargs)
context['contacts'] = Contact.objects.filter(contact_owner=self.request.user)
return context
class CreateContactLog(LoginRequiredMixin, CreateView):
model = ContactLog
template_name = 'network/contact_detail.html'
fields = ('log_type','body',)
def get_success_url(self):
return reverse('contact-detail', kwargs={'id':self.kwargs['id']})
def form_valid(self, form):
current_contact = Contact.objects.get(contact_owner=self.request.user, contact_id=self.kwargs['id'])
form.instance.contact_owner = self.request.user
form.instance.contact_id = current_contact
return super(CreateContactLog, self).form_valid(form)
def get_context_data(self, **kwargs):
current_contact = Contact.objects.get(contact_owner=self.request.user, contact_id=self.kwargs['id'])
context = super(CreateContactLog, self).get_context_data(**kwargs)
context["contact_info"] = current_contact
context["first_name"] = current_contact.first_name
context["id"] = current_contact.contact_id
context['log_entries'] = ContactLog.objects.filter(contact_owner=self.request.user, contact_id=current_contact)
return context
一切正常。
现在,我想开始接受付款,我想将我的应用程序与Stripe集成在一起。最好的方法是使用第三方程序包dj-stripe。
我想有2个免费和付费的计划。我想允许免费计划用户最多创建10个联系人。阅读django-stripe的文档,我看到我可以使用基于类的视图,但是我不明白如何限制对象创建的数量。
这是我目前的想法:
也许创建两个类,其中一个将允许创建三个对象,而另一个类将允许无限制但需要付款。
也许我错过了documentation中的某个设置,它将对此有所帮助。
解决此问题的最佳方法是什么?
如果需要更多信息,请告诉我。
非常感谢。
最好,拉苏尔
我将创建一个util函数来检查:
检查1是对contact_owner
上反向关系的直接计数。
dj-stripe具有可在检查2中使用的Customer.get_or_create()
和Customer.get_or_create()
方法。
然后您可以在Customer.has_active_subscription()
中检查此实用程序的结果。如果结果为假,则可以Customer.has_active_subscription()
,然后调用form_valid
。
希望有所帮助,
Alex