Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Refactoring

Luan Fonseca de Farias
September 09, 2015
37

 Refactoring

Luan Fonseca de Farias

September 09, 2015
Tweet

Transcript

  1. • +function makeMessage(message, type, id) { + return '<div class="alert

    alert-"' + type + '" ' + + 'alert-dismissable text-left" id="' + id + '">' + + '<i class="icon-exclamation-sign"></i>' + + data.message + + '<button type="button" class="close" ' + + 'data-dismiss="alert" ' + + 'aria-hidden="true"><C3>x</button>' + + '</div>'; +}
  2. • self.removeAttr('href'); - var successAlert = '<div class="alert alert-success alert-dismissable

    - '<i class="icon-exclamation-sign"></i>' + - data.message + - '<button type="button" class="close" - '</div>'; + var successAlert = makeMessage( + data.message, 'success', 'success-vote-alert'); self.parent('.proposal-votes').append(successAlert);
  3. • self.removeAttr('href'); - var errorAlert = '<div class="alert alert-error alert-dismissable

    - '<i class="icon-exclamation-sign"></i>' + - data.message + - '<button type="button" class="close" - '</div>'; + var errorAlert = makeMessage( + data.message, 'error', 'error-vote-alert'); self.parent('.proposal-votes').append(errorAlert);
  4. •+class DispatchRedirectsMixing(object): + def get_url(self): + object = self.get_object() +

    return reverse('view_event', kwargs={'slug': object.event.slug}) . . .
  5. • . . . + def not_authenticated_redirect(self): + message =

    _(u'You need to be logged in to continue to the next step.') + + if self.request.method == 'GET': + messages.error(self.request, message) + return HttpResponseRedirect(self.get_url()) + + response = {} + response['message'] = message + response['redirectUrl'] = u'{}?{}={}'.format( + settings.LOGIN_URL, + REDIRECT_FIELD_NAME, + self.request.META.get('PATH_INFO') + ) + return HttpResponse( + json.dumps(response), + status=401, + content_type='application/json') . . .
  6. • . . . + def not_allowed_redirect(self, url): + message

    = _(u'You are not allowed to see this page.') + if self.request.method == 'GET': + messages.error(self.request, message) + return HttpResponseRedirect(url) + response = {} + response['message'] = message + response['redirectUrl'] = '' + return HttpResponse( + json.dumps(response), + status=401, + content_type='application/json' + )
  7. • -class RateProposal(BaseProposalView, UpdateView): +class RateProposal(BaseProposalView, UpdateView, DispatchRedirectsMixing): def post(self,

    request, *args, **kwargs): self.object = self.get_object() rate = kwargs.get('rate') @@ -291,39 +326,13 @@ class RateProposal(BaseProposalView, UpdateView): 'view_event', kwargs={'slug': proposal.event.slug}) if not self.request.user.is_authenticated(): - message = _(u'You need to be logged in to ' . . . - json.dumps(response), - status=401, - content_type='application/json' - ) + return self.not_authenticated_redirect(url=view_event_url) + elif not proposal.user_can_approve(self.request.user): + return self.not_allowed_redirect(url=view_event_url) return super(RateProposal, self).dispatch(*args, **kwargs)
  8. • -class ApproveProposal(BaseProposalView, UpdateView): +class ApproveProposal(BaseProposalView, UpdateView, DispatchRedirectsMixing): def post(self,

    request, *args, **kwargs): self.object = self.get_object() response_content = {} @@ -358,39 +367,15 @@ class ApproveProposal(BaseProposalView, UpdateView): 'view_event', kwargs={'slug': proposal.event.slug}) if not self.request.user.is_authenticated(): . . . - content_type='application/json') + return self.not_authenticated_redirect(url=view_event_url) elif not proposal.user_can_approve(self.request.user): - message = _(u'You are not allowed to see this page.') . . . - ) + return self.not_allowed_redirect(url=view_event_url) return super(ApproveProposal, self).dispatch(*args, **kwargs)
  9. • -class DisapproveProposal(BaseProposalView, UpdateView): +class DisapproveProposal(BaseProposalView, + UpdateView, + DispatchRedirectsMixing):

    def post(self, request, *args, **kwargs): self.object = self.get_object() response_content = {} @@ -425,33 +410,7 @@ class DisapproveProposal(BaseProposalView, UpdateView): 'view_event', kwargs={'slug': proposal.event.slug}) if not self.request.user.is_authenticated(): - message = _(u'You need to be logged in to ' . . . - ) + return self.not_authenticated_redirect(url=view_event_url) + elif not proposal.user_can_vote(self.request.user): + return self.not_allowed_redirect(url=view_event_url) return super(DisapproveProposal, self).dispatch(*args, **kwargs)
  10. • +def get_proposals(self): + proposals = self.object.proposals.cached_authors() + if self.object.user_can_see_proposals(self.request.user):

    + if not self.request.user.is_anonymous(): + proposals = proposals.order_by_never_voted( + user_id=self.request.user.id) + elif not self.request.user.is_anonymous(): + proposals = proposals.filter(author=self.request.user) + else: + proposals = proposals.none() + return proposals
  11. • context['vote_rates'] = Vote.VOTE_RATES - event_proposals = self.object.proposals.cached_authors() - if

    self.object.user_can_see_proposals(self.request.user): - if not self.request.user.is_anonymous(): - event_proposals = event_proposals.order_by_never_voted( - user_id=self.request.user.id) - elif not self.request.user.is_anonymous(): - event_proposals = event_proposals.filter(author=self.request.user) - else: - event_proposals = event_proposals.none() - context.update(event_proposals=event_proposals) + context.update(event_proposals=self.get_proposals()) return context
  12. • +def change_approved_status(self, is_approved): + if is_approved and self.is_approved: +

    raise ValidationError(_("This Proposal was already approved.")) + elif not is_approved and not self.is_approved: + raise ValidationError(_("This Proposal was already disapproved.")) + + self.is_approved = is_approved + self.save()
  13. • - def approve(self): - if self.is_approved: - raise ValidationError(_("This

    Proposal was already approved.")) - self.is_approved = True - self.save() - - def disapprove(self): - if not self.is_approved: - raise ValidationError(_("This Proposal was already disapproved.")) - self.is_approved = False - self.save()
  14. • try: - self.object.approve() + self.object.change_approved_status(is_approved=True) except (IntegrityError, ValidationError), e:

    response_content['message'] = e.message response_status = 400 @@ -345,7 +345,7 @@ class ApproveProposal(BaseProposalView, UpdateView): self.object = self.get_object() try: - self.object.approve() + self.object.change_approved_status(is_approved=True) except (IntegrityError, ValidationError), e: