Exemple #1
0
def create_case_add_attachments(request, bean_case):
    try:
        files = request.FILES.getlist('update-case-attachment')
        if files:
            case_update = Bean('AOP_Case_Updates')
            case_update['contact_id'] = request.user.userattr.contact_id
            case_update['case_id'] = bean_case['id']
            case_update['name'] = bean_case['description'][:45]
            case_update['description'] = bean_case['description'].replace('\n', '<br>')
            case_update['internal'] = 0
            SuiteCRM().save_bean(case_update)
            if case_update['id']:
                for f in files:
                    note = Bean('Notes')
                    note['name'] = f.name
                    note['parent_type'] = 'AOP_Case_Updates'
                    note['parent_id'] = case_update['id']
                    note['contact_id'] = request.user.userattr.contact_id
                    SuiteCRM().save_bean(note)
                    if note['id']:
                        SuiteCRM().set_note_attachment(
                            note['id'],
                            f.name,
                            base64.b64encode(f.read())
                        )
                return True
    except Exception:
        pass
    return False
Exemple #2
0
def module_detail(request, module, id):
    context = basepage_processor(request)
    record = None
    ordered_module_fields = get_module_view_fields(module, 'detail')
    if user_can_read_module(request.user, module) and user_can_read_record(request.user, module, id):
        template = loader.get_template('portal/module_detail.html')
        try:
            if module == 'Cases':
                record = get_case(id)
                context.update({
                    'case_updates' : get_case_updates(id)
                })
            else:
                record = SuiteCRM().get_bean(module, id)
        except:
            pass
        context.update({
            'module_key' : module,
            'module_fields' : ordered_module_fields,
            'record' : record,
            'user_can_edit' : user_can_edit_module(request.user, module),
            'user_can_delete' : user_can_delete_module(request.user, module)
        })
    else:
        template = loader.get_template('portal/insufficient_permissions.html')
    return HttpResponse(template.render(context, request))
Exemple #3
0
def module_remove_record(request, module):
    if request.method == 'POST' and 'id' in request.POST:
        id = request.POST['id']
        if user_can_delete_module(request.user, module) \
                and user_is_linked_to_record(request.user, module, id):
            bean = Bean(module)
            bean['id'] = id
            bean['deleted'] = 1
            try:
                SuiteCRM().save_bean(bean)
                return JsonResponse({
                    "status" : "Success",
                    "msg" : _("Record deleted successfully.")
                })
            except Exception as e:
                return JsonResponse({
                    "status" : "Error",
                    "error" : _("Error deleting record.")
                }, status = 400)
        else:
            return JsonResponse({
                "status" : "Error",
                "error" : _("Insufficient permissions.")
            }, status = 400)
    return JsonResponse({
        "status" : "Error",
        "error" : _("Invalid request.")
    }, status = 400)
Exemple #4
0
def crm_entry_point(request):
    if request.GET['option'] and request.GET['task'] and request.GET['sug']:
        contact = None
        try:
            contact = SuiteCRM().get_bean(
                'Contacts',
                request.GET['sug'],
                ['id', 'first_name', 'last_name', 'email1', 'account_id']
            )
        except:
            return JsonResponse({
                "status" : "Error",
                "error" : _("Error retrieving contact")
            }, status = 400)
        if not contact['email1']:
            return JsonResponse({
                "status" : "Error",
                "error" : _("Contact has no valid email")
            }, status = 400)
        if request.GET['task'] == 'create':
            return create_portal_user(contact)
        elif request.GET['task'] == 'disable_user':
            return disable_portal_user(contact)
        elif request.GET['task'] == 'enable_user':
            return enable_portal_user(contact)
        print request.GET['task']
    return JsonResponse({
        "status" : "Error",
        "error" : _("Invalid request")
    }, status = 400)
Exemple #5
0
def create_portal_user(contact):
    username = contact['email1']
    password = User.objects.make_random_password()
    try:
        user = User.objects.get(username=username)
        return JsonResponse(
            {
                "status": "Error",
                "error": "An account with this email already exists"
            },
            status=400)
    except:
        pass
    user = User.objects.create_user(username=username,
                                    email=username,
                                    password=password,
                                    first_name=contact['first_name'],
                                    last_name=contact['last_name'])
    UserAttr(user=user,
             contact_id=contact['id'],
             account_id=contact['account_id']).save()
    default_role = get_default_role()
    if default_role:
        RoleUser(user=user, role=default_role).save()
    contact2 = Bean('Contacts')
    contact2['id'] = contact['id']
    contact2['joomla_account_id'] = user.id
    contact2['joomla_account_access'] = password
    SuiteCRM().save_bean(contact2)
    return JsonResponse({"success": True})
Exemple #6
0
def module_edit(request, module, id):
    context = basepage_processor(request)
    record = None
    ordered_module_fields = get_module_view_fields(module, 'edit')
    if user_can_edit_module(request.user, module) and user_is_linked_to_record(request.user, module, id):
        template = loader.get_template('portal/module_edit.html')
        if request.method == 'POST':
            try:
                bean = get_bean_from_post(module, 'edit', request.POST)
                bean['id'] = id
                try:
                    module_def = ModuleDefinitionFactory.get_module_definition(module)
                    module_def.before_save_on_edit_hook(bean, request)
                except Exception:
                    pass
                SuiteCRM().save_bean(bean)
                context.update({
                    'record_edited' : True
                })
                url = reverse(
                    'module_detail',
                    kwargs={
                        'module': module,
                        'id': id
                    }
                )
                return HttpResponseRedirect(url)
            except:
                context.update({
                    'error_on_save' : True
                })
        try:
            record = SuiteCRM().get_bean(module, id)
        except Exception:
            context.update({
                'error_retrieving_bean' : True
            })
        context.update({
            'module_key' : module,
            'module_fields' : ordered_module_fields,
            'record' : record
        })
    else:
        template = loader.get_template('portal/insufficient_permissions.html')
    return HttpResponse(template.render(context, request))
Exemple #7
0
def get_aos_quotes_record(module, id):
    return SuiteCRM().get_bean(module,
                               id,
                               link_name_to_fields_array=[{
                                   'name':
                                   'aos_products_quotes',
                                   'value':
                                   AOS_PRODUCTS_QUOTES_FIELDS
                               }])
Exemple #8
0
def note_attachment(request, id):
    attachment = SuiteCRM().get_note_attachment(id)['note_attachment']
    if attachment['file']:
        response = HttpResponse(base64.b64decode(attachment['file']),
                                content_type='application/octet-stream')
        response['Content-Disposition'] = "attachment; filename=%s" \
                % attachment['filename']
        return response
    else:
        raise Http404(_("The requested file was not found."))
Exemple #9
0
def user_is_linked_to_record(user, module, id):
    try:
        module_def = ModuleDefinitionFactory.get_module_definition(module)
        user_type = user.userattr.user_type
        if (user_type == 'account' \
                and module_def.accounts_link_type != LinkType.CONTACT) \
                or module_def.contacts_link_type == LinkType.ACCOUNT:
            related_module = 'Accounts'
            related_id = user.userattr.account_id
            link_type = module_def.accounts_link_type
            link_name = module_def.accounts_link_name
        else:
            related_module = 'Contacts'
            related_id = user.userattr.contact_id
            link_type = module_def.contacts_link_type
            link_name = module_def.contacts_link_name

        if link_type == LinkType.RELATED:
            filter_query = module.lower() + '.id = \'' + id + '\' AND '
            filter_query += get_filter_related(module, link_name, related_id)
            records = SuiteCRM().get_bean_list(module,
                                               max_results=1,
                                               query=filter_query)
        elif link_type == LinkType.RELATIONSHIP:
            records = SuiteCRM().get_relationships(
                related_module,
                related_id,
                link_name,
                related_fields=['id'],
                limit=1,
                related_module_query=module.lower() + '.id = \'' + id + '\'')
        elif module_def.contacts_link_type == LinkType.PARENT:
            filter_query = module.lower() + '.id = \'' + id + '\' AND '
            filter_query += get_filter_parent(module, related_module,
                                              related_id)
            records = SuiteCRM().get_bean_list(module,
                                               max_results=1,
                                               query=filter_query)
        if records['entry_list'][0]['id'] == id:
            return True
    except:
        pass
    return False
Exemple #10
0
def get_case(case_id):
    return SuiteCRM().get_bean('Cases',
                               case_id,
                               link_name_to_fields_array=[{
                                   'name': 'notes',
                                   'value': NOTE_FIELDS
                               }, {
                                   'name':
                                   'contacts',
                                   'value':
                                   CONTACT_FIELDS
                               }])
