Ejemplo n.º 1
0
def add_candidate(session, candidate_file, candidate_name, candidate_author,
                  candidate_original_url, candidate_license,
                  candidate_submitter, submitter_email,
                  election_id, user=None):
    """ Add a new candidate to the database.

    :arg session: session with which to interact with the database
    :arg candidate_file: the file name of candidate
    :arg candidate_name: the name of candidate
    :arg candidate_author: the name of the author of this candidate
    :arg candidate_original_url: if the artwork originates from
            someone else, this should be a link to the original artwork.
    :arg candidate_license: the license name of candidate
    :arg candidate_submitter: the name of the submitter of this candidate
    :arg submitter_email: the email address of submitter
    :arg election_id: the identifier of the election this candidate is
            candidate for.
    """
    if not user:
        raise NuancierException('User required to add a new candidate')

    election = nuancier.lib.model.Elections.by_id(session, election_id)

    candidates = nuancier.lib.model.Candidates.get_by_submitter(
        session, candidate_submitter, election_id)
    if election.user_n_candidates and \
        len(candidates) >= election.user_n_candidates:
        raise NuancierException(
            'You have already uploaded the maximum number of candidates (%s) '
            'you can upload for this election' % election.user_n_candidates)

    candidate = nuancier.lib.model.Candidates.by_election_file(
        session, election_id, candidate_file)
    if candidate:
        raise NuancierException(
            'A candidate with the filename "%s" has already been submitted'
            % (candidate_file)
        )

    candidate = nuancier.lib.model.Candidates(
        candidate_file=candidate_file,
        candidate_name=candidate_name,
        candidate_author=candidate_author,
        candidate_original_url=candidate_original_url,
        candidate_license=candidate_license,
        candidate_submitter=candidate_submitter,
        submitter_email=submitter_email,
        election_id=election_id,
    )
    session.add(candidate)
    session.flush()

    notifications.publish(
        topic='candidate.new',
        msg=dict(
            agent=user,
            election=election.api_repr(version=1),
            candidate=candidate.api_repr(version=1),
        )
    )
Ejemplo n.º 2
0
def add_candidate(session, candidate_file, candidate_name, candidate_author,
                  candidate_original_url, candidate_license,
                  candidate_submitter, submitter_email,
                  election_id, user=None):
    """ Add a new candidate to the database.

    :arg session: session with which to interact with the database
    :arg candidate_file: the file name of candidate
    :arg candidate_name: the name of candidate
    :arg candidate_author: the name of the author of this candidate
    :arg candidate_original_url: if the artwork originates from
            someone else, this should be a link to the original artwork.
    :arg candidate_license: the license name of candidate
    :arg candidate_submitter: the name of the submitter of this candidate
    :arg submitter_email: the email address of submitter
    :arg election_id: the identifier of the election this candidate is
            candidate for.
    """
    if not user:
        raise NuancierException('User required to add a new candidate')

    election = nuancier.lib.model.Elections.by_id(session, election_id)

    candidates = nuancier.lib.model.Candidates.get_by_submitter(
        session, candidate_submitter, election_id)
    if election.user_n_candidates and \
        len(candidates) >= election.user_n_candidates:
        raise NuancierException(
            'You have already uploaded the maximum number of candidates (%s) '
            'you can upload for this election' % election.user_n_candidates)

    candidate = nuancier.lib.model.Candidates.by_election_file(
        session, election_id, candidate_file)
    if candidate:
        raise NuancierException(
            'A candidate with the filename "%s" has already been submitted'
            % (candidate_file)
        )

    candidate = nuancier.lib.model.Candidates(
        candidate_file=candidate_file,
        candidate_name=candidate_name,
        candidate_author=candidate_author,
        candidate_original_url=candidate_original_url,
        candidate_license=candidate_license,
        candidate_submitter=candidate_submitter,
        submitter_email=submitter_email,
        election_id=election_id,
    )
    session.add(candidate)
    session.flush()

    notifications.publish(
        topic='candidate.new',
        msg=dict(
            agent=user,
            election=election.api_repr(version=1),
            candidate=candidate.api_repr(version=1),
        )
    )
