No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

views.py 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. # This file is part of dnmgmt, a number resource management system
  2. # Licensed under GNU General Public License v3 or later
  3. # Written by Sebastian Lohff (seba@someserver.de)
  4. from django.shortcuts import render, get_object_or_404
  5. from django.db.models import Q
  6. from django.contrib.auth.decorators import login_required
  7. from django.http import HttpResponseRedirect, Http404
  8. from django.urls import reverse, reverse_lazy
  9. from django.views.generic import DetailView, CreateView, UpdateView
  10. from django.contrib.auth.mixins import LoginRequiredMixin
  11. from django.contrib import messages
  12. from formtools.wizard.views import SessionWizardView
  13. from .models import Maintainer, Contact, InetNum, ASBlock, ASNumber
  14. from .forms import MntForm, MntInitialForm, ContactForm, ContactInitialForm, InetNumForm, ASBlockForm, ASNumberForm
  15. from .generic import DeleteCheckView, MntGenericMixin
  16. from .helpers import findInDatabase
  17. @login_required
  18. def createObjectOverview(request):
  19. mnts = request.user.maintainer_set.filter().all()
  20. netblocks = InetNum.objects.filter(Q(mnt_by__in=mnts) | Q(mnt_lower__in=mnts)).distinct()
  21. asblocks = ASBlock.objects.filter(Q(mnt_by__in=mnts) | Q(mnt_lower__in=mnts)).distinct()
  22. return render(request, "whoisdb/create_overview.html", {"netblocks": netblocks, "asblocks": asblocks})
  23. @login_required
  24. def dbDashboard(request):
  25. mnts = request.user.maintainer_set.filter(rir=False, lir=False).all()
  26. if request.GET.get("delegated", None) or mnts.count() == 0:
  27. # if user wants to see rir/lir objects or only has rir/lir mnts, use all available mnts
  28. mnts = request.user.maintainer_set.all()
  29. showDelegations = True
  30. else:
  31. showDelegations = False
  32. hasDelegations = request.user.maintainer_set.filter(Q(rir=True) | Q(lir=True)).count() > 0
  33. contacts = Contact.objects.filter(mnt_by__in=mnts)
  34. netblocks = InetNum.objects.filter(Q(mnt_by__in=mnts) | Q(mnt_lower__in=mnts)).distinct()
  35. asblocks = ASBlock.objects.filter(Q(mnt_by__in=mnts) | Q(mnt_lower__in=mnts)).distinct()
  36. asnumbers = ASNumber.objects.filter(Q(mnt_by__in=mnts) | Q(mnt_lower__in=mnts)).distinct()
  37. mntForm = contactForm = None
  38. if mnts.count() == 0:
  39. mntForm = contactForm = None
  40. if request.method == "POST":
  41. mntForm = MntInitialForm(user=request.user, data=request.POST, prefix="mnt")
  42. contactForm = ContactInitialForm(user=request.user, data=request.POST, prefix="contact")
  43. if mntForm.is_valid() and contactForm.is_valid():
  44. mnt = mntForm.save(commit=False)
  45. mnt.handleAuto(request.user.username)
  46. mnt.save()
  47. contact = contactForm.save(commit=False)
  48. contact.handleAuto()
  49. contact.type = Contact.TYPE_PERSON
  50. contact.save()
  51. contact.mnt_by.add(mnt.id)
  52. contact.save()
  53. mnt.auth.add(request.user.id)
  54. mnt.admin_c.add(contact.id)
  55. mnt.save()
  56. return HttpResponseRedirect(reverse("whoisdb:dashboard"))
  57. else:
  58. mntForm = MntInitialForm(user=request.user, prefix="mnt", initial={'handle': 'AUTO', 'description': 'Primary maintainer of %s' % request.user.username})
  59. contactForm = ContactInitialForm(user=request.user, initial={'handle': 'AUTO', 'name': request.user.username.capitalize()}, prefix='contact')
  60. return render(request, "whoisdb/overview.html", {"mnts": mnts, "contacts": contacts, "mntForm": mntForm, "contactForm": contactForm, "netblocks": netblocks, "asblocks": asblocks, "asnumbers": asnumbers, 'hasDelegations': hasDelegations, "showDelegations": showDelegations})
  61. def showHandle(request, handle):
  62. # a) find handle
  63. models = [Contact, Maintainer, ASBlock, ASNumber, InetNum]
  64. obj = None
  65. for model in models:
  66. if handle.endswith(model.handleSuffix):
  67. obj = get_object_or_404(model, handle=handle)
  68. break
  69. if not obj:
  70. raise Http404("Handle object not found")
  71. return render(request, "whoisdb/handle_show.html", {"object": obj})
  72. def searchObject(request):
  73. results = None
  74. term = request.GET.get("q", None)
  75. if term:
  76. results = findInDatabase(term)
  77. return render(request, "whoisdb/search.html", {"results": results, "term": term})
  78. class MaintainerCreate(LoginRequiredMixin, CreateView):
  79. template_name = "whoisdb/obj_create.html"
  80. form_class = MntForm
  81. def get_form_kwargs(self, *args, **kwargs):
  82. kwargs = super(MaintainerCreate, self).get_form_kwargs(*args, **kwargs)
  83. kwargs["user"] = self.request.user
  84. kwargs["initial"] = {
  85. "handle": "AUTO",
  86. #"auth": self.request.user.username,
  87. }
  88. return kwargs
  89. def form_valid(self, form):
  90. self.object = form.save(commit=False)
  91. self.object.handleAuto(self.request.user.username)
  92. self.object.save()
  93. self.object.auth.add(self.request.user)
  94. self.object.save()
  95. return super(MaintainerCreate, self).form_valid(form)
  96. class MaintainerEdit(LoginRequiredMixin, UpdateView):
  97. template_name = "whoisdb/obj_edit.html"
  98. model = Maintainer
  99. form_class = MntForm
  100. slug_field = "handle"
  101. slug_url_kwarg = "handle"
  102. def get_form_kwargs(self, *args, **kwargs):
  103. kwargs = super(MaintainerEdit, self).get_form_kwargs(*args, **kwargs)
  104. kwargs["user"] = self.request.user
  105. return kwargs
  106. def get_queryset(self):
  107. return self.model.objects.filter(auth=self.request.user)
  108. class MaintainerDelete(LoginRequiredMixin, DeleteCheckView):
  109. template_name = "whoisdb/obj_delete.html"
  110. model = Maintainer
  111. slug_field = "handle"
  112. slug_url_kwarg = "handle"
  113. success_url = reverse_lazy("whoisdb:dashboard")
  114. def get_queryset(self):
  115. return self.model.objects.filter(auth=self.request.user)
  116. class MaintainerDetail(LoginRequiredMixin, DetailView):
  117. template_name = "whoisdb/handle_show.html"
  118. model = Maintainer
  119. slug_field = "handle"
  120. slug_url_kwarg = "handle"
  121. #context_object_name = "mnt"
  122. class ContactDetail(DetailView):
  123. model = Contact
  124. slug_field = "handle"
  125. slug_url_kwarg = "handle"
  126. context_object_name = "contact"
  127. class ContactEdit(MntGenericMixin, LoginRequiredMixin, UpdateView):
  128. template_name = "whoisdb/obj_edit.html"
  129. model = Contact
  130. form_class = ContactForm
  131. slug_field = "handle"
  132. slug_url_kwarg = "handle"
  133. def get_form_kwargs(self, *args, **kwargs):
  134. kwargs = super(ContactEdit, self).get_form_kwargs(*args, **kwargs)
  135. kwargs["user"] = self.request.user
  136. return kwargs
  137. class ContactCreate(LoginRequiredMixin, CreateView):
  138. template_name = "whoisdb/obj_create.html"
  139. form_class = ContactForm
  140. def get_form_kwargs(self, *args, **kwargs):
  141. kwargs = super(ContactCreate, self).get_form_kwargs(*args, **kwargs)
  142. kwargs["user"] = self.request.user
  143. kwargs["initial"] = {
  144. "handle": "AUTO",
  145. "type": Contact.TYPE_PERSON
  146. }
  147. return kwargs
  148. class ContactDelete(MntGenericMixin, LoginRequiredMixin, DeleteCheckView):
  149. template_name = "whoisdb/obj_delete.html"
  150. model = Contact
  151. slug_field = "handle"
  152. slug_url_kwarg = "handle"
  153. success_url = reverse_lazy("whoisdb:dashboard")
  154. # InetNum
  155. class InetNumCreate(LoginRequiredMixin, CreateView):
  156. template_name = "whoisdb/obj_create.html"
  157. form_class = InetNumForm
  158. def get_form_kwargs(self, *args, **kwargs):
  159. print("NOOOOOOOOOOOOOOOOOOOOOOOOOOOOT", args, kwargs)
  160. kwargs = super(InetNumCreate, self).get_form_kwargs(*args, **kwargs)
  161. kwargs["user"] = self.request.user
  162. kwargs["initial"] = {
  163. "handle": "AUTO",
  164. }
  165. return kwargs
  166. class InetNumDetail(DetailView):
  167. model = InetNum
  168. slug_field = "handle"
  169. slug_url_kwarg = "handle"
  170. context_object_name = "inetnum"
  171. class InetNumEdit(MntGenericMixin, LoginRequiredMixin, UpdateView):
  172. template_name = "whoisdb/obj_edit.html"
  173. model = InetNum
  174. form_class = InetNumForm
  175. slug_field = "handle"
  176. slug_url_kwarg = "handle"
  177. def get_form_kwargs(self, *args, **kwargs):
  178. kwargs = super(InetNumEdit, self).get_form_kwargs(*args, **kwargs)
  179. kwargs["user"] = self.request.user
  180. kwargs["initial"] = {'prefix': str(self.object.getNetwork())}
  181. return kwargs
  182. class InetNumDelete(MntGenericMixin, LoginRequiredMixin, DeleteCheckView):
  183. template_name = "whoisdb/obj_delete.html"
  184. model = InetNum
  185. slug_field = "handle"
  186. slug_url_kwarg = "handle"
  187. success_url = reverse_lazy("whoisdb:dashboard")
  188. # asblock
  189. class ASBlockCreate(LoginRequiredMixin, CreateView):
  190. template_name = "whoisdb/obj_create.html"
  191. form_class = ASBlockForm
  192. def get_form_kwargs(self, *args, **kwargs):
  193. kwargs = super(ASBlockCreate, self).get_form_kwargs(*args, **kwargs)
  194. kwargs["user"] = self.request.user
  195. kwargs["initial"] = {
  196. "handle": "AUTO",
  197. }
  198. return kwargs
  199. class ASBlockDetail(DetailView):
  200. model = ASBlock
  201. slug_field = "handle"
  202. slug_url_kwarg = "handle"
  203. context_object_name = "asblock"
  204. class ASBlockEdit(MntGenericMixin, LoginRequiredMixin, UpdateView):
  205. template_name = "whoisdb/obj_edit.html"
  206. model = ASBlock
  207. form_class = ASBlockForm
  208. slug_field = "handle"
  209. slug_url_kwarg = "handle"
  210. def get_form_kwargs(self, *args, **kwargs):
  211. kwargs = super(ASBlockEdit, self).get_form_kwargs(*args, **kwargs)
  212. kwargs["user"] = self.request.user
  213. #kwargs["initial"] = {'prefix': str(self.object.getNetwork())}
  214. return kwargs
  215. class ASBlockDelete(MntGenericMixin, LoginRequiredMixin, DeleteCheckView):
  216. template_name = "whoisdb/obj_delete.html"
  217. model = ASBlock
  218. slug_field = "handle"
  219. slug_url_kwarg = "handle"
  220. success_url = reverse_lazy("whoisdb:dashboard")
  221. # asnumber
  222. class ASNumberCreate(LoginRequiredMixin, CreateView):
  223. template_name = "whoisdb/obj_create.html"
  224. form_class = ASNumberForm
  225. def get_form_kwargs(self, *args, **kwargs):
  226. kwargs = super(ASNumberCreate, self).get_form_kwargs(*args, **kwargs)
  227. kwargs["user"] = self.request.user
  228. kwargs["initial"] = {
  229. "handle": "AUTO",
  230. }
  231. return kwargs
  232. class ASNumberDetail(DetailView):
  233. model = ASNumber
  234. slug_field = "handle"
  235. slug_url_kwarg = "handle"
  236. context_object_name = "asnumber"
  237. class ASNumberEdit(MntGenericMixin, LoginRequiredMixin, UpdateView):
  238. template_name = "whoisdb/obj_edit.html"
  239. model = ASNumber
  240. form_class = ASNumberForm
  241. slug_field = "handle"
  242. slug_url_kwarg = "handle"
  243. def get_form_kwargs(self, *args, **kwargs):
  244. kwargs = super(ASNumberEdit, self).get_form_kwargs(*args, **kwargs)
  245. kwargs["user"] = self.request.user
  246. return kwargs
  247. class ASNumberDelete(MntGenericMixin, LoginRequiredMixin, DeleteCheckView):
  248. template_name = "whoisdb/obj_delete.html"
  249. model = ASNumber
  250. slug_field = "handle"
  251. slug_url_kwarg = "handle"
  252. success_url = reverse_lazy("whoisdb:dashboard")
  253. class ASAndSubnetWizard(LoginRequiredMixin, SessionWizardView):
  254. form_list = [ASNumberForm, InetNumForm]
  255. template_name = "whoisdb/wizard.html"
  256. def get_form_initial(self, step):
  257. return {"handle": "AUTO"}
  258. def done(self, form_list, **kwargs):
  259. fl = list(form_list)
  260. asNum = fl[0].save()
  261. net = fl[1].save()
  262. messages.info(self.request, "The following objects have been created: AS %s %s, Subnet %s %s" % (asNum.handle, asNum.number, net.handle, net.getNetwork()))
  263. return HttpResponseRedirect(reverse("whoisdb:dashboard"))
  264. def get_form_kwargs(self, *args, **kwargs):
  265. kwargs = super(ASAndSubnetWizard, self).get_form_kwargs(*args, **kwargs)
  266. kwargs["user"] = self.request.user
  267. return kwargs
  268. def get_context_data(self, *args, **kwargs):
  269. d = super(ASAndSubnetWizard, self).get_context_data(*args, **kwargs)
  270. step = d["wizard"]["steps"].step1
  271. if step == 1:
  272. d["message"] = "Create an AS object"
  273. elif step == 2:
  274. d["message"] = "Create a Subnet"
  275. return d