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

Jkmarx/add config model #3177

Merged
merged 8 commits into from
Jan 25, 2019
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion refinery/core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from .models import (
Analysis, AnalysisNodeConnection, AnalysisResult, DataSet, Download,
ExtendedGroup, Event, InvestigationLink, Invitation, Ontology, Project,
SiteProfile, SiteStatistics, Tutorials, UserProfile, Workflow,
SiteProfile, SiteStatistics, SiteVideo, Tutorials, UserProfile, Workflow,
WorkflowEngine)
from .utils import admin_ui_deletion

Expand Down Expand Up @@ -154,6 +154,10 @@ class SiteStatisticsAdmin(AdminFieldPopulator):
change_list_template = "admin/core/sitestatistics/change_list.html"


class SiteVideoAdmin(AdminFieldPopulator):
list_display = ['site_profile']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AdminFieldPopulator by default will expose all of a Model's fields in the Admin UI. Do we just want to show the site_profile or would it be useful to see the caption, source and source_id as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@scottx611x It's displaying as I would expect below. Can you clarify?
screen shot 2019-01-25 at 9 33 00 am



class EventAdmin(AdminFieldPopulator):
pass

Expand All @@ -173,5 +177,6 @@ class EventAdmin(AdminFieldPopulator):
admin.site.register(Tutorials, TutorialsAdmin)
admin.site.register(Ontology, OntologyAdmin)
admin.site.register(SiteProfile, SiteProfileAdmin)
admin.site.register(SiteVideo, SiteVideoAdmin)
admin.site.register(SiteStatistics, SiteStatisticsAdmin)
admin.site.register(Event, EventAdmin)
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('core', '0028_auto_20180611_1640'),
]

operations = [
migrations.CreateModel(
name='SiteVideo',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False,
auto_created=True, primary_key=True)),
('caption', models.TextField(blank=True)),
('source', models.CharField(max_length=100, blank=True)),
('source_id', models.CharField(max_length=100)),
],
),
migrations.AddField(
model_name='siteprofile',
name='about_markdown',
field=models.TextField(blank=True),
),
migrations.AddField(
model_name='siteprofile',
name='intro_markdown',
field=models.TextField(blank=True),
),
migrations.AddField(
model_name='siteprofile',
name='twitter_username',
field=models.CharField(max_length=100, blank=True),
),
migrations.AddField(
model_name='sitevideo',
name='site_profile',
field=models.ForeignKey(to='core.SiteProfile'),
),
]
10 changes: 10 additions & 0 deletions refinery/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2221,6 +2221,9 @@ class SiteProfile(models.Model):

site = models.OneToOneField(Site, related_name='profile')
repo_mode_home_page_html = models.TextField(blank=True)
about_markdown = models.TextField(blank=True)
intro_markdown = models.TextField(blank=True)
twitter_username = models.CharField(max_length=100, blank=True)

def __unicode__(self):
return self.site.name
Expand Down Expand Up @@ -2340,6 +2343,13 @@ def get_aggregate_sum(field_name):
]


