Esempio n. 1
0
 def test_presence_false(self):
     """
     Tests the _parse method for a 'presence' method when a string has no
     match.
     """
     parser = Parser(method='P/A', regex='Bad Robots')
     result = parser._parse(self.string)
     self.assertFalse(result)
Esempio n. 2
0
 def test_count_multi_matches(self):
     """
     Tests the _parse method for a 'count' method when a string has
     multiple matches.
     """
     parser = Parser(method='COUNT', regex='Bad Bots')
     result = parser._parse(self.string)
     self.assertEqual(result, 2)
Esempio n. 3
0
 def test_copy(self):
     """
     Tests the _parse method for a 'value' method when a string has a
     match.
     """
     parser = Parser(method='COPY')
     result = parser._parse('this is an example post')
     self.assertEqual(result, 'this is an example post')
Esempio n. 4
0
 def test_value_no_match(self):
     """
     Tests the _parse method for a 'value' method when a string has no
     match.
     """
     parser = Parser(method='SUBSTRING', regex='Threats:\s*(\w+)\n')
     result = parser._parse(self.string)
     self.assertEqual(result, None)
Esempio n. 5
0
    def test_value_match(self):
        """
        Tests the _parse method for a 'value' method when a string has a
        match.
        """
        parser = Parser(method='SUBSTRING', regex='Threat:\s*(\w+)\n')
        result = parser._parse(self.string)
        self.assertEqual(result, 'Medium')

        parser = Parser(
            method='SUBSTRING',
            regex='source.?IP.*\s(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})')
        result = parser._parse(self.string)
        self.assertEqual(result, '128.101.101.101')
Esempio n. 6
0
def addparser(request):
    print(request.user.is_staff)
    parserform = AddParser()
    dealform = AddDeal()
    # allParsers = list(Parser.objects.values_list('name'))
    # allParsers = [x[0] for x in allParsers]
    # menu = SelectParser(choices=allParsers)
    allParsers = Parser.objects.all()
    menu = QueryMenu(queryset=allParsers, id="form_select_parser_view_deal")
    dealform.addparser(queryset=allParsers, id="form_select_parser_add_deal")
    allCategory = GetAllCategory()
    categoryForm = AddCategory(*allCategory)
    if request.method == "POST":
        if request.POST.get('add_parser') == 'add_parser':
            name = request.POST['name']
            parser = request.POST['body']
            filename = request.POST['filename']
            filepath = os.path.join(settings.BASE_DIR, 'parsers',
                                    'parserfiles', filename)
            msgs, validParser = ParserValidate(parser)
            if validParser:
                with open(filepath, 'w') as file:
                    print(parser, file=file)
                parserData = Parser(name=name, filepath=filepath)
                try:
                    parserData.save()
                    msgs.append("New Parser Saved!")
                except:
                    msgs.append("Could not save new parser")
            return render(request, 'view_parser.html', {"addparser":parserform, \
                       "adddeal":dealform,\
                       "categoryform":categoryForm,\
                       "parsermenu":menu, \
                       "allParsers":allParsers, \
                       "msgs":msgs, \
                       "validparser":validParser})
        if request.POST.get('add_deal') == 'add_deal':
            title = request.POST['title']
            website = request.POST['website']
            parserid = request.POST['parser']
            brand = request.POST['brand']
            mainclass = request.POST['mainclass']
            subclass = request.POST['subclass']

            parserID = Parser.objects.filter(id=parserid)[0]
            msgs = []
            newCategory = Category(brand=brand,
                                   mainclass=mainclass,
                                   subclass=subclass)
            if Category.objects.filter(brand=brand,
                                       mainclass=mainclass,
                                       subclass=subclass).count() == 0:
                try:
                    newCategory.save()
                    msgs.append("New Category Saved")
                except:
                    msgs.append("Could not save new deal")
            targetCategory = Category.objects.filter(brand=brand,
                                                     mainclass=mainclass,
                                                     subclass=subclass)[0]
            newdeal = Deal(title=title,
                           website=website,
                           parser=parserID,
                           category=targetCategory)
            try:
                newdeal.save()
                msgs.append("New Deal Saved")
            except:
                msgs.append("Could not save new deal")

            return render(request, 'view_parser.html', {"addparser":parserform, \
                       "adddeal":dealform,\
                       "categoryform":categoryForm,\
                       "parsermenu":menu, \
                       "allParsers":allParsers, \
                       "msgs":msgs})

    return render(request, 'view_parser.html', {"addparser":parserform, \
               "adddeal":dealform,\
               "categoryform":categoryForm,\
               "parsermenu":menu, \
               "allParsers":allParsers})