# Path: characters/forms.py
# class CharacterForm(forms.ModelForm):
# 
#     class Meta:
#         model = Character
#         fields = ['name', 'background', 'race', 'cclass', 'alignment',
#                 'strength', 'dexterity', 'constitution', 'intelligence',
#                 'wisdom', 'charisma', 'max_hit_points']
# 
#     def __init__(self, *args, **kwargs):
#         super(CharacterForm, self).__init__(*args, **kwargs)
#         for field in ['strength', 'dexterity', 'constitution', 'intelligence',
#                 'wisdom', 'charisma', 'max_hit_points']:
#             self.fields[field].widget.attrs['readonly'] = True
# 
# Path: characters/models.py
# class Character(models.Model):
# 
#     objects = CharacterManager()
# 
#     player = models.ForeignKey(User)
# 
#     name = models.CharField(max_length=200)
#     background = models.TextField()
# 
#     race = models.ForeignKey(Race)
#     # This name isn't ideal, but 'class' is a Python builtin, so here we are...
#     # I went with 'cclass' as short for 'character class'.
#     cclass = models.ForeignKey(Class, verbose_name="class")
# 
#     alignment = models.CharField(max_length=2, choices=Alignment.CHOICES,
#             default=Alignment.NEUTRAL)
# 
#     level = models.IntegerField(default=1)
#     experience_points = models.IntegerField(default=0)
# 
#     max_hit_points = models.IntegerField(default=10)
#     current_hit_points = models.IntegerField(default=10)
# 
#     strength = models.IntegerField(default=generate_stat)
#     dexterity = models.IntegerField(default=generate_stat)
#     constitution = models.IntegerField(default=generate_stat)
#     intelligence = models.IntegerField(default=generate_stat)
#     wisdom = models.IntegerField(default=generate_stat)
#     charisma = models.IntegerField(default=generate_stat)
# 
#     inventory = models.ManyToManyField(Item, null=True, blank=True)
# 
#     created = models.DateTimeField(auto_now_add=True)
#     modified = models.DateTimeField(auto_now=True)
# 
#     def __str__(self):
#         return self.name
# 
# class Class(models.Model):
# 
#     name = models.CharField('class', max_length=200)
#     description = models.TextField()
# 
#     def __str__(self):
#         return self.name
# 
#     class Meta:
#         verbose_name_plural = "Classes"
# 
# class Race(models.Model):
# 
#     name = models.CharField('race', max_length=200)
#     description = models.TextField()
# 
#     def __str__(self):
#         return self.name


from django.core.urlresolvers import reverse
from django.shortcuts import get_object_or_404, redirect, render
from django.views.generic import ListView, DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from characters.forms import CharacterForm
from characters.models import Character, Class, Race
    def get_queryset(self):
        return Character.objects.all().order_by('name')


class CharacterDetailView(DetailView):

    model = Character
    template_name = 'characters/view_character.html'


class CharacterCreationView(CreateView):

    model = Character
    template_name = 'characters/create_character.html'
    form_class = CharacterForm

    def form_valid(self, form):
        form.instance.player = self.request.user
        return super(CharacterCreationView, self).form_valid(form)

    def get_queryset(self):
        return Character.objects.for_user(self.request.user)

    def get_success_url(self):
        return reverse('characters:view', kwargs={'pk': self.object.pk})


class CharacterUpdateView(UpdateView):

    model = Character

# Path: characters/forms.py
# class CharacterForm(forms.ModelForm):
# 
#     class Meta:
#         model = Character
#         fields = ['name', 'background', 'race', 'cclass', 'alignment',
#                 'strength', 'dexterity', 'constitution', 'intelligence',
#                 'wisdom', 'charisma', 'max_hit_points']
# 
#     def __init__(self, *args, **kwargs):
#         super(CharacterForm, self).__init__(*args, **kwargs)
#         for field in ['strength', 'dexterity', 'constitution', 'intelligence',
#                 'wisdom', 'charisma', 'max_hit_points']:
#             self.fields[field].widget.attrs['readonly'] = True
# 
# Path: characters/models.py
# class Character(models.Model):
# 
#     objects = CharacterManager()
# 
#     player = models.ForeignKey(User)
# 
#     name = models.CharField(max_length=200)
#     background = models.TextField()
# 
#     race = models.ForeignKey(Race)
#     # This name isn't ideal, but 'class' is a Python builtin, so here we are...
#     # I went with 'cclass' as short for 'character class'.
#     cclass = models.ForeignKey(Class, verbose_name="class")
# 
#     alignment = models.CharField(max_length=2, choices=Alignment.CHOICES,
#             default=Alignment.NEUTRAL)
# 
#     level = models.IntegerField(default=1)
#     experience_points = models.IntegerField(default=0)
# 
#     max_hit_points = models.IntegerField(default=10)
#     current_hit_points = models.IntegerField(default=10)
# 
#     strength = models.IntegerField(default=generate_stat)
#     dexterity = models.IntegerField(default=generate_stat)
#     constitution = models.IntegerField(default=generate_stat)
#     intelligence = models.IntegerField(default=generate_stat)
#     wisdom = models.IntegerField(default=generate_stat)
#     charisma = models.IntegerField(default=generate_stat)
# 
#     inventory = models.ManyToManyField(Item, null=True, blank=True)
# 
#     created = models.DateTimeField(auto_now_add=True)
#     modified = models.DateTimeField(auto_now=True)
# 
#     def __str__(self):
#         return self.name
# 
# class Class(models.Model):
# 
#     name = models.CharField('class', max_length=200)
#     description = models.TextField()
# 
#     def __str__(self):
#         return self.name
# 
#     class Meta:
#         verbose_name_plural = "Classes"
# 
# class Race(models.Model):
# 
#     name = models.CharField('race', max_length=200)
#     description = models.TextField()
# 
#     def __str__(self):
#         return self.name


# Path: characters/views.py
from django.core.urlresolvers import reverse
from django.shortcuts import get_object_or_404, redirect, render
from django.views.generic import ListView, DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from characters.forms import CharacterForm
from characters.models import Character, Class, Race

    def get_queryset(self):
        return Character.objects.all().order_by('name')


class CharacterDetailView(DetailView):

    model = Character
    template_name = 'characters/view_character.html'


class CharacterCreationView(CreateView):

    model = Character
    template_name = 'characters/create_character.html'
    form_class = CharacterForm

    def form_valid(self, form):
        form.instance.player = self.request.user
        return super(CharacterCreationView, self).form_valid(form)

    def get_queryset(self):
        return Character.objects.for_user(self.request.user)

    def get_success_url(self):
        return reverse('characters:view', kwargs={'pk': self.object.pk})


class CharacterUpdateView(UpdateView):

    model = Character

    form_class = CharacterForm