class SiteVideo(models.Model):
caption = models.TextField(blank=True)
site_profile = models.ForeignKey(SiteProfile)
source = models.CharField(max_length=100, blank=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing that could be useful for the source field is the use of the choices arg.

class SiteVideo(models.Model):
    YOUTUBE = 'YT'

    VIDEO_SOURCE_CHOICES = (
        (YOUTUBE, 'Youtube'),
        ...
    )
    ...
    source = models.CharField(choices=VIDEO_SOURCE_CHOICES, max_length=100, blank=True)

Even if we don't envision videos coming from too many different sources, this would still solve the problem of potentially having many differing entries like: youtube, Youtube, YouTube etc.

source_id = models.CharField(max_length=100)


class Event(models.Model):
date_time = models.DateTimeField(default=timezone.now)
data_set = models.ForeignKey(DataSet, null=True)
Expand Down
49 changes: 48 additions & 1 deletion refinery/core/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from rest_framework import serializers
from rest_framework.validators import UniqueValidator

from .models import DataSet, Event, User, UserProfile, Workflow
from .models import (DataSet, Event, SiteProfile, SiteVideo, User,
UserProfile, Workflow)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -82,6 +83,52 @@ def partial_update(self, instance, validated_data):
return instance


class SiteVideoSerializer(serializers.ModelSerializer):
class Meta:
model = SiteVideo
fields = ('caption', 'site_profile', 'source', 'source_id', 'id')

def create(self, validated_data):
return SiteVideo.objects.create(**validated_data)

def partial_update(self, instance, validated_data):
instance.caption = validated_data.get('caption', instance.caption)
instance.source = validated_data.get('source', instance.source)
instance.source_id = validated_data.get('source_id',
instance.source_id)
instance.save()
return instance


class SiteProfileSerializer(serializers.ModelSerializer):
site_videos = serializers.SerializerMethodField()

def get_site_videos(self, site_profile):
site_videos = site_profile.sitevideo_set.all()
serializer = SiteVideoSerializer(site_videos, many=True)
return serializer.data

class Meta:
model = SiteProfile
fields = ('about_markdown', 'site', 'intro_markdown',
'twitter_username', 'site_videos')

def partial_update(self, instance, validated_data):
"""
Update and return an existing `SiteProfile` instance, given the
validated data.
"""
instance.about_markdown = validated_data.get('about_markdown',
instance.about_markdown)
instance.intro_markdown = validated_data.get('intro_markdown',
instance.intro_markdown)
instance.twitter_username = validated_data.get(
'twitter_username', instance.twitter_username
)
instance.save()
return instance
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the shorthand for this would look like: instance.update(**validated_data)
Edit: Just kidding this wouldn't work... a QuerySet is required.

SiteProfile.objects.filter(pk=instance.pk).update(**validated_data) would probably work, but I haven't tried it out



class UserProfileSerializer(serializers.ModelSerializer):

class Meta:
Expand Down
37 changes: 35 additions & 2 deletions refinery/core/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.core.files.base import ContentFile
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import TestCase
Expand Down Expand Up @@ -32,8 +33,8 @@
from .models import (INPUT_CONNECTION, OUTPUT_CONNECTION, Analysis,
AnalysisNodeConnection, AnalysisResult, BaseResource,
DataSet, Event, ExtendedGroup, InvestigationLink,
Project, SiteStatistics, Tutorials, UserProfile,
Workflow, WorkflowEngine)
Project, SiteProfile, SiteStatistics, SiteVideo,
Tutorials, UserProfile, Workflow, WorkflowEngine)
from .tasks import collect_site_statistics


Expand Down Expand Up @@ -997,6 +998,38 @@ def test_set_owner_data_set_adds_share_perms(self):
self.assertTrue('share_dataset' in user_perms)


class SiteProfileUnitTests(TestCase):
def setUp(self):
self.current_site = Site.objects.get_current()
self.site_profile = SiteProfile.objects.create(site=self.current_site)

def test_site_profile_created_fk(self):
self.assertEqual(self.site_profile.site, self.current_site)

def test_site_profile_creates_blank_fields(self):
self.assertEqual(self.site_profile.about_markdown, '')
self.assertEqual(self.site_profile.twitter_username, '')


class SiteVideoUnitTests(TestCase):
def setUp(self):
self.current_site = Site.objects.get_current()
self.site_profile = SiteProfile.objects.create(site=self.current_site)
self.source_id_1 = 'yt_id_5'
self.site_video = SiteVideo.objects.create(
site_profile=self.site_profile, source_id=self.source_id_1
)

def test_site_profile_creates_blank_fields(self):
self.assertEqual(self.site_video.caption, '')
self.assertEqual(self.site_video.source, '')

def test_site_returns_videos(self):
self.site_profile.refresh_from_db()
self.assertEqual(self.site_profile.sitevideo_set.all()[0],
self.site_video)


class SiteStatisticsUnitTests(TestCase):
def setUp(self):
# Simulate a day of user activity
Expand Down