<commit_before>try:
    from django.urls import reverse
except ImportError:
    from django.core.urlresolvers import reverse
from tastypie import authorization
from tastypie.authentication import MultiAuthentication

from crits.emails.email import Email
from crits.emails.handlers import handle_pasted_eml, handle_yaml, handle_eml
from crits.emails.handlers import handle_email_fields, handle_msg
from crits.core.api import CRITsApiKeyAuthentication, CRITsSessionAuthentication
from crits.core.api import CRITsSerializer, CRITsAPIResource

from crits.vocabulary.acls import EmailACL


class EmailResource(CRITsAPIResource):
    """
    Class to handle everything related to the Email API.

    Currently supports GET and POST.
    """

    class Meta:
        object_class = Email
        allowed_methods = ('get', 'post', 'patch')
        resource_name = "emails"
        authentication = MultiAuthentication(CRITsApiKeyAuthentication(),
                                             CRITsSessionAuthentication())
        authorization = authorization.Authorization()
        serializer = CRITsSerializer()

    def get_object_list(self, request):
        """
        Use the CRITsAPIResource to get our objects but provide the class to get
        the objects from.

        :param request: The incoming request.
        :type request: :class:`django.http.HttpRequest`
        :returns: Resulting objects in the specified format (JSON by default).
        """

        return super(EmailResource, self).get_object_list(request, Email)

    def obj_create(self, bundle, **kwargs):
        """
        Handles creating Emails through the API.

        :param bundle: Bundle containing the information to create the Campaign.
        :type bundle: Tastypie Bundle object.
        :returns: HttpResponse.
        """

        user = bundle.request.user
        type_ = bundle.data.get('upload_type', None)

        content = {'return_code': 1,
                   'type': 'Email',
                   'message': ''}

        if not type_:
            content['message'] = 'You must specify the upload type.'
            self.crits_response(content)
        elif type_ not in ('eml', 'msg', 'raw', 'yaml', 'fields'):
            content['message'] = 'Unknown or unsupported upload type.'
            self.crits_response(content)

        # Remove this so it doesn't get included with the fields upload
        del bundle.data['upload_type']
        result = None

        # Extract common information
        source = bundle.data.get('source_name', None)
        method = bundle.data.get('source_method', '')
        reference = bundle.data.get('source_reference', None)
        tlp = bundle.data.get('source_tlp', 'amber')
        campaign = bundle.data.get('campaign', None)
        confidence = bundle.data.get('confidence', None)
        bucket_list = bundle.data.get('bucket_list', None)
        ticket = bundle.data.get('ticket', None)

        if method:
            method = " - " + method

        if not user.has_access_to(EmailACL.WRITE):
            content['success'] = False
            content['message'] = 'User does not have permission to create Object.'

            self.crits_response(content)

        if type_ == 'eml':
            file_ = bundle.data.get('filedata', None)
            if not file_:
                content['message'] = 'No file uploaded.'
                self.crits_response(content)
            filedata = file_.read()
            result = handle_eml(filedata, source, reference,
                                user, 'EML Upload' + method, tlp=tlp, campaign=campaign,
                                confidence=confidence, bucket_list=bucket_list, ticket=ticket)
        if type_ == 'msg':
            raw_email = bundle.data.get('filedata', None)
            password = bundle.data.get('password', None)

            result = handle_msg(raw_email,
                                source,
                                reference,
                                'Outlook MSG Upload' + method,
                                tlp,
                                user,
                                password,
                                campaign,
                                confidence,
                                bucket_list=bucket_list,
                                ticket=ticket)
        if type_ == 'raw':<commit_msg>Parameter fix for "handle_eml" call in emails/api<commit_after>try:
    from django.urls import reverse
except ImportError:
    from django.core.urlresolvers import reverse
from tastypie import authorization
from tastypie.authentication import MultiAuthentication

from crits.emails.email import Email
from crits.emails.handlers import handle_pasted_eml, handle_yaml, handle_eml
from crits.emails.handlers import handle_email_fields, handle_msg
from crits.core.api import CRITsApiKeyAuthentication, CRITsSessionAuthentication
from crits.core.api import CRITsSerializer, CRITsAPIResource

from crits.vocabulary.acls import EmailACL


class EmailResource(CRITsAPIResource):
    """
    Class to handle everything related to the Email API.

    Currently supports GET and POST.
    """

    class Meta:
        object_class = Email
        allowed_methods = ('get', 'post', 'patch')
        resource_name = "emails"
        authentication = MultiAuthentication(CRITsApiKeyAuthentication(),
                                             CRITsSessionAuthentication())
        authorization = authorization.Authorization()
        serializer = CRITsSerializer()

    def get_object_list(self, request):
        """
        Use the CRITsAPIResource to get our objects but provide the class to get
        the objects from.

        :param request: The incoming request.
        :type request: :class:`django.http.HttpRequest`
        :returns: Resulting objects in the specified format (JSON by default).
        """

        return super(EmailResource, self).get_object_list(request, Email)

    def obj_create(self, bundle, **kwargs):
        """
        Handles creating Emails through the API.

        :param bundle: Bundle containing the information to create the Campaign.
        :type bundle: Tastypie Bundle object.
        :returns: HttpResponse.
        """

        user = bundle.request.user
        type_ = bundle.data.get('upload_type', None)

        content = {'return_code': 1,
                   'type': 'Email',
                   'message': ''}

        if not type_:
            content['message'] = 'You must specify the upload type.'
            self.crits_response(content)
        elif type_ not in ('eml', 'msg', 'raw', 'yaml', 'fields'):
            content['message'] = 'Unknown or unsupported upload type.'
            self.crits_response(content)

        # Remove this so it doesn't get included with the fields upload
        del bundle.data['upload_type']
        result = None

        # Extract common information
        source = bundle.data.get('source_name', None)
        method = bundle.data.get('source_method', '')
        reference = bundle.data.get('source_reference', None)
        tlp = bundle.data.get('source_tlp', 'amber')
        campaign = bundle.data.get('campaign', None)
        confidence = bundle.data.get('confidence', None)
        bucket_list = bundle.data.get('bucket_list', None)
        ticket = bundle.data.get('ticket', None)

        if method:
            method = " - " + method

        if not user.has_access_to(EmailACL.WRITE):
            content['success'] = False
            content['message'] = 'User does not have permission to create Object.'

            self.crits_response(content)

        if type_ == 'eml':
            file_ = bundle.data.get('filedata', None)
            if not file_:
                content['message'] = 'No file uploaded.'
                self.crits_response(content)
            filedata = file_.read()
            result = handle_eml(data=filedata, sourcename=source, reference=reference,
                                user=user, method='EML Upload' + method, tlp=tlp, campaign=campaign,
                                confidence=confidence, bucket_list=bucket_list, ticket=ticket)
        if type_ == 'msg':
            raw_email = bundle.data.get('filedata', None)
            password = bundle.data.get('password', None)

            result = handle_msg(raw_email,
                                source,
                                reference,
                                'Outlook MSG Upload' + method,
                                tlp,
                                user,
                                password,
                                campaign,
                                confidence,
                                bucket_list=bucket_list,
                                ticket=ticket)
        if type_ == 'raw':
{'fraction_non_alphanumeric': 0.08950827101744845, 'fraction_numerical': 0.00022660321776569228, 'mean_word_length': 1.9521739130434783, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5243851', 'n_tokens_mistral': 2283, 'n_tokens_neox': 2176, 'n_words': 611}