Website to manage tibia accounts and characters
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 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from django import forms
  2. from django.contrib.auth.mixins import LoginRequiredMixin
  3. from django.contrib.auth.models import User
  4. from django.shortcuts import render, render_to_response
  5. from django.urls import reverse, reverse_lazy
  6. from django.views import generic
  7. from django.views.generic.edit import CreateView, FormView
  8. # Create your views here.
  9. from accounts.forms import CreateCharacterForm
  10. from accounts.models import Accounts, Players, Towns
  11. from accounts.utils import LoginHelper, VocationHelper
  12. class RegisterAccountView(CreateView):
  13. model = Accounts
  14. fields = ['name', 'password', 'email']
  15. template_name = 'register_account.html'
  16. def form_valid(self, form):
  17. self.object = form.save(commit=False)
  18. # create django user
  19. u, created = User.objects.get_or_create(
  20. username=self.object.name,
  21. email=self.object.email,
  22. )
  23. if created:
  24. u.set_password(self.object.password)
  25. u.save()
  26. # create tibia account with sha1 hashed password
  27. hashed_password = LoginHelper.hash_password(self.object.password)
  28. self.object.password = hashed_password
  29. self.object.linked_django_user = u
  30. self.object.save()
  31. return render_to_response(self.template_name, self.get_context_data())
  32. class IndexView(generic.ListView):
  33. template_name = 'index.html'
  34. context_object_name = 'best_players'
  35. def get_queryset(self):
  36. return Players.objects.filter(group_id=1).order_by('-level').values('name', 'level')[:10]
  37. class PlayerView(LoginRequiredMixin, generic.ListView):
  38. template_name = 'players.html'
  39. context_object_name = 'players'
  40. def get_queryset(self):
  41. players = Players.objects.filter(account__linked_django_user=self.request.user)\
  42. .values('name', 'level', 'vocation')
  43. return players
  44. def get_context_data(self, **kwargs):
  45. context = super(PlayerView, self).get_context_data(**kwargs)
  46. players = context['players']
  47. context['players'] = [{
  48. 'name': p['name'],
  49. 'level': p['level'],
  50. 'vocation': VocationHelper.vocation_to_string(p['vocation'])}
  51. for p in players]
  52. return context
  53. class CreateCharView(LoginRequiredMixin, FormView):
  54. form_class = CreateCharacterForm
  55. template_name = 'add_character.html'
  56. success_url = reverse_lazy('accounts:characters')
  57. def form_valid(self, form):
  58. rookgaard = Towns.objects.get(name='Rookgaard Tutorial Island')
  59. start_health = 150
  60. start_mana = 55
  61. start_cap = 400
  62. user = self.request.user
  63. account = Accounts.objects.get(linked_django_user=user)
  64. player = Players.objects.create(
  65. name=form.cleaned_data['name'],
  66. level=1,
  67. #vocation=int(form.cleaned_data['vocation']),
  68. vocation=0,
  69. sex=int(form.cleaned_data['sex']),
  70. posx=rookgaard.posx,
  71. posy=rookgaard.posy,
  72. posz=rookgaard.posz,
  73. health=start_health,
  74. healthmax=start_health,
  75. mana=start_mana,
  76. manamax=start_mana,
  77. cap=start_cap,
  78. account_id=account.id,
  79. town_id=rookgaard.id,
  80. )
  81. return super(CreateCharView, self).form_valid(form)