Ejemplo n.º 3
0
def add_candidate(session, candidate_file, candidate_name, candidate_author,
                  candidate_original_url, candidate_license,
                  candidate_submitter, submitter_email,
                  election_id, user=None):
    """ Add a new candidate to the database.

    :arg session: session name
    :arg candidate_file: the file name of candidate
    :arg candidate_name: the name of candidate
    :arg candidate_author: the name of the author of this candidate
    :arg candidate_original_url: if the artwork originates from
            someone else, this should be a link to the original artwork.
    :arg candidate_license: the license name of candidate
    :arg candidate_submitter: the name of the submitter of this candidate
    :arg submitter_email: mail ID of submitter
    :arg election_id: the identifier of the election this candidate is
            candidate for.

    """
    if not user:
        raise NuancierException('User required to add a new candidate')

    candidate = nuancier.lib.model.Candidates.by_election_file(
        session, election_id, candidate_file)
    if candidate:
        raise NuancierException(
            'A candidate with the filename "%s" has already been submitted'
            % (candidate_file)
        )

    candidate = nuancier.lib.model.Candidates(
        candidate_file=candidate_file,
        candidate_name=candidate_name,
        candidate_author=candidate_author,
        candidate_original_url=candidate_original_url,
        candidate_license=candidate_license,
        candidate_submitter=candidate_submitter,
        submitter_email=submitter_email,
        election_id=election_id,
    )
    session.add(candidate)
    session.flush()

    election = nuancier.lib.model.Elections.by_id(session, election_id)

    notifications.publish(
        topic='candidate.new',
        msg=dict(
            agent=user,
            election=election.api_repr(version=1),
            candidate=candidate.api_repr(version=1),
        )
    )
Ejemplo n.º 4
0
def add_election(session,
                 election_name,
                 election_folder,
                 election_year,
                 election_date_start,
                 election_date_end,
                 submission_date_start,
                 submission_date_end,
                 election_n_choice,
                 user_n_candidates,
                 election_badge_link=None,
                 user=None):
    """ Add a new election to the database.

    :arg session:
    :arg election_name:
    :arg election_folder:
    :arg election_year:
    :arg election_date_start:
    :arg election_date_end:
    :arg submission_date_start:
    :arg submission_date_end:
    :arg election_n_choice:
    :arg user_n_candidates:
    :kwarg election_badge_link:
    :kwarg user: The user/admin creating the election.
    """
    if not user:
        raise NuancierException('User required to create an election')

    election = nuancier.lib.model.Elections(
        election_name=election_name,
        election_folder=election_folder,
        election_year=election_year,
        election_date_start=election_date_start,
        election_date_end=election_date_end,
        submission_date_start=submission_date_start,
        submission_date_end=submission_date_end,
        election_n_choice=election_n_choice,
        user_n_candidates=user_n_candidates,
        election_badge_link=election_badge_link,
    )
    session.add(election)
    session.flush()

    notifications.publish(topic='election.new',
                          msg=dict(
                              agent=user,
                              election=election.api_repr(version=1),
                          ))

    return election
Ejemplo n.º 5
0
def add_election(session, election_name, election_folder, election_year,
                 election_date_start, election_date_end,
                 submission_date_start, submission_date_end,
                 election_n_choice, user_n_candidates,
                 election_badge_link=None, user=None):
    """ Add a new election to the database.

    :arg session:
    :arg election_name:
    :arg election_folder:
    :arg election_year:
    :arg election_date_start:
    :arg election_date_end:
    :arg submission_date_start:
    :arg submission_date_end:
    :arg election_n_choice:
    :arg user_n_candidates:
    :kwarg election_badge_link:
    :kwarg user: The user/admin creating the election.
    """
    if not user:
        raise NuancierException('User required to create an election')

    election = nuancier.lib.model.Elections(
        election_name=election_name,
        election_folder=election_folder,
        election_year=election_year,
        election_date_start=election_date_start,
        election_date_end=election_date_end,
        submission_date_start=submission_date_start,
        submission_date_end=submission_date_end,
        election_n_choice=election_n_choice,
        user_n_candidates=user_n_candidates,
        election_badge_link=election_badge_link,
    )
    session.add(election)
    session.flush()

    notifications.publish(
        topic='election.new',
        msg=dict(
            agent=user,
            election=election.api_repr(version=1),
        )
    )

    return election
