def to_python(self, value): if isinstance(value, StatusType): return value elif isinstance(value, int): return StatusTypes.from_id(value) elif isinstance(value, str) or isinstance(value, unicode): return StatusTypes.from_name(value)
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
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: self.raise_error(u'작품 제목을 입력하세요.', status=400) # 400 Bad Request 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) self.raise_error(u'이미 같은 작품이 "%s"로 등록되어 있습니다.' % record.title, status=422) # 422 Unprocessable Entity except Record.DoesNotExist: record = Record.objects.create( user=request.user, work=work, title=title, category=category, ) history = History.objects.create( user=request.user, work=record.work, status='', status_type=StatusTypes.from_name(request.POST['status_type']), ) # Sync fields record.status = history.status record.status_type = history.status_type record.updated_at = history.updated_at return { 'record': serialize_record(record), 'post': serialize_post(history), }
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), }