def post(self, *args, **kwargs): """ Handle POST from transcribe page for individual asset :param args: :param kwargs: :return: redirect back to same page """ self.get_context_data() asset = Asset.objects.get(collection__slug=self.args[0], slug=self.args[1]) if self.request.POST.get("action").lower() == 'contact manager': return redirect(reverse('contact') + "?pre_populate=true") if self.request.user.is_anonymous: captcha_form = CaptchaEmbedForm(self.request.POST) if not captcha_form.is_valid(): logger.info("Invalid captcha response") return self.get(self.request, *args, **kwargs) if "tx" in self.request.POST: tx = self.request.POST.get("tx") status = self.state_dictionary[self.request.POST.get("action")] # Save all transcriptions, we will need this reports Transcription.objects.create( asset=asset, user_id=self.request.user.id if self.request.user.id is not None else get_anonymous_user(), text=tx, status=status, ) asset.status = status asset.save() if "tags" in self.request.POST and self.request.user.is_authenticated == True: tags = self.request.POST.get("tags").split(",") utags, status = UserAssetTagCollection.objects.get_or_create( asset=asset, user_id=self.request.user.id) all_tag = utags.tags.all().values_list("name", flat=True) all_tag_list = list(all_tag) delete_tags = [i for i in all_tag_list if i not in tags] utags.tags.filter(name__in=delete_tags).delete() for tag in tags: tag_ob, t_status = Tag.objects.get_or_create(name=tag, value=tag) if tag_ob not in utags.tags.all(): utags.tags.add(tag_ob) return redirect(self.request.path)
def get_context_data(self, **kwargs): """ Handle the GET request :param kws: :return: dictionary of items used in the template """ ctx = super().get_context_data(**kwargs) asset = ctx["asset"] ctx["item"] = item = asset.item ctx["project"] = project = item.project ctx["campaign"] = project.campaign transcription = asset.transcription_set.order_by("-pk").first() ctx["transcription"] = transcription # We'll handle the case where an item with no transcriptions should be shown as status=edit here # so the logic doesn't need to be repeated in templates: if transcription: transcription_status = transcription.status.lower() else: transcription_status = "edit" ctx["transcription_status"] = transcription_status previous_asset = ( item.asset_set.published() .filter(sequence__lt=asset.sequence) .order_by("sequence") .last() ) next_asset = ( item.asset_set.published() .filter(sequence__gt=asset.sequence) .order_by("sequence") .first() ) if previous_asset: ctx["previous_asset_url"] = previous_asset.get_absolute_url() if next_asset: ctx["next_asset_url"] = next_asset.get_absolute_url() tag_groups = UserAssetTagCollection.objects.filter(asset__slug=asset.slug) tags = [] for tag_group in tag_groups: for tag in tag_group.tags.all(): tags.append(tag) captcha_form = CaptchaEmbedForm() # TODO: we need to move this into JavaScript to allow caching the page if self.request.user.is_anonymous: ctx[ "is_anonymous_user_captcha_validated" ] = self.is_anonymous_user_captcha_validated() ctx.update({"tags": tags, "captcha_form": captcha_form}) return ctx
def post(self, *args, **kwargs): """ Handle POST from campaigns page for individual asset :param args: :param kwargs: :return: redirect back to same page """ # don't know why this would be called here # self.get_context_data() if self.request.POST.get("action").lower() == "contact a manager": return redirect(reverse("contact") + "?pre_populate=true") response = requests.get( "%s://%s/ws/asset_by_slug/%s/%s/" % ( self.request.scheme, self.request.get_host(), self.args[0], self.args[1], ), cookies=self.request.COOKIES, ) asset_json = json.loads(response.content.decode("utf-8")) if self.request.user.is_anonymous and not ( self.is_anonymous_user_captcha_validated()): captcha_form = CaptchaEmbedForm(self.request.POST) if not captcha_form.is_valid(): logger.info("Invalid captcha response") messages.error(self.request, 'Invalid Captcha.') return self.get(self.request, *args, **kwargs) else: self.request.session['captcha_validated_at'] = ( datetime.now().timestamp() ) redirect_path = self.request.path if "tx" in self.request.POST and 'tagging' not in self.request.POST: tx = self.request.POST.get("tx") tx_status = self.state_dictionary[self.request.POST.get("action")] requests.post( "%s://%s/ws/transcription_create/" % (self.request.scheme, self.request.get_host()), data={ "asset": asset_json["id"], "user_id": self.request.user.id if self.request.user.id is not None else get_anonymous_user(self.request), "status": tx_status, "text": tx, }, cookies=self.request.COOKIES, ) # dictionary to pick which function should return the next page on a POST submit next_page_dictionary = { Status.EDIT: lambda x, y: x, Status.SUBMITTED: self.submitted_page, Status.COMPLETED: self.completed_page, } if tx_status == Status.EDIT: messages.success(self.request, 'The transcription was saved successfully.') elif tx_status == Status.SUBMITTED: messages.success(self.request, 'The transcription is ready for review.') elif tx_status == Status.COMPLETED: messages.success(self.request, 'The transcription is completed.') redirect_path = next_page_dictionary[tx_status](redirect_path, asset_json) elif "tags" in self.request.POST and self.request.user.is_authenticated == True: tags = self.request.POST.get("tags").split(",") # get existing tags response = requests.get( "%s://%s/ws/tags/%s/" % (self.request.scheme, self.request.get_host(), asset_json["id"]), cookies=self.request.COOKIES, ) existing_tags_json_val = json.loads(response.content.decode("utf-8")) existing_tags_list = [] for tag_dict in existing_tags_json_val["results"]: existing_tags_list.append(tag_dict["value"]) for tag in tags: response = requests.post( "%s://%s/ws/tag_create/" % (self.request.scheme, self.request.get_host()), data={ "campaign": asset_json["campaign"]["slug"], "asset": asset_json["slug"], "user_id": self.request.user.id if self.request.user.id is not None else get_anonymous_user(self.request), "name": tag, "value": tag, }, cookies=self.request.COOKIES, ) # keep track of existing tags so we can remove deleted tags if tag in existing_tags_list: existing_tags_list.remove(tag) # delete "old" tags for old_tag in existing_tags_list: response = requests.delete("%s://%s/ws/tag_delete/%s/%s/%s/%s/" % (self.request.scheme, self.request.get_host(), self.args[0], self.args[1], old_tag, self.request.user.id), cookies=self.request.COOKIES) redirect_path += "#tab-tag" messages.success(self.request, "Tags have been saved.") return redirect(redirect_path)
def get_context_data(self, **kws): """ Handle the GET request :param kws: :return: dictionary of items used in the template """ response = requests.get( "%s://%s/ws/asset_by_slug/%s/%s/" % ( self.request.scheme, self.request.get_host(), self.args[0], self.args[1], ), cookies=self.request.COOKIES, ) asset_json = json.loads(response.content.decode("utf-8")) in_use_url = "/campaigns/%s/asset/%s/" % ( asset_json["campaign"]["slug"], asset_json["slug"], ) current_user_id = ( self.request.user.id if self.request.user.id is not None else get_anonymous_user(self.request) ) page_in_use = self.check_page_in_use(in_use_url, current_user_id) # TODO: in the future, this is from a settings file value discussion_hide = True # Get all transcriptions, they are no longer tied to a specific user response = requests.get( "%s://%s/ws/transcription/%s/" % (self.request.scheme, self.request.get_host(), asset_json["id"]), cookies=self.request.COOKIES, ) transcription_json = json.loads(response.content.decode("utf-8")) response = requests.get( "%s://%s/ws/tags/%s/" % (self.request.scheme, self.request.get_host(), asset_json["id"]), cookies=self.request.COOKIES, ) json_tags = [] if response.status_code == status.HTTP_200_OK: json_tags_response = json.loads(response.content.decode("utf-8")) for json_tag in json_tags_response["results"]: json_tags.append(json_tag["value"]) captcha_form = CaptchaEmbedForm() response = requests.get( "%s://%s/ws/page_in_use_user/%s/%s/" % ( self.request.scheme, self.request.get_host(), current_user_id, in_use_url ), cookies=self.request.COOKIES, ) page_in_use_json = json.loads(response.content.decode("utf-8")) if page_in_use_json["user"] is None: same_page_count_for_this_user = 0 else: same_page_count_for_this_user = 1 page_dict = { "page_url": in_use_url, "user": current_user_id, "updated_on": datetime.now(), } if page_in_use is False and same_page_count_for_this_user == 0: # add this page as being in use by this user # call the web service which will use the serializer to insert the value. # this takes care of deleting old entries in PageInUse table factory = APIRequestFactory() request = factory.post("/ws/page_in_use%s/" % (in_use_url,), page_dict) request.session = self.request.session PageInUseCreate.as_view()(request) elif same_page_count_for_this_user == 1: # update the PageInUse change_page_in_use = {"page_url": in_use_url, "user": current_user_id} test_url = "%s://%s/ws/page_in_use_update/%s/%s/" % ( self.request.scheme, self.request.get_host(), current_user_id, in_use_url ) requests.put( "%s://%s/ws/page_in_use_update/%s/%s/" % ( self.request.scheme, self.request.get_host(), current_user_id, in_use_url ), data=change_page_in_use, cookies=self.request.COOKIES, ) res = dict( super().get_context_data(**kws), page_in_use=page_in_use, asset=asset_json, transcription=transcription_json, tags=json_tags, captcha_form=captcha_form, discussion_hide=discussion_hide, ) if self.request.user.is_anonymous: res['is_anonymous_user_captcha_validated'] = ( self.is_anonymous_user_captcha_validated() ) return res
def get_context_data(self, **kws): """ Handle the GET request :param kws: :return: dictionary of items used in the template """ asset = Asset.objects.get(collection__slug=self.args[0], slug=self.args[1]) in_use_url = "/transcribe/%s/asset/%s/" % (asset.collection.slug, asset.slug) current_user_id = (self.request.user.id if self.request.user.id is not None else get_anonymous_user()) page_in_use = self.check_page_in_use(in_use_url, current_user_id) # TODO: in the future, this is from a settings file value discussion_hide = True # Get all transcriptions, they are no longer tied to a specific user transcription = Transcription.objects.filter(asset=asset).last() # Get all tags, they are no longer tied to a specific user db_tags = UserAssetTagCollection.objects.filter(asset=asset) tags = all_tags = None if db_tags: for tags_in_db in db_tags: if tags is None: tags = tags_in_db.tags.all() all_tags = tags else: pass all_tags = (tags | tags_in_db.tags.all() ).distinct() # merge the querysets captcha_form = CaptchaEmbedForm() same_page_count_for_this_user = PageInUse.objects.filter( page_url=in_use_url, user=current_user_id).count() page_dict = {"page_url": in_use_url, "user": current_user_id} if page_in_use is False and same_page_count_for_this_user == 0: # add this page as being in use by this user # call the web service which will use the serializer to insert the value. # this takes care of deleting old entries in PageInUse table factory = APIRequestFactory() request = factory.post("/ws/page_in_use%s/" % (in_use_url, ), page_dict) request.session = self.request.session PageInUseCreate.as_view()(request) elif same_page_count_for_this_user == 1: # update the PageInUse obj, created = PageInUse.objects.update_or_create( page_url=in_use_url, user=current_user_id) return dict(super().get_context_data(**kws), page_in_use=page_in_use, asset=asset, transcription=transcription, tags=all_tags, captcha_form=captcha_form, discussion_hide=discussion_hide)