Пример #1
0
def submit(request):
    if request.method == 'POST':
        submit = request.POST
    elif request.method == 'GET':
        submit = request.GET
    else:
        raise Http404

    mac = submit.get('mac')
    machine = None
    if mac:
        try:
            machine = Machine.objects.get(mac=mac)
        except Machine.DoesNotExist:
            machine = Machine(mac=mac)

    if machine:
        if submit.get('hostname'):
            machine.hostname = submit.get('hostname')
        machine.last_checkin = timezone.now()
        machine.save()

        response = ''

        facts = submit.get('Facts')
        if facts is not None:
            facts = json.loads(facts)
            for key in facts:
                try:
                    fact = Fact.objects.get(machine=machine, name=key)
                except:
                    fact = Fact(machine=machine, name=key)
                fact.last_update = timezone.now()
                fact.value = facts[key]

                fact.save()

        facts = submit.get('HistoricalFacts')
        if facts is not None:
            facts = json.loads(facts)
            for key in facts:
                fact = HistoricalFact(machine=machine, name=key)
                fact.timestamp = timezone.now()
                fact.value = facts[key]

                fact.save()

        return HttpResponse('Report submitted.\n')

    return HttpResponse('Report not submitted.\n')
Пример #2
0
from mongoengine import *
from variables import Variables
from models import Fact, Rumor
import ast
import random

connect(host=Variables.DB_CONNECTION)

if Variables.seed:
    with open(
            '/home/talalnks/RumorBusterBackend/RumorsFacts.json') as dataFile:
        data = ast.literal_eval(dataFile.read())
        for rumor in data:
            existingRumor = Rumor.objects(body=rumor['Rumors']).first()
            if not existingRumor:
                r = Rumor(body=rumor['Rumors'],
                          report_counter=random.randint(25, 150)).save()
                if len(rumor['Facts']) > 0:
                    f = Fact(body=rumor['Facts'], rumor=r).save()