Exemple #11
0
def relate_bean_with_account(bean, account_id):
    try:
        module = bean.module
        if not account_id or not module:
            return False
        module_def = ModuleDefinitionFactory.get_module_definition(module)
        if module_def.accounts_link_type == LinkType.RELATED:
            bean[module_def.accounts_link_name] = account_id
            SuiteCRM().save_bean(bean)
        elif module_def.accounts_link_type == LinkType.RELATIONSHIP:
            result = SuiteCRM().set_relationship('Accounts',
                                                 account_id,
                                                 module_def.accounts_link_name,
                                                 related_ids=[bean['id']])
            if result['created'] != 1:
                return False
        elif module_def.accounts_link_type == LinkType.PARENT:
            bean['parent_type'] = 'Accounts'
            bean['parent_id'] = account_id
            SuiteCRM().save_bean(bean)
    except:
        return False
    return True
Exemple #12
0
def get_case_update(case_update_id):
    return SuiteCRM().get_bean('AOP_Case_Updates',
                               case_update_id,
                               link_name_to_fields_array=[{
                                   'name': 'notes',
                                   'value': NOTE_FIELDS
                               }, {
                                   'name': 'assigned_user_link',
                                   'value': USER_FIELDS
                               }, {
                                   'name':
                                   'contact',
                                   'value':
                                   CONTACT_FIELDS
                               }])
Exemple #13
0
def get_case_updates(case_id):
    return SuiteCRM().get_relationships(
        'Cases',
        case_id,
        'aop_case_updates',
        related_fields=CASE_UPDATE_FIELDS,
        related_module_link_name_to_fields_array=[{
            'name': 'notes',
            'value': NOTE_FIELDS
        }, {
            'name': 'assigned_user_link',
            'value': USER_FIELDS
        }, {
            'name': 'contact',
            'value': CONTACT_FIELDS
        }],
        order_by='date_entered',
        related_module_query='aop_case_updates.internal = 0')
Exemple #14
0
def module_create(request, module):
    context = basepage_processor(request)
    ordered_module_fields = get_module_view_fields(module, 'create')
    if user_can_create_module(request.user, module):
        template = loader.get_template('portal/module_create.html')
        if request.method == 'POST':
            try:
                bean = get_bean_from_post(module, 'create', request.POST)
                try:
                    module_def = ModuleDefinitionFactory.get_module_definition(module)
                    module_def.before_save_on_create_hook(bean, request)
                except Exception:
                    pass
                SuiteCRM().save_bean(bean)
                relate_result = relate_bean_with_user(bean, request.user)
                context.update(relate_result)
                context.update({
                    'record_created_successfully' : True
                })
                if module == 'Cases':
                    create_case_add_attachments(request, bean)
                url = reverse(
                    'module_detail',
                    kwargs={
                        'module': module,
                        'id': bean['id']
                    }
                )
                return HttpResponseRedirect(url)
            except Exception as e:
                print e
                context.update({
                    'error_on_create' : True
                })
        context.update({
            'module_key' : module,
            'module_fields' : ordered_module_fields
        })
    else:
        template = loader.get_template('portal/insufficient_permissions.html')
    return HttpResponse(template.render(context, request))
Exemple #15
0
def reopen_case(request):
    if user_can_read_module(request.user, 'Cases') and request.method == 'POST' \
            and 'case-id' in request.POST \
            and user_is_linked_to_case(request.user, request.POST['case-id']):
        try:
            bean = Bean('Cases')
            bean['id'] = request.POST['case-id']
            bean['state'] = 'Open'
            bean['status'] = 'Open_New'
            SuiteCRM().save_bean(bean)
        except Exception:
            pass
        url = reverse('module_detail',
                      kwargs={
                          'module': 'Cases',
                          'id': request.POST['case-id']
                      })
        return HttpResponseRedirect(url)
    else:
        context = basepage_processor(request)
        template = loader.get_template('portal/insufficient_permissions.html')
        return HttpResponse(template.render(context, request))
Exemple #16
0
def get_pdf(request, module, id):
    context = basepage_processor(request)
    template_id = get_pdf_template_id(module)
    if user_can_read_module(request.user, module) \
            and user_can_read_record(request.user, module, id) \
            and template_id:
        try:
            pdf = SuiteCRM().get_pdf_template(template_id, module, id)
            if pdf['error']:
                raise Http404(_("The requested file was not found."))
            else:
                response = HttpResponse(
                    base64.b64decode(pdf['file']),
                    content_type='application/octet-stream')
                response['Content-Disposition'] = "attachment; filename=%s" \
                        % pdf['filename']
                return response
        except Exception:
            context.update({'msg': _('Error while retrieving document.')})
            template = loader.get_template('portal/error.html')
            return HttpResponse(template.render(context, request))
    else:
        template = loader.get_template('portal/insufficient_permissions.html')
        return HttpResponse(template.render(context, request))
