예제 #1
0
파일: views.py 프로젝트: yous/animeta
def create_record(request):
    if 'work' in request.POST:
        title = request.POST['work']
        work = get_or_create_work(title)
    else:
        return {"error": "work is required."}

    if 'status_type' not in request.POST:
        return {"error": "status_type is required."}

    status_type = StatusTypes.from_name(request.POST['status_type'])
    if status_type is None:
        return {"error": "status_type should be watching, finished, suspended, or interested."}

    record, created = request.user.record_set.get_or_create(work=work, defaults={'title': title})

    history = request.user.history_set.create(
        work = work,
        status = request.POST.get('status_text', ''),
        status_type = status_type,
        comment = request.POST.get('comment', ''),
    )

    result = _history_as_dict(history)
    result['record_id'] = record.id
    return result
예제 #2
0
파일: forms.py 프로젝트: frombc7197/animeta
 def clean_work_title(self):
     title = self.cleaned_data['work_title']
     self.work = get_or_create_work(title)
     try:
         r = self.user.record_set.get(work=self.work)
         raise forms.ValidationError(u'이미 같은 작품이 "%s"로 등록되어 있습니다.' % r.title)
     except Record.DoesNotExist:
         pass
     return title
예제 #3
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),
        }
예제 #4
0
파일: views.py 프로젝트: frombc7197/animeta
def add_many(request):
    addition_log = []
    if request.method == 'POST':
        formset = SimpleRecordFormSet(request.POST)
        if formset.is_valid():
            for row in formset.cleaned_data:
                if not row: continue
                title = row['work_title'].strip()
                work = get_or_create_work(title)
                addition_log.append(title)
                history = History.objects.create(user=request.user, work=work, status_type=StatusTypes.Finished)
                record = history.record
                record.title = title
                record.save()

    return render(request, 'record/import.html',
        {'owner': request.user, 'formset': SimpleRecordFormSet(),
         'addition_log': addition_log})
예제 #5
0
 def update_title(self, title):
     work = get_or_create_work(title)
     self.history_set.update(work=work)
     self.work = work
     self.title = title
     self.save()
예제 #6
0
def create_work(request):
    title = request.POST['title'].strip()
    work = get_or_create_work(title)
    return redirect('moderation.views.work_detail', work_id=work.id)
예제 #7
0
 def post(self, request):
     payload = json.loads(request.body)
     work = get_or_create_work(payload['title'])
     return serialize_work(work)