Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bounty state model #5474

Merged
merged 2 commits into from
Nov 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions app/dashboard/migrations/0063_bounty_state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Generated by Django 2.2.4 on 2019-11-11 13:06

import django.contrib.postgres.fields.jsonb
from django.db import migrations, models
import django.db.models.deletion
import economy.models


class Migration(migrations.Migration):

dependencies = [
('dashboard', '0062_hackathonevent_show_results'),
]

operations = [
migrations.AddField(
model_name='bounty',
name='bounty_state',
field=models.CharField(choices=[('open', 'Open Bounty'), ('work_started', 'Work Started'), ('work_submitted', 'Work Submitted'), ('done', 'Done'), ('cancelled', 'Cancelled'), ('expired', 'Expired')], db_index=True, default='open', max_length=50),
),
migrations.CreateModel(
name='BountyEvent',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_on', models.DateTimeField(db_index=True, default=economy.models.get_time)),
('modified_on', models.DateTimeField(default=economy.models.get_time)),
('event_type', models.CharField(choices=[('accept_worker', 'Accept Worker'), ('cancel_bounty', 'Cancel Bounty'), ('submit_work', 'Submit Work'), ('stop_work', 'Stop Work'), ('express_interest', 'Express Interest'), ('payout_bounty', 'Payout Bounty'), ('expire_bounty', 'Expire Bounty'), ('extend_expiration', 'Extend Expiration'), ('close_bounty', 'Close Bounty')], max_length=50)),
('metadata', django.contrib.postgres.fields.jsonb.JSONField(blank=True, default=dict)),
('bounty', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='events', to='dashboard.Bounty')),
('created_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='events', to='dashboard.Profile')),
],
options={
'abstract': False,
},
),
]
74 changes: 74 additions & 0 deletions app/dashboard/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,23 @@ class Bounty(SuperModel):
('submitted', 'submitted'),
('unknown', 'unknown'),
)

BOUNTY_STATES = (
('open', 'Open Bounty'),
('work_started', 'Work Started'),
('work_submitted', 'Work Submitted'),
('done', 'Done'),
('cancelled', 'Cancelled'),
('expired', 'Expired'),
)

FUNDED_STATUSES = ['reserved', 'open', 'started', 'submitted', 'done']
OPEN_STATUSES = ['reserved', 'open', 'started', 'submitted']
CLOSED_STATUSES = ['expired', 'unknown', 'cancelled', 'done']
WORK_IN_PROGRESS_STATUSES = ['reserved', 'open', 'started', 'submitted']
TERMINAL_STATUSES = ['done', 'expired', 'cancelled']

bounty_state = models.CharField(max_length=50, choices=BOUNTY_STATES, default='open', db_index=True)
web3_type = models.CharField(max_length=50, default='bounties_network')
title = models.CharField(max_length=1000)
web3_created = models.DateTimeField(db_index=True)
Expand Down Expand Up @@ -382,6 +393,46 @@ def save(self, *args, **kwargs):
self.github_url = clean_bounty_url(self.github_url)
super().save(*args, **kwargs)

EVENT_HANDLERS = {
'traditional': {
'open_bounty': {
'accept_worker': 'work_started',
'cancel_bounty': 'cancelled'},
'work_started': {
'submit_work': 'work_submitted',
'stop_work': 'open_bounty',
'cancel_bounty': 'cancelled'},
'work_submitted': {
'payout_bounty': 'done',
'cancel_bounty': 'cancelled'},
},
'cooperative': {
'open_bounty': {
'accept_worker': 'work_started',
'cancel_bounty': 'cancelled'},
'work_started': {
'submit_work': 'work_submitted',
'stop_work': 'open_bounty',
'cancel_bounty': 'cancelled'},
'work_submitted': {
'close_bounty': 'done',
'cancel_bounty': 'cancelled'},
},
'contest': {
'open_bounty': {
'payout_bounty': 'done',
'cancel_bounty': 'cancelled'}
}
}


def handle_event(self, event):
"""Handle a new BountyEvent, and potentially change state"""
next_state = self.EVENT_HANDLERS[self.project_type][self.bounty_state].get(event.event_type)
if next_state:
self.bounty_state = next_state
self.save()

@property
def latest_activity(self):
activity = Activity.objects.filter(bounty=self.pk).order_by('-pk')
Expand Down Expand Up @@ -1226,6 +1277,29 @@ def total_reserved_length_label(self):
return ''


class BountyEvent(SuperModel):
"""An Event taken by a user, which may change the state of a Bounty"""

EVENT_TYPES = (
('accept_worker', 'Accept Worker'),
('cancel_bounty', 'Cancel Bounty'),
('submit_work', 'Submit Work'),
('stop_work', 'Stop Work'),
('express_interest', 'Express Interest'),
('payout_bounty', 'Payout Bounty'),
('expire_bounty', 'Expire Bounty'),
('extend_expiration', 'Extend Expiration'),
('close_bounty', 'Close Bounty'),
)

bounty = models.ForeignKey('dashboard.Bounty', on_delete=models.CASCADE,
related_name='events')
created_by = models.ForeignKey('dashboard.Profile',
on_delete=models.SET_NULL, related_name='events', blank=True, null=True)
event_type = models.CharField(max_length=50, choices=EVENT_TYPES)
metadata = JSONField(default=dict, blank=True)


class BountyFulfillmentQuerySet(models.QuerySet):
"""Handle the manager queryset for BountyFulfillments."""

Expand Down
69 changes: 69 additions & 0 deletions app/dashboard/tests/test_bounty_state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# -*- coding: utf-8 -*-
"""Test BountyState and BountyEvent interactions and FSM

Copyright (C) 2019 Gitcoin Core

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

"""

from datetime import timedelta

from django.utils import timezone

from dashboard.models import Bounty, BountyEvent
from test_plus.test import TestCase


class BountyStateTest(TestCase):
"""Define tests for bounty states and events."""

def setUp(self):
self.bounty = Bounty.objects.create(
title='foo',
project_type='traditional',
value_in_token=3,
token_name='USDT',
web3_created=timezone.now() - timedelta(days=7),
github_url='https://github.com/danlipert/gitcoin-test/issues/1',
token_address='0x0',
issue_description='hello world',
bounty_owner_github_username='example',
is_open=True,
accepted=True,
expires_date=timezone.now() + timedelta(days=1, hours=1),
idx_project_length=5,
project_length='Months',
bounty_type='Feature',
experience_level='Intermediate',
raw_data={},
idx_status='open',
bounty_owner_email='[email protected]',
current_bounty=True,
bounty_state='open_bounty'
)

def test_handle_event(self):
event = BountyEvent.objects.create(
bounty=self.bounty,
event_type='express_interest'
)
self.bounty.handle_event(event)
assert self.bounty.bounty_state == 'open_bounty'
event_accept = BountyEvent.objects.create(
bounty=self.bounty,
event_type='accept_worker'
)
self.bounty.handle_event(event_accept)
assert self.bounty.bounty_state == 'work_started'