Exemple #17
0
def get_aos_contracts_pdf_templates():
    return SuiteCRM().get_bean_list('AOS_PDF_Templates',
                                    "aos_pdf_templates.type = 'AOS_Contracts'",
                                    select_fields=AOS_PDF_TEMPLATES_FIELDS)
Exemple #18
0
def add_case_update(request):
    if user_can_read_module(request.user, 'Cases') and request.method == 'POST':
        if request.method == 'POST' and 'case-id' in request.POST \
                and 'update-case-text' in request.POST \
                and user_is_linked_to_case(request.user, request.POST['case-id']):
            update_case_text = request.POST['update-case-text'].strip()
            if not update_case_text:
                return JsonResponse({
                    "status" : "Error",
                    "error" : _("Empty case updates are not allowed.")
                }, status = 400)
            case_update = Bean('AOP_Case_Updates')
            case_update['contact_id'] = request.user.userattr.contact_id
            case_update['case_id'] = request.POST['case-id']
            case_update['name'] = update_case_text[:45]
            case_update['description'] = update_case_text.replace('\n', '<br>')
            case_update['internal'] = 0
            try:
                SuiteCRM().save_bean(case_update)
                if case_update['id']:
                    for f in request.FILES.getlist('update-case-attachment'):
                        note = Bean('Notes')
                        note['name'] = f.name
                        note['parent_type'] = 'AOP_Case_Updates'
                        note['parent_id'] = case_update['id']
                        note['contact_id'] = request.user.userattr.contact_id
                        SuiteCRM().save_bean(note)
                        if note['id']:
                            SuiteCRM().set_note_attachment(
                                note['id'],
                                f.name,
                                base64.b64encode(f.read())
                            )
                        else:
                            return JsonResponse({
                                "status" : "Error",
                                "error" : _("An error occurred while uploading the attachment(s).")
                            }, status = 400)
                else:
                    return JsonResponse({
                        "status" : "Error",
                        "error" : _("An error occurred while creating the case update.")
                    }, status = 400)
                return JsonResponse({
                    "status" : "Success",
                    "msg" : _("The case update has been added successfully."),
                    "case_update" : render_to_string(
                        'portal/module_detail_case_update.html',
                        {
                            "update" : get_case_update(case_update['id']),
                            "show" : True
                        }
                    )
                })
            except:
                return JsonResponse({
                    "status" : "Error",
                    "error" : _("An error occurred while updating the case.")
                }, status = 400)
    return JsonResponse({
        "status" : "Error",
        "error" : _("Invalid request.")
    }, status = 400)
Exemple #19
0
def retrieve_list_view_records(module, arguments, user):
    try:
        module_def = ModuleDefinitionFactory.get_module_definition(module)
    except ModuleDefinitionNotFoundException:
        return {'module_key': module, 'unsupported_module': True}
    try:
        user_type = user.userattr.user_type
        if (user_type == 'account' \
                and module_def.accounts_link_type != LinkType.CONTACT) \
                or module_def.contacts_link_type == LinkType.ACCOUNT:
            related_module = 'Accounts'
            related_id = user.userattr.account_id
            link_type = module_def.accounts_link_type
            link_name = module_def.accounts_link_name
        else:
            related_module = 'Contacts'
            related_id = user.userattr.contact_id
            link_type = module_def.contacts_link_type
            link_name = module_def.contacts_link_name
    except:
        return {'module_key': module, 'error_retrieving_records': True}
    records = []
    module_fields = {}
    ordered_module_fields = OrderedDict()
    filterable_fields = get_filter_layout(module)
    limit = arguments.get('limit')
    if limit:
        limit = int(limit)
    else:
        limit = 20
    offset = arguments.get('offset')
    if offset:
        offset = int(offset)
    order_by = arguments.get('order_by')
    order = arguments.get('order')
    try:
        view = Layout.objects.get(module=module, view='list')
        fields_list = json.loads(view.fields)
        module_fields = SuiteCRMCached().get_module_fields(
            module, fields_list)['module_fields']
        for field in fields_list:
            if field in module_fields:
                ordered_module_fields[field] = module_fields[field]
        remove_colon_of_field_labels(module_fields)
        set_sortable_atribute_on_module_fields(module_fields)
        order_by_string = None
        if order_by in fields_list and module_fields[order_by]['sortable']:
            order_by_string = order_by
        else:
            if not order_by:
                order_by = module_def.default_order_by_field
                order_by_string = order_by
                order = module_def.default_order
            else:
                order_by = None
        if order_by and order in ['asc', 'desc']:
            order_by_string += ' ' + order
        else:
            order = None
        filter_query = get_filter_query(module, filterable_fields, arguments)
        if link_type == LinkType.RELATED:
            if filter_query:
                filter_query += " AND "
            filter_query += get_filter_related(module, link_name, related_id)
            if module_def.custom_where:
                if filter_query:
                    filter_query += " AND "
                filter_query += module_def.custom_where
            records = SuiteCRM().get_bean_list(module,
                                               max_results=limit,
                                               offset=offset,
                                               order_by=order_by_string,
                                               query=filter_query)
        elif link_type == LinkType.RELATIONSHIP:
            if module_def.custom_where:
                if filter_query:
                    filter_query += " AND "
                filter_query += module_def.custom_where
            records = SuiteCRM().get_relationships(
                related_module,
                related_id,
                link_name,
                related_fields=['id'] + fields_list,
                limit=limit,
                offset=offset,
                order_by=order_by_string,
                related_module_query=filter_query)
        elif link_type == LinkType.PARENT:
            if filter_query:
                filter_query += " AND "
            filter_query += get_filter_parent(module, related_module,
                                              related_id)
            if module_def.custom_where:
                if filter_query:
                    filter_query += " AND "
                filter_query += module_def.custom_where
            records = SuiteCRM().get_bean_list(module,
                                               max_results=limit,
                                               offset=offset,
                                               order_by=order_by_string,
                                               query=filter_query)
        elif link_type == LinkType.NONE:
            if module_def.custom_where:
                if filter_query:
                    filter_query += " AND "
                filter_query += module_def.custom_where
            records = SuiteCRM().get_bean_list(module,
                                               max_results=limit,
                                               offset=offset,
                                               order_by=order_by_string,
                                               query=filter_query)
    except Exception as e:
        print e

    return {
        'module_key': module,
        'records': records,
        'module_fields': ordered_module_fields,
        'filterable_fields': filterable_fields,
        'current_filters': get_listview_filter(arguments),
        'order_by': order_by,
        'order': order
    }
