views.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. from django.shortcuts import render
  2. from django.http import HttpResponse
  3. from django.template import loader
  4. from django.shortcuts import redirect
  5. from django.conf import settings
  6. from django.urls import reverse
  7. from django.contrib.auth.models import Group
  8. import pprint
  9. import vyos
  10. from .models import Instance
  11. from django.contrib.auth.models import User
  12. from django.contrib.auth.models import Group
  13. from django.template.defaultfilters import register
  14. @register.filter
  15. def get_item(dictionary, key):
  16. return dictionary.get(key)
  17. def index(request):
  18. if not request.user.is_authenticated:
  19. return redirect('%s?next=%s' % (reverse('registration-login'), request.path))
  20. #interfaces = vyos.get_interfaces()
  21. all_instances = vyos.instance_getall()
  22. for instance in all_instances:
  23. if group == None:
  24. all_instance[instance]['group'] = "admin"
  25. hostname_default = vyos.get_hostname_prefered(request)
  26. template = loader.get_template('config/instance.html')
  27. context = {
  28. #'interfaces': interfaces,
  29. 'instances': all_instances,
  30. 'hostname_default': hostname_default,
  31. }
  32. return HttpResponse(template.render(context, request))
  33. def users_list(request):
  34. if not request.user.is_authenticated:
  35. return redirect('%s?next=%s' % (reverse('registration-login'), request.path))
  36. #interfaces = vyos.get_interfaces()
  37. all_instances = vyos.instance_getall()
  38. hostname_default = vyos.get_hostname_prefered(request)
  39. users = User.objects.all()
  40. groups = Group.objects.all()
  41. group_show = []
  42. for group in groups:
  43. if group.name != "admin":
  44. group_show.append(group.name)
  45. has_group_add = False
  46. for el in request.POST:
  47. if el.startswith('group-') and request.POST[el]:
  48. pos = el.split("-", 1)
  49. el_username = pos[1]
  50. el_groupname = request.POST[el]
  51. # test also if username is member of admin or superuser, than this one should not being no group
  52. if el_groupname not in ['admin']:
  53. try:
  54. el_userid = User.objects.get(username=el_username)
  55. except User.DoesNotExist:
  56. print("zerou")
  57. return redirect('config:users_list')
  58. try:
  59. if el_userid.groups.exists():
  60. for g in el_userid.groups.all():
  61. el_userid.groups.remove(g)
  62. except Group.DoesNotExist:
  63. print("zerou2")
  64. return redirect('config:users_list')
  65. print("kkk", el_groupname, el_username)
  66. if el_groupname == "--remove--":
  67. has_group_add = has_group_add + 1
  68. else:
  69. el_groupadd = Group.objects.get(name=el_groupname)
  70. el_groupadd.user_set.add(el_userid)
  71. has_group_add = has_group_add + 1
  72. if has_group_add > 0:
  73. return redirect('config:users-list')
  74. user_groups = {}
  75. for user in users:
  76. user_groups_list = user.groups.all()
  77. if len(user_groups_list) > 0:
  78. user_groups[str(user)] = str(user_groups_list[0])
  79. else:
  80. user_groups[str(user)] = None
  81. template = loader.get_template('config/users_list.html')
  82. context = {
  83. #'interfaces': interfaces,
  84. 'instances': all_instances,
  85. 'hostname_default': hostname_default,
  86. 'users' : users,
  87. 'groups': group_show,
  88. 'user_groups': user_groups
  89. }
  90. return HttpResponse(template.render(context, request))
  91. def groups_list(request):
  92. if not request.user.is_authenticated:
  93. return redirect('%s?next=%s' % (reverse('registration-login'), request.path))
  94. #interfaces = vyos.get_interfaces()
  95. all_instances = vyos.instance_getall()
  96. hostname_default = vyos.get_hostname_prefered(request)
  97. groups = Group.objects.all()
  98. template = loader.get_template('config/groups_list.html')
  99. context = {
  100. #'interfaces': interfaces,
  101. 'instances': all_instances,
  102. 'hostname_default': hostname_default,
  103. 'groups' : groups,
  104. }
  105. return HttpResponse(template.render(context, request))
  106. def instances(request):
  107. if not request.user.is_authenticated:
  108. return redirect('%s?next=%s' % (reverse('registration-login'), request.path))
  109. all_instances = vyos.instance_getall()
  110. hostname_default = vyos.get_hostname_prefered(request)
  111. print(all_instances)
  112. if hostname_default == None:
  113. if all_instances.count() > 0:
  114. for i in all_instances:
  115. pprint.pprint(i.hostname)
  116. instance_default(request, i.hostname)
  117. else:
  118. return redirect('config:instance-add')
  119. groups = Group.objects.all()
  120. template = loader.get_template('config/instances.html')
  121. context = {
  122. 'instances': all_instances,
  123. 'hostname_default': hostname_default,
  124. 'groups' : groups,
  125. }
  126. return HttpResponse(template.render(context, request))
  127. def instance_add(request):
  128. if not request.user.is_authenticated:
  129. return redirect('%s?next=%s' % (reverse('registration-login'), request.path))
  130. #interfaces = vyos.get_interfaces()
  131. all_instances = vyos.instance_getall()
  132. hostname_default = vyos.get_hostname_prefered(request)
  133. if len(request.POST) > 0:
  134. instance = Instance()
  135. instance.alias = request.POST['alias']
  136. instance.hostname = request.POST['hostname']
  137. instance.port = request.POST['port']
  138. instance.key = request.POST['key']
  139. if 'https' in request.POST:
  140. instance.https = request.POST['https']
  141. else:
  142. instance.https = False
  143. instance_id = instance.save()
  144. return redirect('config:instances')
  145. else:
  146. instance_id = 0
  147. template = loader.get_template('config/instance_add.html')
  148. context = {
  149. 'hostname_default': hostname_default,
  150. 'instance_id': instance_id,
  151. 'instances': all_instances,
  152. }
  153. return HttpResponse(template.render(context, request))
  154. def group_add(request):
  155. if not request.user.is_authenticated:
  156. return redirect('%s?next=%s' % (reverse('registration-login'), request.path))
  157. #interfaces = vyos.get_interfaces()
  158. all_instances = vyos.instance_getall()
  159. hostname_default = vyos.get_hostname_prefered(request)
  160. error_message = None
  161. if len(request.POST) > 0 and 'name' in request.POST:
  162. try:
  163. group_get = Group.objects.get(name=request.POST['name'])
  164. error_message = 'Group already exists'
  165. except Group.DoesNotExist:
  166. group_create = Group(name=request.POST['name'])
  167. group_create.save()
  168. return redirect('config:groups-list')
  169. else:
  170. instance_id = 0
  171. template = loader.get_template('config/group_add.html')
  172. context = {
  173. 'hostname_default': hostname_default,
  174. 'instance_id': instance_id,
  175. 'instances': all_instances,
  176. 'error_message' : error_message
  177. }
  178. return HttpResponse(template.render(context, request))
  179. def user_add(request):
  180. if not request.user.is_authenticated:
  181. return redirect('%s?next=%s' % (reverse('registration-login'), request.path))
  182. #interfaces = vyos.get_interfaces()
  183. all_instances = vyos.instance_getall()
  184. hostname_default = vyos.get_hostname_prefered(request)
  185. error_message = None
  186. count = 0
  187. name = ''
  188. if 'name' in request.POST:
  189. name = request.POST['name']
  190. count += 1
  191. username = ''
  192. if 'username' in request.POST:
  193. username = request.POST['username']
  194. count += 1
  195. password = ''
  196. if 'password' in request.POST:
  197. password = request.POST['password']
  198. count += 1
  199. email = ''
  200. if 'email' in request.POST:
  201. email = request.POST['email']
  202. count += 1
  203. if count >= 4:
  204. try:
  205. user = User.objects.get(username=username)
  206. error_message = 'Username already exists'
  207. except User.DoesNotExist:
  208. user_create = User(
  209. username=username,
  210. email=email,
  211. password=password,
  212. last_name=name
  213. )
  214. user_create.save()
  215. return redirect('config:users-list')
  216. template = loader.get_template('config/user_add.html')
  217. context = {
  218. 'hostname_default': hostname_default,
  219. 'instances': all_instances,
  220. 'error_message' : error_message,
  221. 'name' : name,
  222. 'username' : username,
  223. 'password' : password,
  224. 'email' : email,
  225. }
  226. return HttpResponse(template.render(context, request))
  227. def instance_conntry(request, hostname):
  228. if not request.user.is_authenticated:
  229. return redirect('%s?next=%s' % (reverse('registration-login'), request.path))
  230. all_instances = vyos.instance_getall()
  231. hostname_default = vyos.get_hostname_prefered(request)
  232. # permcheck
  233. instance = Instance.objects.get(hostname=hostname)
  234. connected = vyos.conntry(hostname)
  235. if connected == True:
  236. request.session['hostname'] = hostname
  237. template = loader.get_template('config/instance_conntry.html')
  238. context = {
  239. 'instance': instance,
  240. "connected": connected,
  241. 'instances': all_instances,
  242. 'hostname_default': hostname_default,
  243. }
  244. return HttpResponse(template.render(context, request))
  245. # get default instance or set default instance
  246. def instance_change(request, hostname = False):
  247. if not request.user.is_authenticated:
  248. return redirect('%s?next=%s' % (reverse('registration-login'), request.path))
  249. all_instances = vyos.instance_getall()
  250. #method = None
  251. if hostname != "__none__":
  252. #method = "param"
  253. pass
  254. elif hostname == "__none__":
  255. #method = "get"
  256. hostname = request.POST.get('vyos-id', False)
  257. # permcheck
  258. if hostname != False:
  259. try:
  260. instance = Instance.objects.get(hostname=hostname)
  261. except Instance.DoesNotExist:
  262. print("instance not exists: " + str(hostname))
  263. return redirect('config:instances')
  264. if instance:
  265. connected = vyos.conntry(hostname)
  266. # show some error when not connected
  267. if connected == True:
  268. request.session['hostname'] = hostname
  269. instance.main = True
  270. instance.save()
  271. return redirect('config:instances')
  272. def instance_remove(request, hostname):
  273. if not request.user.is_authenticated:
  274. return redirect('%s?next=%s' % (reverse('registration-login'), request.path))
  275. all_instances = vyos.instance_getall()
  276. # permcheck
  277. instance = Instance.objects.get(hostname=hostname)
  278. hostname_default = vyos.get_hostname_prefered(request)
  279. #if hostname_default != hostname:
  280. instance.delete()
  281. return redirect('config:instances')
  282. def instance_changegroup(request, hostname):
  283. if not request.user.is_authenticated:
  284. return redirect('%s?next=%s' % (reverse('registration-login'), request.path))
  285. all_instances = vyos.instance_getall()
  286. group_name = request.POST.get('group')
  287. print(group_name, hostname)
  288. if group_name == "__admin__":
  289. instance = Instance.objects.get(hostname=hostname)
  290. instance.group = None
  291. instance.save()
  292. else:
  293. group = Group.objects.get(name=group_name)
  294. instance = Instance.objects.get(hostname=hostname)
  295. instance.group = group
  296. instance.save()
  297. return redirect('config:instances')