예제 #1
0
def create_project(user, name="", uid=None, summary='', text='', stream=None, label=None,
                   privacy=Project.PRIVATE, update=False):

    name = name or "My New Project"
    # Set or create the project uid.
    uid = uid or util.get_uuid(8)

    # Attempts to select the project.
    project = Project.objects.filter(uid=uid)

    # If it is not an update request return the project unchanged.
    if project and not update:
        return project.first()

    if project:
        # Update existing project.
        current = project.first()
        text = text or current.text
        name = name or current.name
        project.update(text=text, name=name)
        project = project.first()
        logger.info(f"Updated project: {project.name} uid: {project.uid}")
    else:
        # Create a new project.
        project = Project.objects.create(label=label, name=name, uid=uid, text=text,
                                         owner=user, privacy=privacy)

        logger.info(f"Created project: {project.name} uid: {project.uid}")

    # Update the image for the project.
    if stream:
        project.image.save(stream.name, stream, save=True)

    return project
예제 #2
0
def create_job(analysis,
               user=None,
               json_text='',
               json_data={},
               name=None,
               state=Job.QUEUED,
               uid=None,
               save=True,
               fill_with={}):
    """
    Note: Parameter 'fill_with' needs to be a flat key:value dictionary.
    """
    state = state or Job.QUEUED
    owner = user or analysis.project.owner
    project = analysis.project

    if json_data:
        json_text = hjson.dumps(json_data)
    else:
        json_text = json_text or analysis.json_text

    # Needs the json_data to set the summary.
    json_data = hjson.loads(json_text)

    # Generate a meaningful job title.
    name = make_job_title(recipe=analysis, data=json_data)
    uid = uid or util.get_uuid(8)

    # Create the job instance.
    job = Job.objects.create(name=name,
                             state=state,
                             json_text=json_text,
                             security=Job.AUTHORIZED,
                             project=project,
                             analysis=analysis,
                             owner=owner,
                             template=analysis.template,
                             uid=uid)
    # Fill the json data.
    json_data = fill_json_data(job=job,
                               source_data=json_data,
                               project=project,
                               fill_with=fill_with)

    # Generate a meaningful job title.
    name = make_job_title(recipe=analysis, data=json_data)
    # Update the json_text and name
    job.json_text = hjson.dumps(json_data)
    job.name = name

    if save:
        # Save the updated json_text and name.
        job.save()
        # Update the projects lastedit user when a job is created
        logger.info(f"Created job id={job.id} name={job.name}")

    return job