class DeviceModelSerializer(serializers.ModelSerializer): belong_business = BusinessSerializer(read_only=True, many=True) label = TagSerializer(read_only=True, many=True) data_center = DatacenterSerializer(read_only=True) location_zone = RoomSerializer(read_only=True) location_cabinet = RackSerializer(read_only=True) series = SeriesSerializer(read_only=True) asset_manager = StaffSerializer(read_only=True) class Meta: model = DeviceModel fields = '__all__'
def get(self, request, *args, **kwargs): preferenceset =UserPreferencesSerializer(request.user).data["preferences"] preferences = Preferences.objects.filter(preference_id__in=preferenceset) tagset = set() for preference in preferences: tagset = set(chain(tagset,PreferencesMappingSerializer(preference).data["tags"])) tags = Tag.objects.filter(tag_id__in=tagset) articleset = set() for tag in tags: articleset = set(chain(articleset,TagSerializer(tag).data["articles"])) articles = ArticleSerializer(Article.objects.filter(article_id__in= articleset),many=True).data return Response(articles, status=status.HTTP_200_OK)
def search_related_tags(request, repository, mucua, args): """ a partir de um termo, buscar termos relacionados 2 queries: - buscar medias que tem o termo e retornar lista de ids de media - puxar todas as tags das medias de lista de ids com quantidade de cada tag -> tag_count """ # limpa url e pega argumentos args = args.split('/sort')[0] args = args.split('/limit')[0] args = args.split('/') try: this_mucua = Mucua.objects.get(description=mucua) except Mucua.DoesNotExist: this_mucua = Mucua.objects.get(description=DEFAULT_MUCUA) medias = Media.objects.all().filter(origin = this_mucua.id) # busca medias com termos buscados for s in args: medias = medias.filter(tags__name = s) medias_id = '' for media in medias: medias_id += str(media.id) + ',' # compõe lista de ids de media medias_id = medias_id[:-1] # retorna tags da lista de ids gerada acima sql = "SELECT t.id, t.name, count(t.name) as tag_count \ FROM \ tag_tag t \ LEFT JOIN \ media_media_tags mt \ ON mt.tag_id = t.id \ LEFT JOIN \ media_media m \ ON m.id = mt.media_id \ WHERE \ m.id IN (" + medias_id + ") \ GROUP BY t.name" tags = Tag.objects.raw(sql) serializer = TagSerializer(tags, many=True) return Response(serializer.data)
def mucua_tags(request, repository, mucua): """ retorna todas as tags dos conteudos de origem [mucua] com quantidade de cada tag -> tag_count """ if mucua == 'rede': tags = Tag.objects.all().filter(media__origin__isnull=False).annotate(tag_count=Count('name')) else: try: this_mucua = Mucua.objects.get(description=mucua) except Mucua.DoesNotExist: this_mucua = Mucua.objects.get(description=DEFAULT_MUCUA) tags = Tag.objects.all().filter(media__origin = this_mucua.id).annotate(tag_count=Count('name')) serializer = TagSerializer(tags, many=True) return Response(serializer.data)
def retrieve(self, request, pk=None): if request.user.is_superuser: tag = Tag.objects.filter(tag_id=pk) articleset = set() for t in tag: articleset = set( chain(articleset, TagSerializer(t).data["articles"])) articles = ArticleSerializer( Article.objects.filter(article_id__in=articleset), many=True).data return Response(ArticleSerializer(articles, many=True).data) else: res = { 'error': 'can not authenticate with the given credentials or the account has been deactivated' } return Response(res, status=status.HTTP_403_FORBIDDEN)
class EventSerializer(serializers.ModelSerializer): tags = TagSerializer(read_only=True, many=True) access_level = AccessLevelSerializer(read_only=True) event_types = EventTypeSerializer(read_only=True) publisher = UserSerializer(read_only=True) org = OrgSerializer(read_only=True) class Meta: model = Event fields = ( 'title', 'slug', 'event_date', 'pub_date', 'access_level', 'location', 'body', 'event_types', 'tags', 'publisher', 'org' ) lookup_field = 'slug'
def post(self, request, *args, **kwargs): data = request.data data["Artist"] = request.user.id with connection.cursor() as cursor: cursor.execute( "SELECT id FROM album_album WHERE id = %s AND User_id = %s", [data["Album"], data["Artist"]]) if (not cursor.fetchone()): return Response( { "error": "Bad album id, or authenticated user does not own album" }, status=status.HTTP_400_BAD_REQUEST) tag_serial = None if (data.get("Genre")): tag_serial = TagSerializer(data={"Genre": data.get("Genre")}, partial=True) if (tag_serial.is_valid()): tag_serial.save() data["Tag"] = tag_serial.data["id"] data.pop("Genre") else: print('Invalid Genre error', track_serializer.errors) return Response(track_serializer.errors, status=status.HTTP_400_BAD_REQUEST) track_serializer = TrackSerializer(data=request.data) if request.data["Album"] and track_serializer.is_valid(): track_serializer.save() return Response(track_serializer.data, status=status.HTTP_201_CREATED) else: if (tag_serial): tag_serial.delete() print('error', track_serializer.errors) return Response(track_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def perform_update(self, serializer: TagSerializer): tag_sync_conf(serializer.save())
def get_top_tags_api(request): tags = get_top_tags() serializer = TagSerializer(instance=tags, many=True) return Response(serializer.data, status=200)
def to_representation(self, value): return TagSerializer(value).data
def post(self, request, *args, **kwargs): user = request.user data = request.data # course = get_object_or_404(Course, pk=kwargs.get("pk")) tags = data object_model = kwargs.get('obj_model') model = ContentType.objects.get(model=object_model) object_id = kwargs.get("obj_pk") response = [] for tag in tags: t = Tag.objects.filter(url=tag['url']).first() if not t: serializer = TagSerializer(data=tag) serializer.is_valid(raise_exception=True) serializer.save() t = serializer.instance et = EntityTag.objects.filter(tag=t.pk, content_type=model.pk, object_id=object_id).first() if not et: entitytag = { "tag": t.pk, "content_type": model.pk, "user": user.pk, "object_id": object_id, } serializer = EntityTagSerializer(data=entitytag) serializer.is_valid(raise_exception=True) serializer.save() response.append(serializer.instance) return Response(EntityTagSerializer(response, many=True).data, status=HTTP_200_OK)