Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add UserArtists

  • Loading branch information...
commit 7c2341ccaf17c6f1cccdfd3f5e22a8ef11661a1f 1 parent 4c642cc
@alexkay authored
Showing with 69 additions and 36 deletions.
  1. +44 −1 app/models.py
  2. +23 −34 app/views.py
  3. +1 −0  db/muspy.sql
  4. +1 −1  urls.py
View
45 app/models.py
@@ -16,15 +16,18 @@
# along with muspy. If not, see <http://www.gnu.org/licenses/>.
import random
+from time import sleep
from django.contrib.auth.models import User
from django.core.mail import EmailMultiAlternatives
-from django.db import models
+from django.db import IntegrityError, models, transaction
from django.db.backends.signals import connection_created
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.template.loader import render_to_string
+import app.musicbrainz as mb
+
class Artist(models.Model):
mbid = models.CharField(max_length=36, unique=True)
@@ -34,11 +37,41 @@ class Artist(models.Model):
@classmethod
def get_by_mbid(cls, mbid):
+ """ Fetches the artist and releases from MB if not in the database. """
try:
return cls.objects.get(mbid=mbid)
except cls.DoesNotExist:
+ pass
+
+ artist_data = mb.get_artist(mbid)
+ if not artist_data:
return None
+ # Sleep 1s to comply with the MB web service.
+ sleep(1)
+
+ artist = Artist(
+ mbid=mbid, name=artist_data['name'], sort_name=artist_data['sort-name'],
+ disambiguation=artist_data['disambiguation'] if 'disambiguation' in artist_data else '')
+ artist.save()
+
+ # Add a few release groups immediately.
+ release_groups = mb.get_release_groups(mbid, limit=100, offset=0)
+ if release_groups:
+ with transaction.commit_on_success():
+ for rg_data in release_groups:
+ # Ignoring releases without a release date.
+ if rg_data.get('first-release-date'):
+ release_group = ReleaseGroup(
+ artist=artist,
+ mbid=rg_data['id'],
+ name=rg_data['title'],
+ type=rg_data['type'],
+ date=ReleaseGroup.parse_date(rg_data['first-release-date']),
+ is_deleted=False)
+ release_group.save()
+ return artist
+
@classmethod
def get_by_user(cls, user):
# TODO: paging
@@ -80,6 +113,7 @@ def parse_date(cls, date_str):
return date
# Django's ManyToManyField generates terrible SQL, simulate it.
+# This also allows us to include additional fields.
class UserArtist(models.Model):
class Meta:
@@ -87,6 +121,15 @@ class Meta:
user = models.ForeignKey(User)
artist = models.ForeignKey(Artist)
+ date = models.DateTimeField(auto_now_add=True)
+
+ @classmethod
+ def add(cls, user, artist):
+ user_artist = cls(user=user, artist=artist)
+ try:
+ user_artist.save()
+ except IntegrityError:
+ pass
class UserProfile(models.Model):
View
57 app/views.py
@@ -16,12 +16,10 @@
# along with muspy. If not, see <http://www.gnu.org/licenses/>.
from datetime import date
-from time import sleep
from django.contrib import messages
from django.contrib.auth import authenticate, login, logout, REDIRECT_FIELD_NAME
from django.contrib.auth.decorators import login_required
-from django.db import transaction
from django.http import HttpResponse
from django.shortcuts import redirect, render
@@ -69,34 +67,8 @@ def article(request, slug):
def artist(request, mbid):
artist = Artist.get_by_mbid(mbid)
if not artist:
- artist_data = mb.get_artist(mbid)
- if not artist_data:
- # TODO: Show a meaningful error message.
- return HttpResponseNotFound()
-
- # Sleep 1s to comply with the MB web service.
- sleep(1)
-
- artist = Artist(
- mbid=mbid, name=artist_data['name'], sort_name=artist_data['sort-name'],
- disambiguation=artist_data['disambiguation'] if 'disambiguation' in artist_data else '')
- artist.save()
-
- # Add a few release groups immediately.
- release_groups = mb.get_release_groups(mbid, limit=100, offset=0)
- if release_groups:
- with transaction.commit_on_success():
- for rg_data in release_groups:
- # Ignoring releases without a release date.
- if rg_data.get('first-release-date'):
- release_group = ReleaseGroup(
- artist=artist,
- mbid=rg_data['id'],
- name=rg_data['title'],
- type=rg_data['type'],
- date=ReleaseGroup.parse_date(rg_data['first-release-date']),
- is_deleted=False)
- release_group.save()
+ # TODO: Show a meaningful error message.
+ return HttpResponseNotFound()
PER_PAGE = 10
offset = int(request.GET.get('offset', 0))
@@ -136,19 +108,19 @@ def artists(request):
LIMIT = 20
if search:
if len(search) > 16384:
- messages.error('The search string is too long.')
+ messages.error(request, 'The search string is too long.')
return redirect('/artists')
if ',' in search and not offset:
# Batch add mode.
Job.add_artists(request.user.key().id(), search, dontadd)
- messages.info('Your artists will be processed in the next couple of '
+ messages.info(request, 'Your artists will be processed in the next couple of '
'minutes. In the meantime you can add more artists.')
return redirect('/artists')
found_artists, count = mb.search_artists(search, limit=LIMIT, offset=offset)
if found_artists is None:
- messages.error('The search server could not fulfill your request '
+ messages.error(request, 'The search server could not fulfil your request '
'due to an internal error. Please try again later.')
return render(request, 'artists.html', {
'artist_rows': artist_rows,
@@ -171,7 +143,7 @@ def artists(request):
UserArtist.add(request.user, artist)
Job.copy_releases(mbid, request.user.key().id())
- message.success("%s has been added!" % artist.name)
+ messages.success(request, "%s has been added!" % artist.name)
return redirect('/artists')
artists_offset = offset + len(found_artists)
@@ -189,6 +161,23 @@ def artists(request):
'artists_offset': artists_offset,
'artists_left': artists_left})
+@login_required
+def artists_add(request):
+ mbid = request.GET.get('id', '').lower()
+ artist = Artist.get_by_mbid(mbid)
+ if not artist:
+ # TODO: Show a meaningful error message.
+ return HttpResponseNotFound()
+
+ UserArtist.add(request.user, artist)
+
+ #TODO
+# search = request.GET.get('search', '')
+# UserSearch.remove(request.user, [search])
+
+ messages.success(request, "%s has been added!" % artist.name)
+ return redirect('/artists')
+
def blog(request):
posts = get_posts()
root = request.build_absolute_uri('/')
View
1  db/muspy.sql
@@ -21,6 +21,7 @@ CREATE TABLE "app_userartist" (
"id" integer NOT NULL PRIMARY KEY,
"user_id" integer NOT NULL REFERENCES "auth_user" ("id"),
"artist_id" integer NOT NULL REFERENCES "app_artist" ("id"),
+ "date" datetime NOT NULL,
UNIQUE ("user_id", "artist_id")
);
CREATE TABLE "app_userprofile" (
View
2  urls.py
@@ -26,7 +26,7 @@
(r'^activate$', 'activate'),
(r'^artist/([0-9a-f\-]+)$', 'artist'),
(r'^artists$', 'artists'),
-# (r'^artists-add$', 'artists_add'),
+ (r'^artists-add$', 'artists_add'),
# (r'^artists-remove$', 'artists_remove'),
(r'^blog$', 'blog'),
(r'^blog/feed$', 'blog_feed'),
Please sign in to comment.
Something went wrong with that request. Please try again.