# Path: badges/models.py
# class Badge(models.Model):
#     """ Parent class
#         Base Badge.
#     """
#     name = models.CharField(blank=False, null=False, max_length=255)
#     title = models.CharField(verbose_name=_('Title given with badge (if proceed)'), blank=True,
#                              null=True, max_length=255)
#     description = models.TextField(blank=False, null=False)
#     image = models.ImageField(upload_to='badges', blank=False, null=False)
#     date = models.DateTimeField(verbose_name='Creation date', name="date", auto_now=True)
#     experience = models.DecimalField(verbose_name=_('Experience gained with this badge'),
#                                      default=5.0, decimal_places=2, max_digits=4)
# 
#     def __unicode__(self):
#         return "{}".format(self.name)
# 
#     @property
#     def is_language(self):
#         try:
#             return self.languagebadge
#         except LanguageBadge.DoesNotExist:
#             return False
# 
#     @property
#     def is_fidelity(self):
#         try:
#             return self.fidelitybadge
#         except FidelityBadge.DoesNotExist:
#             return False
# 
#     @property
#     def is_fork(self):
#         try:
#             return self.forkbadge
#         except ForkBadge.DoesNotExist:
#             return False
# 
#     @property
#     def is_license(self):
#         try:
#             return self.licensebadge
#         except LicenseBadge.DoesNotExist:
#             return False
# 
# class CustomBadge(Badge):
#     """ Custom Badge
#         These kind of badge is granted if the Developer knows the 'code' to get it
#         before the expiration date!
# 
#         i.e. 'Assist to GitGaming presentation'
#         i.e. 'Be a participant of the CUSL Contest'
#     """
# 
#     code = models.CharField(blank=False, null=False, unique=True, max_length=255)
#     expiration_date = models.DateTimeField(blank=False, null=False)
# 
# Path: developers/models.py
# class Achievement(models.Model):
#     """
#         Achievement class. This model is used to connect Users with Badges. It will have information
#         about each badge earned by every user and the date of the achievement.
#     """
#     date = models.DateField(auto_now=True)
#     user = models.ForeignKey(Developer, related_name='developer')
#     badge = models.ForeignKey(Badge, related_name='badge')
# 
#     def __unicode__(self):
#         return 'Achievement {} for user {}'.format(self.badge, self.user)


from django.shortcuts import render, render_to_response
from .models import Badge, CustomBadge
from developers.models import Achievement
from django.utils.translation import ugettext as _
from django.template import RequestContext
from datetime import datetime
from django.utils import timezone

# Create your views here.


def grant_custom_badge(request, code):
    try:
        b = CustomBadge.objects.get(code=code)
        try:
            Achievement.objects.get(user=request.user.django_user, badge=b)
            return render_to_response('badges/custom_badge_error.html', {'msg': _('Yoy already have this badge!')},
                                      context_instance=RequestContext(request))

# Path: badges/models.py
# class Badge(models.Model):
#     """ Parent class
#         Base Badge.
#     """
#     name = models.CharField(blank=False, null=False, max_length=255)
#     title = models.CharField(verbose_name=_('Title given with badge (if proceed)'), blank=True,
#                              null=True, max_length=255)
#     description = models.TextField(blank=False, null=False)
#     image = models.ImageField(upload_to='badges', blank=False, null=False)
#     date = models.DateTimeField(verbose_name='Creation date', name="date", auto_now=True)
#     experience = models.DecimalField(verbose_name=_('Experience gained with this badge'),
#                                      default=5.0, decimal_places=2, max_digits=4)
# 
#     def __unicode__(self):
#         return "{}".format(self.name)
# 
#     @property
#     def is_language(self):
#         try:
#             return self.languagebadge
#         except LanguageBadge.DoesNotExist:
#             return False
# 
#     @property
#     def is_fidelity(self):
#         try:
#             return self.fidelitybadge
#         except FidelityBadge.DoesNotExist:
#             return False
# 
#     @property
#     def is_fork(self):
#         try:
#             return self.forkbadge
#         except ForkBadge.DoesNotExist:
#             return False
# 
#     @property
#     def is_license(self):
#         try:
#             return self.licensebadge
#         except LicenseBadge.DoesNotExist:
#             return False
# 
# class CustomBadge(Badge):
#     """ Custom Badge
#         These kind of badge is granted if the Developer knows the 'code' to get it
#         before the expiration date!
# 
#         i.e. 'Assist to GitGaming presentation'
#         i.e. 'Be a participant of the CUSL Contest'
#     """
# 
#     code = models.CharField(blank=False, null=False, unique=True, max_length=255)
#     expiration_date = models.DateTimeField(blank=False, null=False)
# 
# Path: developers/models.py
# class Achievement(models.Model):
#     """
#         Achievement class. This model is used to connect Users with Badges. It will have information
#         about each badge earned by every user and the date of the achievement.
#     """
#     date = models.DateField(auto_now=True)
#     user = models.ForeignKey(Developer, related_name='developer')
#     badge = models.ForeignKey(Badge, related_name='badge')
# 
#     def __unicode__(self):
#         return 'Achievement {} for user {}'.format(self.badge, self.user)


# Path: badges/views.py
from django.shortcuts import render, render_to_response
from .models import Badge, CustomBadge
from developers.models import Achievement
from django.utils.translation import ugettext as _
from django.template import RequestContext
from datetime import datetime
from django.utils import timezone


# Create your views here.


def grant_custom_badge(request, code):
    try:
        b = CustomBadge.objects.get(code=code)
        try:
            Achievement.objects.get(user=request.user.django_user, badge=b)
            return render_to_response('badges/custom_badge_error.html', {'msg': _('Yoy already have this badge!')},
                                      context_instance=RequestContext(request))

        except Achievement.DoesNotExist: