Site icon JnPnote

Following Django Tutorial (CRM)-Part12

This post is for myself to remember how to build Django project.
I did follow the tutorial from the JustDjango Learn website free tutorial.
And this is the fourth tutorial named Getting Started With Django.
I am not going over all the details and descriptions for each part. There are good explanations on video of the JustDjango Learn. So, visit their site and try their tutorials if you need more details. Also, the orders of this post and their video might be different because I put things first what I think should come first.

I am doing this on Windows 10 with just Windows PowerShell. Not using virtual machines at all.

1. Filter Agents

organisation = self.request.user.userprofile
return Agent.objects.filter(organisation=organisation)

2. User Type

is_organisor = models.BooleanField(default=True)
is_agent = models.BooleanField(default=False)

3. Agent’s UI

from django.contrib.auth.mixins import AccessMixin
from django.shortcuts import redirect

class OrganisorAndLoginRequiredMixin(AccessMixin):
	"""Verify that the current user is authenticated and is an organisor."""
	def dispatch(self, request, *args, **kwargs):
		if not request.user.is_authenticated or not request.user.is_organisor:
			return redirect("leads:lead-list")
		return super().dispatch(request, *args, **kwargs)

4. Make agent null as default

5. Leads queryset

class LeadListView(LoginRequiredMixin, generic.ListView):
	template_name = "lead_list.html"
	context_object_name = "leads"

	def get_queryset(self):
		user = self.request.user
		# initial queryset of leads for the entire organisation
		if user.is_organisor:
			queryset = Lead.objects.filter(organisation=user.userprofile)
		else:
			queryset = Lead.objects.filter(organisation=user.agent.organisation)
			# filter for the agent that is logged in
			queryset = queryset.filter(agent__user=user)
		return queryset
def get_queryset(self):
		user = self.request.user
		# initial queryset of leads for the entire organisation
		return Lead.objects.filter(organisation=user.userprofile)

6. invite agent

class AgentCreateView(OrganisorAndLoginRequiredMixin, generic.CreateView):
	template_name = "agent_create.html"
	form_class = AgentModelForm

	def get_success_url(self):
		return reverse("agents:agent-list")

	def form_valid(self, form):
		user = form.save(commit=False)
		user.is_agent = True
		user.is_organisor = False
		user.set_password(f"{random.randint(0,100000)}")
		user.save()
		Agent.objects.create(
			user=user,
			organisation=self.request.user.userprofile
		)
		send_mail(
			subject="You are invite to be an agent",
			message="You were added as an agent on DJANGOCRM. Please come login to start working.",
			from_email="admin@test.com",
			recipient_list=[user.email]
		)
		return super(AgentCreateView, self).form_valid(form)

done for this post

Exit mobile version