Ejemplo n.º 6
0
def edit_election(session, election, election_name, election_folder,
                  election_year, election_date_start, election_date_end,
                  submission_date_start, election_n_choice,
                  election_badge_link=None, user=None):
    """ Edit an election of the database.

    :arg session:
    :arg election:
    :arg election_name:
    :arg election_folder:
    :arg election_year:
    :arg election_date_start:
    :arg election_date_end:
    :arg submission_date_start:
    :arg election_n_choice:
    :kwarg election_badge_link:
    :kwarg user:
    """
    if not user:
        raise NuancierException('User required to edit an election')

    edited = []
    if election.election_name != election_name:
        election.election_name = election_name
        edited.append('election name')

    if election.election_folder != election_folder:
        election.election_folder = election_folder
        edited.append('election folder')

    if election.election_year != election_year:
        election.election_year = election_year
        edited.append('election year')

    if election.election_date_start != election_date_start:
        election.election_date_start = election_date_start
        edited.append('election start date')

    if election.election_date_end != election_date_end:
        election.election_date_end = election_date_end
        edited.append('election end date')

    if election.submission_date_start != submission_date_start:
        election.submission_date_start = submission_date_start
        edited.append('submission start date')

    if election.election_n_choice != election_n_choice:
        election.election_n_choice = election_n_choice
        edited.append('election name')

    if election.election_badge_link != election_badge_link:
        election.election_badge_link = election_badge_link
        edited.append('election badge link')

    if edited:
        session.add(election)
        session.flush()

    notifications.publish(
        topic='election.update',
        msg=dict(
            agent=user,
            election=election.api_repr(version=1),
            updated=edited,
        )
    )

    return election
Ejemplo n.º 7
0
def edit_election(session,
                  election,
                  election_name,
                  election_folder,
                  election_year,
                  election_date_start,
                  election_date_end,
                  submission_date_start,
                  submission_date_end,
                  election_n_choice,
                  user_n_candidates,
                  election_badge_link=None,
                  user=None):
    """ Edit an election of the database.

    :arg session:
    :arg election:
    :arg election_name:
    :arg election_folder:
    :arg election_year:
    :arg election_date_start:
    :arg election_date_end:
    :arg submission_date_start:
    :arg submission_date_end:
    :arg election_n_choice:
    :arg user_n_candidates:
    :kwarg election_badge_link:
    :kwarg user:
    """
    if not user:
        raise NuancierException('User required to edit an election')

    edited = []
    if election.election_name != election_name:
        election.election_name = election_name
        edited.append('election name')

    if election.election_folder != election_folder:
        election.election_folder = election_folder
        edited.append('election folder')

    if election.election_year != election_year:
        election.election_year = election_year
        edited.append('election year')

    if election.election_date_start != election_date_start:
        election.election_date_start = election_date_start
        edited.append('election start date')

    if election.election_date_end != election_date_end:
        election.election_date_end = election_date_end
        edited.append('election end date')

    if election.submission_date_start != submission_date_start:
        election.submission_date_start = submission_date_start
        edited.append('submission start date')

    if election.submission_date_end != submission_date_end:
        election.submission_date_end = submission_date_end
        edited.append('submission date end')

    if election.election_n_choice != election_n_choice:
        election.election_n_choice = election_n_choice
        edited.append('election name')

    if election.election_badge_link != election_badge_link:
        election.election_badge_link = election_badge_link
        edited.append('election badge link')

    if election.user_n_candidates != user_n_candidates:
        election.user_n_candidates = user_n_candidates
        edited.append('Number of candidates per user')

    if edited:
        session.add(election)
        session.flush()

    notifications.publish(topic='election.update',
                          msg=dict(
                              agent=user,
                              election=election.api_repr(version=1),
                              updated=edited,
                          ))

    return election