-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into design-update
- Loading branch information
Showing
11 changed files
with
240 additions
and
5 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
foundation/organisation/migrations/0003_add_nowdoing_field.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('organisation', '0002_sidebarextension'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='NowDoing', | ||
fields=[ | ||
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), | ||
('doing_type', models.CharField(max_length=10, choices=[(b'reading', b'reading'), (b'listening', b'listening'), (b'working', b'working'), (b'location', b'location'), (b'watching', b'watching'), (b'eating', b'eating')])), | ||
('link', models.URLField(null=True, blank=True)), | ||
('text', models.TextField(null=True, blank=True)), | ||
], | ||
), | ||
migrations.AddField( | ||
model_name='person', | ||
name='username_on_slack', | ||
field=models.CharField(max_length=100, null=True, blank=True), | ||
), | ||
migrations.AddField( | ||
model_name='nowdoing', | ||
name='person', | ||
field=models.ForeignKey(to='organisation.Person'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from unittest import TestCase | ||
|
||
from ..utils import get_activity, extract_ograph_title | ||
|
||
|
||
class HelpersTest(TestCase): | ||
def test_get_activity_extracts_type(self): | ||
text = '#reading http://goodreads.com/123' | ||
self.assertEqual(get_activity(text), 'reading') | ||
|
||
text2 = '#watching http://goodreads.com/123' | ||
self.assertEqual(get_activity(text2), 'watching') | ||
|
||
text3 = '#eating http://goodreads.com/123' | ||
self.assertEqual(get_activity(text3), 'eating') | ||
|
||
text4 = '#working http://goodreads.com/123' | ||
self.assertEqual(get_activity(text4), 'working') | ||
|
||
def test_get_activity_returns_none_when_no_match(self): | ||
text = '#foobaring http://goodreads.com/123' | ||
self.assertEqual(get_activity(text), None) | ||
|
||
def test_extract_ograph_title(self): | ||
link = 'http://www.goodreads.com/book/show/486625.Close_to_the_Machine' | ||
text = '#reading {}'.format(link) | ||
url, title = extract_ograph_title(text) | ||
self.assertEqual(url, link) | ||
self.assertEqual(title, 'Close to the Machine') | ||
|
||
def test_extract_ograph_title_without_url(self): | ||
text = '#working Making the world more open' | ||
url, title = extract_ograph_title(text) | ||
self.assertIsNone(url) | ||
self.assertEqual(title, 'Making the world more open') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import re | ||
import opengraph | ||
|
||
from django.http import JsonResponse | ||
|
||
from foundation.organisation.models import NowDoing | ||
|
||
|
||
def extract_ograph_title(text): | ||
text_without_hashtag = ' '.join(text.split(' ')[1:]) | ||
url_pattern = r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]' \ | ||
+ '|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+' | ||
urls = re.findall(url_pattern, text_without_hashtag) | ||
if urls: | ||
content = opengraph.OpenGraph(url=urls[0]) | ||
title = content.get('title', text_without_hashtag) | ||
return urls[0], title.encode('utf-8') | ||
return None, text_without_hashtag.encode('utf-8') | ||
|
||
|
||
def get_activity(text): | ||
options = '|'.join([a[0] for a in NowDoing.ACTIVITIES]) | ||
pattern = '^#({}).*'.format(options) | ||
matches = re.findall(pattern, text) | ||
if matches: | ||
return matches[0] | ||
return None | ||
|
||
|
||
def fail_json(message, status_code=400): | ||
response = JsonResponse({'success': False, | ||
'message': message}) | ||
response.status_code = status_code | ||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters