views.py 11 KB

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