Commits

jgsogo committed 317814f Draft

creación vistas usuarios

  • Participants
  • Parent commits d17f014

Comments (0)

Files changed (3)

File confidence_circle/forms.py

 # encoding: utf-8
 
 from django import forms
+from django.utils.translation import ugettext_lazy as _
+from django.contrib.sites.models import get_current_site
+from django.conf import settings
 
-from confidence_circle.models import ConfidenceCircle
+from organizations.backends import invitation_backend
+
+from confidence_circle.models import ConfidenceCircle, ConfidenceCircleStatus
 
 class ConfidenceCircleForm(forms.ModelForm):
 
     class Meta:
         model = ConfidenceCircle
+        exclude = ('user', 'slug',)
 
-    def __init__(self, owner, *args, **kwargs):
-        self.owner = owner
+    def __init__(self, request, *args, **kwargs):
+        self.request = request
         super(ConfidenceCircleForm, self).__init__(*args, **kwargs)
 
     def save(self, commit=True):
         instance = super(ConfidenceCircleForm, self).save(commit=False)
-        instance.user = self.owner
+        instance.user = self.request.user
         instance.save()
-        return instance
+        return instance
+
+class ConfidenceCircleUserAddForm(forms.ModelForm):
+    email = forms.EmailField(max_length=75)
+
+    class Meta:
+        model = ConfidenceCircleStatus
+        exclude = ('circle', 'user', 'status',)
+
+    def __init__(self, request, confidencecircle, *args, **kwargs):
+        self.request = request
+        self.confidencecircle = confidencecircle
+        super(ConfidenceCircleUserAddForm, self).__init__(*args, **kwargs)
+
+    def save(self, *args, **kwargs):
+        """
+        The save method should create a new user in current ConfidenceCircle linking the User
+        matching the provided email address. Of not matching User is found it should kick off the
+        registration process. It needs to create a User in order to link it to the ConfidenceCircle
+        """
+        try:
+            user = settings.AUTH_USER_MODEL.objects.get(email__iexact=self.cleaned_data['email'])
+        except settings.AUTH_USER_MODEL.MultipleObjectsReturned:
+            raise forms.ValidationError(_('This email address has been used multiple times.'))
+        except settings.AUTH_USER_MODEL.DoesNotExist:
+            user = invitation_backend().invite_by_mail(
+                self.cleaned_data['email'],
+                **{'domain': get_current_site(self.request),
+                   'confidencecircle': self.confidencecircle}
+                )
+        return ConfidenceCircleStatus.objects.create(user=user, circle=self.confidencecircle)
+
+    def clean_email(self):
+        email = self.cleaned_data['email']
+        if self.confidencecircle.users.filter(email=email).exists():
+            raise forms.ValidationError(_('There is already an user with this email address!'))
+
+class ConfidenceCircleUserForm(forms.ModelForm):
+    class Meta:
+        model = ConfidenceCircleStatus
+        exclude = ('user', 'circle',)

File confidence_circle/models.py

 
 class ConfidenceCircle(models.Model):
     name = models.CharField(max_length=100)
-    slug = models.SlugField(editable=False)
-    user = models.ForeignKey(settings.AUTH_USER_MODEL, editable=False)
+    slug = models.SlugField()
+    user = models.ForeignKey(settings.AUTH_USER_MODEL)
     users = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='in_confidencecircle_set', through='confidence_circle.ConfidenceCircleStatus', blank=True, null=True)
 
     class Meta:

File confidence_circle/views.py

 
 from django.views.generic import ListView, DetailView, UpdateView, CreateView, DeleteView
 from django.core.urlresolvers import reverse
+from django.http import Http404
+from django.shortcuts import render, redirect
+from django.contrib.sites.models import get_current_site
+
+from organizations.backends import invitation_backend, registration_backend
 
 from confidence_circle.models import ConfidenceCircle
-from confidence_circle.forms import ConfidenceCircleForm
+from confidence_circle.forms import ConfidenceCircleForm, ConfidenceCircleUserAddForm, ConfidenceCircleUserForm
 from confidence_circle.mixins import ConfidenceCircleMixin
 
 # Views about ConfidenceCircle model
 
     def get_form_kwargs(self):
         kwargs = super(ConfidenceCircleCreate, self).get_form_kwargs()
-        kwargs.update({'owner':self.request.user})
+        kwargs.update({'request':self.request})
         return kwargs
 
     def get_success_url(self):
 
     def get_form_kwargs(self):
         kwargs = super(ConfidenceCircleUpdate, self).get_form_kwargs()
-        kwargs.update({'owner':self.request.user})
+        kwargs.update({'request':self.request})
         return kwargs
 
 
     def get_success_url(self):
         return reverse("confidence_circle_home")
 
+
 # Views related to users
 
 class ConfidenceCircleUserList(ConfidenceCircleMixin, ListView):
     def get(self, request, *args, **kwargs):
-        pass
+        self.confidencecircle = self.get_confidencecircle()
+        self.object_list = self.confidencecircle.users.all()
+        context = self.get_context_data(object_list = self.object_list, confidencecircle_users = self.object_list, confidencecircle = self.confidencecircle)
+        return self.render_to_response(context)
+
+class ConfidenceCircleUserDetail(ConfidenceCircleUserMixin, DetailView):
+    pass
+
+class ConfidenceCircleUserCreate(ConfidenceCircleMixin, CreateView):
+    form_class = ConfidenceCircleUserAddForm
+    template_name = 'confidence_circle/confidencecircleuser_form.html'
+
+    def get_success_url(self):
+        return reverse('confidence_circle_user_list', kwargs={'confidencecircle_pk': self.confidencecircle.pk, 'slug':self.confidencecircle.slug})
+
+    def get_form_kwargs(self):
+        kwargs = super(ConfidenceCircleUserCreate, self).get_form_kwargs()
+        kwargs.update({'confidence_circle': self.confidencecircle})
+        return kwargs
+
+    def get(self, request, *args, **kwargs):
+        self.confidencecircle = self.get_object()
+        return super(ConfidenceCircleUserCreate, self).get(request, *args, **kwargs)
+
+    def post(self, request, *args, **kwargs):
+        self.confidencecircle = self.get_object()
+        return super(ConfidenceCircleUserCreate, self).post(request, *args, **kwargs)
+
+class ConfidenceCircleUserRemind(ConfidenceCircleUserMixin, DetailView):
+    template_name = 'confidence_circle/confidencecircleuser_remind.html'
+
+    def get_object(self, **kwargs):
+        self.confidencecircle_user = super(ConfidenceCircleUserRemind, self).get_object()
+        if self.confidencecircle_user.user.is_active:
+            raise Http404(_("Already active"))
+        return self.confidencecircle_user
+
+    def post(self, request, *args, **kwargs):
+        self.object = self.get_object()
+        invitation_backend().send_reminder(self.object.user,
+            **{'domain': get_current_site(self.request),
+               'confidencecircle': self.confidencecircle}
+            )
+        return redirect(self.object)
+
+class ConfidenceCircleUserUpdate(ConfidenceCircleUserMixin, UpdateView):
+    form_class = ConfidenceCircleUserForm
+
+class ConfidenceCircleUserDelete(ConfidenceCircleUserMixin, DeleteView):
+    def get_success_url(self):
+        return reverse('confidence_circle_user_list', kwargs={'confidencecircle_pk': self.object.circle.pk, 'slug':self.object.circle.slug})
+