Skip to content

Commit

Permalink
Add user_invited_to_hunch event.
Browse files Browse the repository at this point in the history
  • Loading branch information
adammck committed Nov 29, 2011
1 parent ad1b350 commit 7b84ada
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 7 deletions.
10 changes: 10 additions & 0 deletions hunchworks/events/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,13 @@ def comment_posted(sender, instance, created, **kwargs):
user_profile.create_event(
"comment_posted_to_hunchevidence",
comment=comment)


# user_invited(sender=Hunch)
def user_invited_to_hunch(sender, instance, inviter, invitee, message, **kwargs):
invitee.create_event(
"user_invited_to_hunch",
hunch=instance,
inviter=inviter,
invitee=invitee,
message=message)
5 changes: 3 additions & 2 deletions hunchworks/events/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@


def create_event(target_object, event_type, **kwargs):
html = _render(event_type, **kwargs)
now = datetime.datetime.now()
html = _render(event_type, now=now, **kwargs)

return _collection().insert({
"created_at": datetime.datetime.now(),
"content_type_id": _content_type_id(target_object),
"object_id": target_object.pk,
"type": event_type,
"created_at": now,
"html": html
})

Expand Down
7 changes: 7 additions & 0 deletions hunchworks/forms/invite.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@


class InviteForm(forms.Form):
hunch = forms.ModelChoiceField(models.Hunch.objects.all(), widget=forms.HiddenInput)

recipients = TokenField(models.InviteProxy)

message = forms.CharField(label="Personal message", widget=forms.Textarea, required=False,
help_text="This message will be sent to every user which you invite, " +
"along with a link to your hunch.")

def send_invites(self, inviter):
hunch = self.cleaned_data["hunch"]
for invite_proxy in self.cleaned_data["recipients"]:
hunch.invite(invite_proxy, inviter, self.cleaned_data["message"])
26 changes: 24 additions & 2 deletions hunchworks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from django.db.models.signals import pre_save, post_save
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from hunchworks.signals import user_invited
from hunchworks import hunchworks_enums
from hunchworks import events

Expand Down Expand Up @@ -151,6 +152,20 @@ class Meta:
def __unicode__(self):
return self.title

def invite(self, invite_proxy, inviter, message):
obj = invite_proxy.content_object

if isinstance(obj, UserProfile):
self.user_profiles.add(obj)

user_invited.send(
sender=Hunch, instance=self,
inviter=inviter, invitee=obj,
message=message)

else:
raise NotImplementedError

@models.permalink
def get_absolute_url(self):
return ("hunch", [self.pk])
Expand Down Expand Up @@ -283,6 +298,10 @@ def evidence_count(self):
events.hunch_created,
sender=Hunch)

user_invited.connect(
events.user_invited_to_hunch,
sender=Hunch)


class Evidence(models.Model):
title = models.CharField(verbose_name="Short description", max_length=100, blank=True,
Expand Down Expand Up @@ -342,6 +361,9 @@ def __unicode__(self):
def get_absolute_url(self):
return ("group", [self.pk])

def invite_to_hunch(self, sent_by, hunch):
hunch.groups.add(self)

def logo_url(self):
if self.logo:
return self.logo.url
Expand Down Expand Up @@ -471,7 +493,7 @@ def search(cls, term, user_profile=None):

class Invitation(models.Model):
email = models.CharField(max_length=100)
invited_by = models.ForeignKey('UserProfile', related_name="invitations")
sent_by = models.ForeignKey('UserProfile', related_name="invitations")
hunch = models.ForeignKey('Hunch', null=True, blank=True)

def __unicode__(self):
Expand Down Expand Up @@ -583,7 +605,7 @@ class InviteProxy(models.Model):
integration, which is kind of the whole point.
"""

MODELS = [UserProfile, Group, Location]
MODELS = [UserProfile, Group]

content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
Expand Down
5 changes: 5 additions & 0 deletions hunchworks/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env python

from django.dispatch import Signal

user_invited = Signal(providing_args=["instance", "inviter", "invitee", "message"])
4 changes: 4 additions & 0 deletions hunchworks/templates/hunches/show.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ <h2>Invite Other Users and Groups</h2>
{{ invite_form.non_field_errors }}
{% csrf_token %}

{% for field in invite_form.hidden_fields %}
{{ field }}
{% endfor %}

<div class="fields">
{% for field in invite_form.visible_fields %}
{% include "includes/fields/base.html" %}
Expand Down
13 changes: 13 additions & 0 deletions hunchworks/templates/includes/events/user_invited_to_hunch.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<article class="event user-invited-to-hunch">
{% include "includes/users/short.html" with object=inviter %}

<div>
<p class="summary">
<a href="{% url profile inviter }}">{{ inviter }}</a> <span>invited you</span> to
<a href="{% url hunch hunch.pk %}" title="{{ hunch }}">a hunch</a>
<time class="timeago" datetime="{{ now|date:"c" }}">{{ now }}</time>
</p>

<h2><a href="{% url hunch hunch.pk %}">{{ hunch }}</a></h2>
</div>
</article>
9 changes: 6 additions & 3 deletions hunchworks/views/hunches.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@ def _auto_id():
invite_form = forms.InviteForm(req.POST)

if invite_form.is_valid():
# do something!
pass
invite_form.send_invites(
req.user.get_profile())
return redirect(hunch)


if hunch_evidence_form is None:
Expand All @@ -126,7 +127,9 @@ def _auto_id():
})

if invite_form is None:
invite_form = forms.InviteForm()
invite_form = forms.InviteForm(initial={
"hunch": hunch
})


def _wrap(hunch_evidence):
Expand Down

0 comments on commit 7b84ada

Please sign in to comment.