from django.views.generic.edit import FormView
from django.core.urlresolvers import reverse
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.sites.shortcuts import get_current_site
from django.utils import timezone
from .forms import DonateForm
from .models import PremiumDonation
import uuid


class DonateView(FormView):
    template_name = 'donations/donate.html'
    form_class = DonateForm
    success_url = '/thanks/'

    def get_context_data(self, **kwargs):
        context = super(DonateView, self).get_context_data(**kwargs)
        context['steam'] = self._get_steam()
        if self.request.user.is_authenticated():
            try:
                context['donation'] = PremiumDonation.objects.get(user=self.request.user)
                if context['donation'].end_time > timezone.now():
                    context['donation_ended'] = False
                else:
                    context['donation_ended'] = True
            except PremiumDonation.DoesNotExist:
                context['donation'] = None
        else:
            context['donation'] = None
        return context

    def _get_steam(self):
        if self.request.user.is_authenticated():
            try:
                u = self.request.user.social_auth.filter(provider="steam").get()
                return u.uid
            except ObjectDoesNotExist:
                pass

        return None

    def get_initial(self):
        """
        Returns the initial data to use for forms on this view.
        """

        steam = self._get_steam()
            
        domain = get_current_site(self.request).domain

        initial = {
            "business": settings.PAYPAL_RECEIVER_EMAIL,
            "item_name": "Donation",
            "invoice": str(steam)+uuid.uuid4.hex,
            "notify_url": "https://" + domain + reverse('paypal-ipn'),
            "return_url": "https://game.azelphur.com/",
            "cancel_return": "https://game.azelphur.com/donate",
            "custom": steam,  # Custom command to correlate to some function later (optional)
        }

        return initial

@@ -1802,16 +1802,20 @@
 (steam)+
+%22:%22+
 uuid.uui

Format invoice ID a little nicer