def unfollow(user, obj, send_action=False, request=None): """ Removes a "follow" relationship. Set ``send_action`` to ``True`` (``False is default) to also send a ``<user> stopped following <object>`` action signal. Example:: unfollow(request.user, other_user) """ from actstream.models import Follow, action check_actionable_model(obj) Follow.objects.filter( user=user, object_id=obj.pk, content_type=ContentType.objects.get_for_model(obj)).delete() if send_action: if request: from django.utils import simplejson as json from agora_site.misc.utils import geolocate_ip action.send(user, verb=_('stopped following'), target=obj, ipaddr=request.META.get('REMOTE_ADDR'), geolocation=json.dumps( geolocate_ip(request.META.get('REMOTE_ADDR')))) else: action.send(user, verb=_('stopped following'), target=obj)
def unfollow(user, obj, send_action=False, request=None): """ Removes a "follow" relationship. Set ``send_action`` to ``True`` (``False is default) to also send a ``<user> stopped following <object>`` action signal. Example:: unfollow(request.user, other_user) """ from actstream.models import Follow, action check_actionable_model(obj) Follow.objects.filter(user=user, object_id=obj.pk, content_type=ContentType.objects.get_for_model(obj)).delete() if send_action: if request: from django.utils import simplejson as json from agora_site.misc.utils import geolocate_ip action.send(user, verb=_('stopped following'), target=obj, ipaddr=request.META.get('REMOTE_ADDR'), geolocation=json.dumps(geolocate_ip(request.META.get('REMOTE_ADDR')))) else: action.send(user, verb=_('stopped following'), target=obj)
def save(self): obj = self.get_comment_object() obj.save() action.send(self.request.user, verb='commented', target=self.target_object, action_object=obj, ipaddr=self.request.META.get('REMOTE_ADDR'), geolocation=json.dumps(geolocate_ip(self.request.META.get('REMOTE_ADDR')))) return obj
def remove_membership_action(self, request, agora, username, goodbye_message, **kwargs): ''' Remove a member (specified with username) from this agora, sending a goodbye message to this member via email ''' if not re.match("^[a-zA-Z0-9_]+$", username): raise ImmediateHttpResponse(response=http.HttpBadRequest()) user = get_object_or_404(User, username=username) # user might not be allowed to leave (because he's the owner, or because # he's not a member of this agora at all) if not agora.has_perms('leave', user): raise ImmediateHttpResponse(response=http.HttpForbidden()) agora.members.remove(user) agora.save() # create an action for the event action.send(request.user, verb='removed member', action_object=agora, ipaddr=request.META.get('REMOTE_ADDR'), target=user, geolocation=json.dumps(geolocate_ip(request.META.get('REMOTE_ADDR')))) # Mail to the user if user.get_profile().has_perms('receive_email_updates'): translation.activate(user.get_profile().lang_code) context = get_base_email_context(request) context.update(dict( agora=agora, other_user=user, notification_text=_('Your have been removed from membership ' 'from %(agora)s . Sorry about that!\n\n') % dict( agora=agora.get_full_name() ) + goodbye_message, to=user )) email = EmailMultiAlternatives( subject=_('%(site)s - membership removed from ' '%(agora)s') % dict( site=Site.objects.get_current().domain, agora=agora.get_full_name() ), body=render_to_string('agora_core/emails/agora_notification.txt', context), to=[user.email]) email.attach_alternative( render_to_string('agora_core/emails/agora_notification.html', context), "text/html") email.send() translation.deactivate() return self.create_response(request, dict(status="success"))
def add_admin_membership_action(self, request, agora, username, welcome_message, **kwargs): ''' Adds an admin (specified with username) to this agora, sending a welcome message to this new admin via email ''' if not re.match("^[a-zA-Z0-9_]+$", username): raise ImmediateHttpResponse(response=http.HttpBadRequest()) user = get_object_or_404(User, username=username) # remve the request admin membership status and add user to the agora remove_perm('requested_admin_membership', user, agora) agora.admins.add(user) agora.save() # create an action for the event action.send(request.user, verb='added admin', action_object=agora, ipaddr=request.META.get('REMOTE_ADDR'), target=user, geolocation=json.dumps(geolocate_ip(request.META.get('REMOTE_ADDR')))) # Mail to the user if user.get_profile().has_perms('receive_email_updates'): translation.activate(user.get_profile().lang_code) context = get_base_email_context(request) context.update(dict( agora=agora, other_user=user, notification_text=_('As administrator of %(agora)s, %(user)s has ' 'himself promoted you to admin of this agora. You can remove ' 'your admin membership at anytime, and if you think he is ' 'spamming you please contact with this website ' 'administrators.\n\n') % dict( agora=agora.get_full_name(), user=request.user.username ) + welcome_message, to=user )) email = EmailMultiAlternatives( subject=_('%(site)s - Promoted to admin of agora %(agora)s') % dict( site=Site.objects.get_current().domain, agora=agora.get_full_name() ), body=render_to_string('agora_core/emails/agora_notification.txt', context), to=[user.email]) email.attach_alternative( render_to_string('agora_core/emails/agora_notification.html', context), "text/html") email.send() translation.deactivate() return self.create_response(request, dict(status="success"))
def deny_admin_membership_action(self, request, agora, username, **kwargs): ''' Deny an admin membership request from the given username user in an agora ''' if not re.match("^[a-zA-Z0-9_]+$", username): raise ImmediateHttpResponse(response=http.HttpBadRequest()) user = get_object_or_404(User, username=username) # One can only accept admin membership if it can cancel it, which means # if it's requested if not agora.has_perms('cancel_admin_membership_request', user): raise ImmediateHttpResponse(response=http.HttpForbidden()) # remove the request admin membership status and add user to the agora remove_perm('requested_admin_membership', user, agora) # create an action for the event action.send(request.user, verb='denied admin membership request', action_object=agora, ipaddr=request.META.get('REMOTE_ADDR'), target=user, geolocation=json.dumps(geolocate_ip(request.META.get('REMOTE_ADDR')))) # Mail to the user if user.get_profile().has_perms('receive_email_updates'): translation.activate(user.get_profile().lang_code) context = get_base_email_context(request) context.update(dict( agora=agora, other_user=user, notification_text=_('Your admin membership request at %(agora)s ' 'has been denied. Sorry about that!') % dict( agora=agora.get_full_name() ), to=user )) email = EmailMultiAlternatives( subject=_('%(site)s - admin membership request denied at ' '%(agora)s') % dict( site=Site.objects.get_current().domain, agora=agora.get_full_name() ), body=render_to_string('agora_core/emails/agora_notification.txt', context), to=[user.email]) email.attach_alternative( render_to_string('agora_core/emails/agora_notification.html', context), "text/html") email.send() translation.deactivate() return self.create_response(request, dict(status="success"))
def cancel_vote_action(self, request, election, **kwargs): ''' Cancels a vote ''' election_url = election.get_link() vote = election.get_vote_for_voter(request.user) if not vote or not vote.is_direct: data = dict(errors=_('You didn\'t participate in this election.')) return self.raise_error(request, http.HttpBadRequest, data) vote.invalidated_at_date = timezone.now() vote.is_counted = False vote.save() context = get_base_email_context(request) context.update( dict(election=election, election_url=election_url, to=vote.voter, agora_url=election.agora.get_link())) try: context['delegate'] = get_delegate_in_agora( vote.voter, election.agora) except: pass if request.user.get_profile().has_perms('receive_email_updates'): translation.activate(request.user.get_profile().lang_code) email = EmailMultiAlternatives( subject=_('Vote cancelled for election %s') % election.pretty_name, body=render_to_string('agora_core/emails/vote_cancelled.txt', context), to=[vote.voter.email]) email.attach_alternative( render_to_string('agora_core/emails/vote_cancelled.html', context), "text/html") email.send() translation.deactivate() action.send(request.user, verb='vote cancelled', action_object=election, target=election.agora, ipaddr=request.META.get('REMOTE_ADDR'), geolocation=json.dumps( geolocate_ip(request.META.get('REMOTE_ADDR')))) return self.create_response(request, dict(status="success"))
def cancel_vote_delegation(self, request, agora, **kwargs): ''' Cancel a delegated vote ''' # get the delegated vote, if any vote = agora.get_delegated_vote_for_user(request.user) if not vote: data = dict(errors=_('Your vote is not currently delegated.')) return self.raise_error(request, http.HttpBadRequest, data) # invalidate the vote vote.invalidated_at_date = timezone.now() vote.is_counted = False vote.save() # create an action for the event action.send(request.user, verb='cancelled vote delegation', action_object=agora, ipaddr=request.META.get('REMOTE_ADDR'), geolocation=json.dumps(geolocate_ip(request.META.get('REMOTE_ADDR')))) # Mail to the user if request.user.get_profile().has_perms('receive_email_updates'): translation.activate(request.user.get_profile().lang_code) context = get_base_email_context(request) context.update(dict( agora=agora, other_user=request.user, notification_text=_('Your hav removed your vote delegation ' 'from %(agora)s') % dict( agora=agora.get_full_name() ), to=request.user )) email = EmailMultiAlternatives( subject=_('%(site)s - admin permissions removed from ' '%(agora)s') % dict( site=Site.objects.get_current().domain, agora=agora.get_full_name() ), body=render_to_string('agora_core/emails/agora_notification.txt', context), to=[request.user.email]) email.attach_alternative( render_to_string('agora_core/emails/agora_notification.html', context), "text/html") email.send() translation.deactivate() return self.create_response(request, dict(status="success"))
def save(self): obj = self.get_comment_object() obj.save() action.send(self.request.user, verb='commented', target=self.target_object, action_object=obj, ipaddr=self.request.META.get('REMOTE_ADDR'), geolocation=json.dumps( geolocate_ip(self.request.META.get('REMOTE_ADDR')))) return obj
def freeze_action(self, request, election, **kwargs): ''' Requests membership from authenticated user to an agora ''' election.frozen_at_date = election.last_modified_at_date = timezone.now() election.save() transaction.commit() if election.is_secure(): election.request_pubkeys() action.send(request.user, verb='froze', action_object=election, target=election.agora, ipaddr=request.META.get('REMOTE_ADDR'), geolocation=json.dumps(geolocate_ip(request.META.get('REMOTE_ADDR')))) return self.create_response(request, dict(status="success"))
def approve_action(self, request, election, **kwargs): ''' Requests membership from authenticated user to an agora ''' election.is_approved = True election.save() action.send(request.user, verb='approved', action_object=election, target=election.agora, ipaddr=request.META.get('REMOTE_ADDR'), geolocation=json.dumps( geolocate_ip(request.META.get('REMOTE_ADDR')))) # Mail to the user if request.user.get_profile().has_perms('receive_email_updates'): translation.activate(request.user.get_profile().lang_code) context = get_base_email_context(request) context.update( dict(agora=election.agora, other_user=request.user, notification_text=_( 'Election %(election)s approved at agora ' '%(agora)s. Congratulations!') % dict(agora=election.agora.get_full_name(), election=election.pretty_name), to=request.user, extra_urls=[ dict(url_title=_("Election URL"), url=election.get_link()) ])) email = EmailMultiAlternatives( subject=_('%(site)s - Election %(election)s approved') % dict(site=Site.objects.get_current().domain, election=election.pretty_name), body=render_to_string( 'agora_core/emails/agora_notification.txt', context), to=[request.user.email]) email.attach_alternative( render_to_string('agora_core/emails/agora_notification.html', context), "text/html") email.send() translation.deactivate() return self.create_response(request, dict(status="success"))
def cancel_vote_action(self, request, election, **kwargs): ''' Cancels a vote ''' election_url=election.get_link() vote = election.get_vote_for_voter(request.user) if not vote or not vote.is_direct: data = dict(errors=_('You didn\'t participate in this election.')) return self.raise_error(request, http.HttpBadRequest, data) vote.invalidated_at_date = datetime.datetime.now() vote.is_counted = False vote.save() context = get_base_email_context(request) context.update(dict( election=election, election_url=election_url, to=vote.voter, agora_url=election.agora.get_link() )) try: context['delegate'] = get_delegate_in_agora(vote.voter, election.agora) except: pass if request.user.get_profile().has_perms('receive_email_updates'): translation.activate(request.user.get_profile().lang_code) email = EmailMultiAlternatives( subject=_('Vote cancelled for election %s') % election.pretty_name, body=render_to_string('agora_core/emails/vote_cancelled.txt', context), to=[vote.voter.email]) email.attach_alternative( render_to_string('agora_core/emails/vote_cancelled.html', context), "text/html") email.send() translation.deactivate() action.send(request.user, verb='vote cancelled', action_object=election, target=election.agora, ipaddr=request.META.get('REMOTE_ADDR'), geolocation=json.dumps(geolocate_ip(request.META.get('REMOTE_ADDR')))) return self.create_response(request, dict(status="success"))
def approve_action(self, request, election, **kwargs): ''' Requests membership from authenticated user to an agora ''' election.is_approved = True election.approved_at_date = election.last_modified_at_date = timezone.now() election.save() action.send(request.user, verb='approved', action_object=election, target=election.agora, ipaddr=request.META.get('REMOTE_ADDR'), geolocation=json.dumps(geolocate_ip(request.META.get('REMOTE_ADDR')))) # Mail to the user if request.user.get_profile().has_perms('receive_email_updates'): translation.activate(request.user.get_profile().lang_code) context = get_base_email_context(request) context.update(dict( agora=election.agora, other_user=request.user, notification_text=_('Election %(election)s approved at agora ' '%(agora)s. Congratulations!') % dict( agora=election.agora.get_full_name(), election=election.pretty_name ), to=request.user, extra_urls=[dict( url_title=_("Election URL"), url=election.get_link() )] )) email = EmailMultiAlternatives( subject=_('%(site)s - Election %(election)s approved') % dict( site=Site.objects.get_current().domain, election=election.pretty_name ), body=render_to_string('agora_core/emails/agora_notification.txt', context), to=[request.user.email]) email.attach_alternative( render_to_string('agora_core/emails/agora_notification.html', context), "text/html") email.send() translation.deactivate() return self.create_response(request, dict(status="success"))
def release_results_action(self, request, election, **kwargs): tkwargs=dict( election_id=election.id, is_secure=request.is_secure(), site_id=Site.objects.get_current().id, remote_addr=request.META.get('REMOTE_ADDR'), user_id=request.user.id ) election.tally_released_at_date = timezone.now() election.save() action.send(request.user, verb='published results', action_object=election, target=election.agora, ipaddr=request.META.get('REMOTE_ADDR'), geolocation=json.dumps(geolocate_ip(request.META.get('REMOTE_ADDR')))) return self.create_response(request, dict(status="success"))
def leave_action(self, request, agora, **kwargs): ''' Remove a member (specified with username) from this agora, sending a goodbye message to this member via email ''' agora.members.remove(request.user) agora.save() # create an action for the event action.send(request.user, verb='left', action_object=agora, ipaddr=request.META.get('REMOTE_ADDR'), geolocation=json.dumps(geolocate_ip(request.META.get('REMOTE_ADDR')))) # Mail to the user if request.user.get_profile().has_perms('receive_email_updates'): translation.activate(request.user.get_profile().lang_code) context = get_base_email_context(request) context.update(dict( agora=agora, other_user=request.user, notification_text=_('You have removed your membership ' 'from %(agora)s') % dict( agora=agora.get_full_name() ), to=request.user )) email = EmailMultiAlternatives( subject=_('%(site)s - membership removed from ' '%(agora)s') % dict( site=Site.objects.get_current().domain, agora=agora.get_full_name() ), body=render_to_string('agora_core/emails/agora_notification.txt', context), to=[request.user.email]) email.attach_alternative( render_to_string('agora_core/emails/agora_notification.html', context), "text/html") email.send() translation.deactivate() return self.create_response(request, dict(status="success"))
def join_action(self, request, agora, **kwargs): ''' Action that an user can execute to join an agora if it has permissions ''' agora.members.add(request.user) agora.save() action.send(request.user, verb='joined', action_object=agora, ipaddr=request.META.get('REMOTE_ADDR'), geolocation=json.dumps(geolocate_ip(request.META.get('REMOTE_ADDR')))) if not is_following(request.user, agora): follow(request.user, agora, actor_only=False, request=request) # Mail to the user user = request.user if user.get_profile().has_perms('receive_email_updates'): translation.activate(user.get_profile().lang_code) context = get_base_email_context(request) context.update(dict( agora=agora, other_user=user, notification_text=_('You just joined %(agora)s. ' 'Congratulations!') % dict(agora=agora.get_full_name()), to=user )) email = EmailMultiAlternatives( subject=_('%(site)s - you are now member of %(agora)s') % dict( site=Site.objects.get_current().domain, agora=agora.get_full_name() ), body=render_to_string('agora_core/emails/agora_notification.txt', context), to=[user.email]) email.attach_alternative( render_to_string('agora_core/emails/agora_notification.html', context), "text/html") email.send() translation.deactivate() return self.create_response(request, dict(status="success"))
def request_admin_membership_action(self, request, agora, **kwargs): ''' Requests membership from authenticated user to an agora ''' assign('requested_admin_membership', request.user, agora) action.send(request.user, verb='requested admin membership', action_object=agora, ipaddr=request.META.get('REMOTE_ADDR'), geolocation=json.dumps(geolocate_ip(request.META.get('REMOTE_ADDR')))) kwargs=dict( agora_id=agora.id, user_id=request.user.id, is_secure=request.is_secure(), site_id=Site.objects.get_current().id, remote_addr=request.META.get('REMOTE_ADDR') ) send_request_admin_membership_mails.apply_async(kwargs=kwargs) return self.create_response(request, dict(status="success"))
def follow(user, obj, send_action=True, actor_only=False, request=None): """ Creates a relationship allowing the object's activities to appear in the user's stream. Returns the created ``Follow`` instance. If ``send_action`` is ``True`` (the default) then a ``<user> started following <object>`` action signal is sent. If ``actor_only`` is ``True`` (the default) then only actions where the object is the actor will appear in the user's activity stream. Set to ``False`` to also include actions where this object is the action_object or the target. Example:: follow(request.user, group, actor_only=False) """ from actstream.models import Follow, action check_actionable_model(obj) follow, created = Follow.objects.get_or_create( user=user, object_id=obj.pk, content_type=ContentType.objects.get_for_model(obj), actor_only=actor_only) if send_action and created: if request: from django.utils import simplejson as json from agora_site.misc.utils import geolocate_ip action.send(user, verb=_('started following'), target=obj, ipaddr=request.META.get('REMOTE_ADDR'), geolocation=json.dumps( geolocate_ip(request.META.get('REMOTE_ADDR')))) else: action.send(user, verb=_('started following'), target=obj) return follow
def follow(user, obj, send_action=True, actor_only=False, request=None): """ Creates a relationship allowing the object's activities to appear in the user's stream. Returns the created ``Follow`` instance. If ``send_action`` is ``True`` (the default) then a ``<user> started following <object>`` action signal is sent. If ``actor_only`` is ``True`` (the default) then only actions where the object is the actor will appear in the user's activity stream. Set to ``False`` to also include actions where this object is the action_object or the target. Example:: follow(request.user, group, actor_only=False) """ from actstream.models import Follow, action check_actionable_model(obj) follow, created = Follow.objects.get_or_create(user=user, object_id=obj.pk, content_type=ContentType.objects.get_for_model(obj), actor_only=actor_only) if send_action and created: if request: from django.utils import simplejson as json from agora_site.misc.utils import geolocate_ip action.send(user, verb=_('started following'), target=obj, ipaddr=request.META.get('REMOTE_ADDR'), geolocation=json.dumps(geolocate_ip(request.META.get('REMOTE_ADDR')))) else: action.send(user, verb=_('started following'), target=obj) return follow
def save(self): """ Creates a new user and account. Returns the newly created user. """ username, password, first_name = (self.cleaned_data['username'], self.cleaned_data['password1'], self.cleaned_data['first_name']) user = self.userena_signup_obj.user self.userena_signup_obj.activation_key = userena_settings.USERENA_ACTIVATED user.is_active = True user.username = username user.set_password(password) user.first_name = first_name profile = user.get_profile() agora_id = profile.extra['join_agora_id'] profile.extra = {} user.save() profile.save() self.userena_signup_obj.save() from agora_site.agora_core.models.agora import Agora agora = get_object_or_404(Agora, pk=agora_id) agora.members.add(user) agora.save() action.send(user, verb='joined', action_object=agora, ipaddr=self.request.META.get('REMOTE_ADDR'), geolocation=json.dumps( geolocate_ip(self.request.META.get('REMOTE_ADDR')))) follow(user, agora, actor_only=False, request=self.request) # Mail to the user translation.activate(user.get_profile().lang_code) context = get_base_email_context(self.request) context.update( dict(agora=agora, other_user=user, notification_text=_('You just joined %(agora)s. ' 'Congratulations!') % dict(agora=agora.get_full_name()), to=user)) email = EmailMultiAlternatives( subject=_('%(site)s - you are now member of %(agora)s') % dict(site=Site.objects.get_current().domain, agora=agora.get_full_name()), body=render_to_string('agora_core/emails/agora_notification.txt', context), to=[user.email]) email.attach_alternative( render_to_string('agora_core/emails/agora_notification.html', context), "text/html") email.send() translation.deactivate() # Sign the user in. auth_user = authenticate(identification=user.email, check_password=False) login(self.request, auth_user) if userena_settings.USERENA_USE_MESSAGES: messages.success( self.request, _('Your account has been activated and you have been signed in.' ), fail_silently=True) return user
def save(self): """ Creates a new user and account. Returns the newly created user. """ username, password, first_name = (self.cleaned_data['username'], self.cleaned_data['password1'], self.cleaned_data['first_name']) user = self.userena_signup_obj.user self.userena_signup_obj.activation_key = userena_settings.USERENA_ACTIVATED user.is_active = True user.username = username user.set_password(password) user.first_name = first_name profile = user.get_profile() agora_id = profile.extra['join_agora_id'] profile.extra = {} user.save() profile.save() self.userena_signup_obj.save() from agora_site.agora_core.models.agora import Agora agora = get_object_or_404(Agora, pk=agora_id) agora.members.add(user) agora.save() action.send(user, verb='joined', action_object=agora, ipaddr=self.request.META.get('REMOTE_ADDR'), geolocation=json.dumps(geolocate_ip(self.request.META.get('REMOTE_ADDR')))) follow(user, agora, actor_only=False, request=self.request) # Mail to the user translation.activate(user.get_profile().lang_code) context = get_base_email_context(self.request) context.update(dict( agora=agora, other_user=user, notification_text=_('You just joined %(agora)s. ' 'Congratulations!') % dict(agora=agora.get_full_name()), to=user )) email = EmailMultiAlternatives( subject=_('%(site)s - you are now member of %(agora)s') % dict( site=Site.objects.get_current().domain, agora=agora.get_full_name() ), body=render_to_string('agora_core/emails/agora_notification.txt', context), to=[user.email]) email.attach_alternative( render_to_string('agora_core/emails/agora_notification.html', context), "text/html") email.send() translation.deactivate() # Sign the user in. auth_user = authenticate(identification=user.email, check_password=False) login(self.request, auth_user) if userena_settings.USERENA_USE_MESSAGES: messages.success(self.request, _('Your account has been activated and you have been signed in.'), fail_silently=True) return user