Ejemplo n.º 1
0
    def post(self, request, id):
        record = get_object_or_404(Record, id=id)
        self.check_login()
        if request.user.id != record.user_id:
            self.raise_error('Permission denied.', status=403)
        title = request.POST.get('title')
        if title:
            try:
                with transaction.atomic():
                    record.update_title(title)
            except:
                # 422 Unprocessable Entity
                self.raise_error(
                    u'이미 같은 작품이 등록되어 있어 제목을 바꾸지 못했습니다.',
                    status=422
                )

        if 'category_id' in request.POST:
            category_id = request.POST.get('category_id')
            if category_id:
                record.category = request.user.category_set.get(id=category_id)
            else:
                record.category = None
            record.save()

        return serialize_record(record)
Ejemplo n.º 2
0
    def post(self, request, id):
        record = get_object_or_404(Record, id=id)
        self.check_login()
        if request.user.id != record.user_id:
            self.raise_error('Permission denied.', status=403)

        with transaction.atomic():
            history = History.objects.create(
                user=request.user,
                work=record.work,
                record=record,
                status=request.POST['status'],
                status_type=StatusType[request.POST['status_type']],
                comment=request.POST['comment'],
                contains_spoiler=request.POST.get('contains_spoiler') == 'true',
            )
            record.status_type = history.status_type
            record.status = history.status
            record.updated_at = history.updated_at
            record.save()

        if request.POST.get('publish_twitter') == 'on':
            post_history_to_twitter(request.user, history)

        return {
            'record': serialize_record(record),
            'post': serialize_post(history),
        }
Ejemplo n.º 3
0
 def get(self, request, name):
     user = get_object_or_404(User, username=name)
     include_has_newer_episode = \
         request.GET.get('include_has_newer_episode') == 'true'
     if request.user != user:
         include_has_newer_episode = False
     records = user.record_set.all()
     sort = request.GET.get('sort')
     if sort == 'date':
         records = records.order_by('-updated_at')
     elif sort == 'title':
         records = records.order_by('title')
     status_type = request.GET.get('status_type')
     if status_type:
         records = records.filter(status_type=StatusType[status_type])
     limit = request.GET.get('limit')
     if limit:
         try:
             limit = int(limit)
             records = records[:limit]
         except ValueError:
             pass
     return [serialize_record(
         record,
         include_has_newer_episode=include_has_newer_episode
     ) for record in records]
Ejemplo n.º 4
0
 def delete(self, request, id):
     history = get_object_or_404(History, id=id)
     self.check_login()
     if request.user.id != history.user_id:
         self.raise_error('Permission denied.', status=403)
     if history.record.history_set.count() == 1:
         self.raise_error(u'등록된 작품마다 최소 1개의 기록이 필요합니다.',
             status=422) # 422 Unprocessable Entity
     history.delete()
     return {
         'record': serialize_record(history.record)
     }
Ejemplo n.º 5
0
def library(request, username=None):
    if username:
        user = get_object_or_404(User, username=username)
    else:
        return redirect('user.views.library', username=request.user.username)

    return render(request, 'user/library.html', {
        'owner': user,
        'preload_data': {
            'owner': serializers.serialize_user(user, request.user),
            'current_user': serializers.serialize_user(request.user, request.user) if request.user.is_authenticated() else None,
            'records': [serializers.serialize_record(r, include_has_newer_episode=user == request.user)
                for r in user.record_set.all()],
        },
    })
Ejemplo n.º 6
0
    def post(self, request, name):
        self.check_login()
        if request.user.username != name:
            self.raise_error('Permission denied.', status=403)
        title = request.POST.get('work_title')
        if not title:
            # 400 Bad Request
            self.raise_error(u'작품 제목을 입력하세요.', status=400)
        work = get_or_create_work(title)
        category_id = request.POST.get('category_id')
        if category_id:
            # TODO: Raise appropriate exception if not exist/no permission
            category = request.user.category_set.get(id=category_id)
        else:
            category = None
        try:
            record = Record.objects.get(user=request.user, work=work)
            # 422 Unprocessable Entity
            self.raise_error(
                u'이미 같은 작품이 "%s"로 등록되어 있습니다.' % record.title,
                status=422
            )
        except Record.DoesNotExist:
            pass

        record = Record.objects.create(
            user=request.user,
            work=work,
            title=title,
            category=category,
            status='',
            status_type=StatusType[request.POST['status_type']],
        )
        history = History.objects.create(
            user=request.user,
            work=record.work,
            record=record,
            status=record.status,
            status_type=record.status_type,
            updated_at=record.updated_at
        )
        return {
            'record': serialize_record(record),
            'post': serialize_post(history),
        }
Ejemplo n.º 7
0
 def get(self, request, period):
     period = Period.parse(period)
     cache_key = "table:%s" % period
     only_first_period = request.GET.get("only_first_period") == "true"
     if only_first_period:
         cache_key += ":first_only"
     data = cache.get(cache_key)
     if data is None:
         indexes = WorkPeriodIndex.objects.select_related("work", "work__index").filter(period=str(period))
         if only_first_period:
             indexes = indexes.exclude(is_first_period=False)
         data = [serialize_work(index.work) for index in indexes]
         cache.set(cache_key, data, 60 * 60)
     if request.user.is_authenticated():
         records = {}
         work_ids = [work["id"] for work in data]
         for record in request.user.record_set.filter(work_id__in=work_ids):
             records[record.work_id] = serialize_record(record)
         for work in data:
             if work["id"] in records:
                 work["record"] = records[work["id"]]
     return data
Ejemplo n.º 8
0
    def post(self, request, id):
        record = get_object_or_404(Record, id=id)
        self.check_login()
        if request.user.id != record.user_id:
            self.raise_error('Permission denied.', status=403)
        history = History.objects.create(
            user=request.user,
            work=record.work,
            status=request.POST['status'],
            status_type=StatusTypes.from_name(request.POST['status_type']),
            comment=request.POST['comment'],
        )

        services = []
        if request.POST.get('publish_twitter') == 'on':
            services.append('twitter')

        post_history(history, services)

        return {
            'record': serialize_record(history.record),
            'post': serialize_post(history),
        }
Ejemplo n.º 9
0
 def get(self, request, id):
     record = get_object_or_404(Record, id=id)
     return serialize_record(record)