示例#1
0
def add_entity_to_group(request, entity_id):
    """view"""
    entity = get_object_or_404(models.Entity, id=entity_id)

    if request.method == "POST":
        form = forms.AddEntityToGroupForm(entity, request.POST)
        if form.is_valid():
            name = form.cleaned_data["group_name"]
            if can_create_group(request.user):
                group = models.Group.objects.get_or_create(name=name)[0]
            else:
                group = get_object_or_404(models.Group, name=name)
            group.entities.add(entity)
            group.save()
            next_url = reverse('crm_view_entity', args=[entity_id])
            return HttpResponseRedirect(next_url)
    else:
        form = forms.AddEntityToGroupForm(entity)

    context_dict = {
        'entity': entity,
        'form': form,
        'request': request,
    }

    return render(request, 'Crm/add_entity_to_group.html', context_dict)
示例#2
0
def add_contact_to_group(request, contact_id):
    """view"""
    try:
        contact = get_object_or_404(models.Contact, id=contact_id)

        if request.method == "POST":
            form = forms.AddContactToGroupForm(contact, request.POST)
            if form.is_valid():
                name = form.cleaned_data["group_name"]
                if can_create_group(request.user):
                    group = models.Group.objects.get_or_create(name=name)[0]
                else:
                    group = get_object_or_404(models.Group, name=name)
                if contact not in group.contacts.all():
                    group.contacts.add(contact)
                    group.save()
                next_url = reverse('crm_view_contact', args=[contact_id])
                return HttpResponseRedirect(next_url)
        else:
            form = forms.AddContactToGroupForm(contact)

        context_dict = {
            'contact': contact,
            'form': form,
        }

        return render(request, 'Crm/add_contact_to_group.html', context_dict)

    # pylint: disable=broad-except
    except Exception as msg:
        print("#ERR", msg)
        raise
示例#3
0
 def clean_group_name(self):
     """validation"""
     name = self.cleaned_data['group_name']
     request = RequestManager().get_request()
     if not can_create_group(request.user) and models.Group.objects.filter(
             name=name).count() == 0:
         raise ValidationError(
             ugettext("You are not allowed to create new groups"))
     if models.Group.objects.filter(
             name=name, contacts__id=self.contact.id).count() > 0:
         raise ValidationError(
             ugettext("The contact already belong to group {0}").format(
                 name))
     return name