Exemple #20
0
def get_related_user_records(module, user):
    records = None
    try:
        module_def = ModuleDefinitionFactory.get_module_definition(module)
        user_type = user.userattr.user_type
        if (user_type == 'account' \
                and module_def.accounts_link_type != LinkType.CONTACT) \
                or module_def.contacts_link_type == LinkType.ACCOUNT:
            related_module = 'Accounts'
            related_id = user.userattr.account_id
            link_type = module_def.accounts_link_type
            link_name = module_def.accounts_link_name
        else:
            related_module = 'Contacts'
            related_id = user.userattr.contact_id
            link_type = module_def.contacts_link_type
            link_name = module_def.contacts_link_name

        fields_list = ['id', 'name']
        order_by = 'name'
        if link_type == LinkType.RELATED:
            filter_query = get_filter_related(module, link_name, related_id)
            if module_def.custom_where:
                if filter_query:
                    filter_query += " AND "
                filter_query += module_def.custom_where
            if module_def.custom_dropdown_where:
                if filter_query:
                    filter_query += " AND "
                filter_query += module_def.custom_dropdown_where
            records = SuiteCRM().get_bean_list(module,
                                               order_by=order_by,
                                               select_fields=fields_list,
                                               query=filter_query)
        elif link_type == LinkType.RELATIONSHIP:
            filter_query = ''
            if module_def.custom_where:
                filter_query = module_def.custom_where
            if module_def.custom_dropdown_where:
                if filter_query:
                    filter_query += " AND "
                filter_query += module_def.custom_dropdown_where
            records = SuiteCRM().get_relationships(
                related_module,
                related_id,
                link_name,
                related_fields=fields_list,
                order_by=order_by,
                related_module_query=filter_query)
        elif link_type == LinkType.PARENT:
            filter_query = get_filter_parent(module, related_module,
                                             related_id)
            if module_def.custom_where:
                if filter_query:
                    filter_query += " AND "
                filter_query += module_def.custom_where
            if module_def.custom_dropdown_where:
                if filter_query:
                    filter_query += " AND "
                filter_query += module_def.custom_dropdown_where
            records = SuiteCRM().get_bean_list(module,
                                               order_by=order_by,
                                               query=filter_query,
                                               select_fields=fields_list)
        elif link_type == LinkType.NONE:
            filter_query = ''
            if module_def.custom_where:
                filter_query = module_def.custom_where
            if module_def.custom_dropdown_where:
                if filter_query:
                    filter_query += " AND "
                filter_query += module_def.custom_dropdown_where
            records = SuiteCRM().get_bean_list(module,
                                               order_by=order_by,
                                               query=filter_query,
                                               select_fields=fields_list)
    except:
        return None
    return records