<commit_before>#REST API

from django.contrib.auth.models import User
from tastypie import fields
from tastypie.resources import ModelResource, ALL
from tastypie.authorization import DjangoAuthorization
from log.authentication import MonlogAuthentication
from log.authentication import CookieAuthentication
from log.models import LogMessage, SEVERITY_CHOICES
from log.validation import LogValidation
from datetime import datetime 

class ApplicationResource(ModelResource):
    """
    Used by LogCollectionResource to enable filtering on applications. 
    This resource is not available in the REST Api.
    """
    class Meta:
        allowed_methods=[]
        queryset = User.objects.all()
        fields= ['id','username']
        resource_name = "application"
        ordering = ['username']


class LogCollectionResource(ModelResource):
    """
    Allows a user to get log messages through GET requests. 

    User must be logged in and provide Djangos authentication cookie to be authenticated.
    """
    application = fields.ForeignKey(ApplicationResource, 'application', full=True)

    def dehydrate(self, bundle):
        if 'severity' in bundle.data:
            # translate numeric severity to text
            bundle.data['severity'] = SEVERITY_CHOICES[bundle.data['severity']][1]
        return bundle

    
    def build_filters(self, filters=None):
        if filters is None:
            filters = {}
        else:
            filters = filters.copy()

        if 'datetime__gte' in filters:
            filters['datetime__gte'] = filters['datetime__gte'].replace("T", " ")
            filters['datetime__gte'] = filters['datetime__gte'].replace("Z", "")
        if 'datetime__lte' in filters:
            filters['datetime__lte'] = filters['datetime__lte'].replace("T", " ")
            filters['datetime__lte'] = filters['datetime__lte'].replace("Z", "")
        if 'add_datetime__gte' in filters:
            filters['add_datetime__gte'] = filters['add_datetime__gte'].replace("T", " ")
            filters['add_datetime__gte'] = filters['add_datetime__gte'].replace("Z", "")
        if 'add_datetime__lte' in filters:
            filters['add_datetime__lte'] = filters['add_datetime__lte'].replace("T", " ")
            filters['add_datetime__lte'] = filters['add_datetime__lte'].replace("Z", "")

        if 'search' in filters:
            filters['long_desc__in'] = filters['search']
            filters['short_desc__in'] = filters['search']
            #del filters['search']
       
            
        orm = super(LogCollectionResource, self).build_filters(filters)

        # if user doesn't specify severity level, no log messages will be returned.
        if "severity__in" not in filters:
            orm['severity__in'] = ""

        return orm

    class Meta:
        allowed_methods = ['get']
        queryset = LogMessage.objects.all()
        resource_name = "logmessages"
        authentication = CookieAuthentication()
        authorization = DjangoAuthorization()
        filtering = {
            "long_desc" : ['in'],
            "short_desc" : ['in'],
            "severity" : ['in'],
            "datetime" : ['gte','lte'],
            "server_ip" : ['in'],
            "application" : ['in'],
            "add_datetime" : ['gte', 'lte']
        }
        ordering = ["severity",
                    "datetime",
                    "server_ip",
                    "application"]

class LogResource(ModelResource):
    class Meta:
        allowed_methods = ['post']
        queryset = LogMessage.objects.all()
        resource_name = "log"
        authentication = MonlogAuthentication()
        authorization = DjangoAuthorization()
        validation = LogValidation()
<commit_msg>cannot use __in. using __contains instead.<commit_after>#REST API

from django.contrib.auth.models import User
from tastypie import fields
from tastypie.resources import ModelResource, ALL
from tastypie.authorization import DjangoAuthorization
from log.authentication import MonlogAuthentication
from log.authentication import CookieAuthentication
from log.models import LogMessage, SEVERITY_CHOICES
from log.validation import LogValidation
from datetime import datetime 

class ApplicationResource(ModelResource):
    """
    Used by LogCollectionResource to enable filtering on applications. 
    This resource is not available in the REST Api.
    """
    class Meta:
        allowed_methods=[]
        queryset = User.objects.all()
        fields= ['id','username']
        resource_name = "application"
        ordering = ['username']


class LogCollectionResource(ModelResource):
    """
    Allows a user to get log messages through GET requests. 

    User must be logged in and provide Djangos authentication cookie to be authenticated.
    """
    application = fields.ForeignKey(ApplicationResource, 'application', full=True)

    def dehydrate(self, bundle):
        if 'severity' in bundle.data:
            # translate numeric severity to text
            bundle.data['severity'] = SEVERITY_CHOICES[bundle.data['severity']][1]
        return bundle

    
    def build_filters(self, filters=None):
        if filters is None:
            filters = {}
        else:
            filters = filters.copy()

        if 'datetime__gte' in filters:
            filters['datetime__gte'] = filters['datetime__gte'].replace("T", " ")
            filters['datetime__gte'] = filters['datetime__gte'].replace("Z", "")
        if 'datetime__lte' in filters:
            filters['datetime__lte'] = filters['datetime__lte'].replace("T", " ")
            filters['datetime__lte'] = filters['datetime__lte'].replace("Z", "")
        if 'add_datetime__gte' in filters:
            filters['add_datetime__gte'] = filters['add_datetime__gte'].replace("T", " ")
            filters['add_datetime__gte'] = filters['add_datetime__gte'].replace("Z", "")
        if 'add_datetime__lte' in filters:
            filters['add_datetime__lte'] = filters['add_datetime__lte'].replace("T", " ")
            filters['add_datetime__lte'] = filters['add_datetime__lte'].replace("Z", "")

        if 'search' in filters:
            filters['long_desc__contains'] = filters['search']
            filters['short_desc__contains'] = filters['search']
            del filters['search']

        orm = super(LogCollectionResource, self).build_filters(filters)
        # if user doesn't specify severity level, no log messages will be returned.
        if "severity__in" not in filters:
            orm['severity__in'] = ""

        return orm

    class Meta:
        allowed_methods = ['get']
        queryset = LogMessage.objects.all()
        resource_name = "logmessages"
        authentication = CookieAuthentication()
        authorization = DjangoAuthorization()
        filtering = {
            "long_desc" : ['contains'],
            "short_desc" : ['contains'],
            "severity" : ['in'],
            "datetime" : ['gte','lte'],
            "server_ip" : ['in'],
            "application" : ['in'],
            "add_datetime" : ['gte', 'lte']
        }
        ordering = ["severity",
                    "datetime",
                    "server_ip",
                    "application"]

class LogResource(ModelResource):
    class Meta:
        allowed_methods = ['post']
        queryset = LogMessage.objects.all()
        resource_name = "log"
        authentication = MonlogAuthentication()
        authorization = DjangoAuthorization()
        validation = LogValidation()

{'fraction_non_alphanumeric': 0.11428190425390052, 'fraction_numerical': 0.00026670222696359517, 'mean_word_length': 2.681885125184094, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3171119', 'n_tokens_mistral': 1973, 'n_tokens_neox': 1925, 'n_words': 485}