Last active
August 29, 2015 14:02
-
-
Save ymorin007/49b4edfeab5c816d583b to your computer and use it in GitHub Desktop.
Voting form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from crispy_forms.helper import FormHelper | |
from crispy_forms.layout import Submit, Reset | |
from django import forms | |
from fights.models import FightChoice | |
from users.models import User, UserVote | |
class FightChoiceField(forms.ModelChoiceField): | |
widget = forms.RadioSelect() | |
def __init__(self, *args, **kwargs): | |
kwargs['queryset'] = FightChoice.objects.all() | |
kwargs['empty_label'] = None | |
super(FightChoiceField, self).__init__(*args, **kwargs) | |
def set_fight(self, fight_id): | |
self.fight_id = fight_id | |
self.queryset = FightChoice.objects.filter(fight_id=fight_id) | |
class VoteForm(forms.ModelForm): | |
fightchoice = FightChoiceField() | |
def __init__(self, fight_id, *args, **kwargs): | |
super(VoteForm, self).__init__(*args, **kwargs) | |
self.fields['fightchoice'].set_fight(fight_id) | |
class Meta: | |
model = UserVote | |
fields = ['fightchoice'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Fight(TimeStampedModel): | |
event = models.ForeignKey(Event, null=True, related_name='fights') | |
fighter = models.ForeignKey(Fighter, related_name='%(app_label)s_%(class)s_fighter', null=True) | |
fightstatus = models.ForeignKey('fights.FightStatus', null=True) | |
opponent = models.ForeignKey(Fighter, related_name='%(app_label)s_%(class)s_opponent', null=True) | |
folder_name = models.CharField(max_length=500, blank=True, null=True) | |
fight_highlights = models.CharField(max_length=500, blank=True, null=True) | |
fight_order = models.IntegerField(blank=True, null=True) | |
name = models.CharField(max_length=500, blank=True) | |
post_fight_interview = models.CharField(max_length=500, blank=True, null=True) | |
show_fact = models.NullBooleanField(default=True, null=True) | |
total_round = models.IntegerField(blank=True, null=True) | |
view_count = models.IntegerField(default=0, null=True) | |
def __unicode__(self): | |
return '{} vs {}'.format(self.fighter.name, self.opponent.name) | |
class FightChoice(TimeStampedModel): | |
fight = models.ForeignKey(Fight) | |
fighter = models.ForeignKey(Fighter, related_name='as_fighter', null=True) | |
loser = models.ForeignKey(Fighter, related_name='as_opponent', null=True) | |
howitended = models.ForeignKey('fights.HowItEnded', null=True) | |
valid_score = models.NullBooleanField(default=0, null=True) | |
odd = models.IntegerField(blank=True, null=True) | |
numerator = models.IntegerField(blank=True, null=True) | |
denominator = models.IntegerField(blank=True, null=True) | |
points = models.IntegerField(blank=True, null=True) | |
def __unicode__(self): | |
return '{} - {} [{}]'.format( | |
self.fighter.name, self.howitended.name, self.points | |
) | |
class UserVote(models.Model): | |
event = models.ForeignKey(Event) | |
user = models.ForeignKey(User) | |
fightchoice = models.ForeignKey(FightChoice) | |
class HowItEnded(models.Model): | |
name = models.CharField(max_length=50, unique=True) | |
admin = models.BooleanField(default=True) | |
def __unicode__(self): | |
return self.name | |
class Fighter(TimeStampedModel): | |
association = models.ForeignKey('Association', null=True) | |
country = CountryField(null=True) | |
weightclass = models.ForeignKey('fighters.WeightClass', null=True) | |
name = models.CharField(unique=True, max_length=50, blank=True) | |
class UserVote(TimeStampedModel): | |
event = models.ForeignKey('events.Event', related_name='votes') | |
fightchoice = models.ForeignKey('fights.FightChoice') | |
user = models.ForeignKey('users.User') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form method="post" action="">{% csrf_token %} | |
{% for fight, form in forms %} | |
<h3>Fight: {{ fight }}</h3> | |
{{ form.as_p }} | |
{{ form.errors }} | |
{% endfor %} | |
<button>Save</button> | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@login_required | |
def new_vote(request, event_id): | |
""" | |
Vote form | |
:param request: | |
""" | |
context = RequestContext(request) | |
event = Event.objects.get(pk=event_id) | |
data = request.POST if request.method == 'POST' else None | |
forms = [(event, VoteForm(fight.id, prefix='fight-{0}'.format(fight.id), data=data)) | |
for fight in event.fights.all()] | |
context_dict = {'user_badge': user_badge(request.user.id), 'season_active': season_active(), | |
'form': forms} | |
if request.method == 'POST': | |
form = VoteForm(event_id, data=request.POST) | |
if form.is_valid(): | |
vote = form.save(commit=False) | |
vote.user_id = request.user.id | |
vote.event_id = event_id | |
vote.save() | |
else: | |
return render_to_response('users/new_vote.html', context_dict, context) | |
else: | |
return render_to_response('users/new_vote.html', context_dict, context) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment