This is my sample documents collection of MongoDB. I need an expected result so anyone guide me to solve my problem
/* 1 */
{
"_id" : 1.0,
"user_id" : "5c592f6716209a24f4125001",
"tu_code" : "5"
}
/* 2 */
{
"_id" : 2.0,
"user_id" : "5c592f6716209a24f4125002",
"tu_code" : "50"
}
/* 3 */
{
"_id" : 3.0,
"user_id" : "5c592f6716209a24f4125003",
"tu_code" : "50.21"
}
/* 4 */
{
"_id" : 4.0,
"user_id" : "5c592f6716209a24f4125004",
"tu_code" : "50.22"
}
/* 5 */
{
"_id" : 5.0,
"user_id" : "5c592f6716209a24f4125005",
"tu_code" : "52"
}
/* 6 */
{
"_id" : 3.0,
"user_id" : "5c592f6716209a24f4125006",
"tu_code" : "5.1"
}
/* 7 */
{
"_id" : 6.0,
"user_id" : "5c592f6716209a24f4125007",
"tu_code" : "5.1.1"
}
/* 8 */
{
"_id" : 7.0,
"user_id" : "5c592f6716209a24f4125008",
"tu_code" : "5.2"
}
/* 9 */
{
"_id" : 8.0,
"user_id" : "5c592f6716209a24f4125009",
"tu_code" : "5.2.1"
}
Here I mentioned my mongo aggregate query below
[
{ "$project": {
"tu_code": { "$toLower": "$tu_code" },
}},
{ "$match": { "tu_code": {"$regex": "^5.*"}}}
]
This above query produces the below result
| _id| tu_code |
| ---| --------|
| 1 | 5 |
| 2 | 50 |
| 3 | 50.21 |
| 4 | 50.22 |
| 5 | 52 |
| 6 | 53 |
| 7 | 5.1 |
| 8 | 5.1.1 |
| 9 | 5.2 |
| 10 | 5.2.1 |
But I want expected result given below
| _id| tu_code |
| ---| --------|
| 1 | 5 |
| 7 | 5.1 |
| 8 | 5.1.1 |
| 9 | 5.2 |
| 10 | 5.2.1 |
Anyone help me to solve this I don't know how to get my expected result.
You can use the \b (word boundary) operator at the end of search expression,
[
{
"$match": {
"tu_code": {
"$regex": "^5\\b"
}
}
}
]
Playground
Related
I have Appsync API connecting to a Dynamo table.
Dynamo table has data : ("id" is key and "year" is sort key)
|--------------|-------------|-------------|-------------|-------------|
| id | year | name | class | subject |
|--------------|-------------|-------------|-------------|-------------|
| 001 | 2017 | Tom | E1 | Math |
|--------------|-------------|-------------|-------------|-------------|
| 002 | 2017 | Mary | E1 | Math |
|--------------|-------------|-------------|-------------|-------------|
| 003 | 2017 | Peter | E1 | Math |
|--------------|-------------|-------------|-------------|-------------|
the schema
type Query {
listStudents(filter: TableStudentFilterInput, limit: Int, nextToken: String): StudentConnection
}
type StudentConnection {
items: [Student]
nextToken: String
}
input TableStudentFilterInput {
id: TableStringFilterInput
year: TableStringFilterInput
name: TableStringFilterInput
class: TableStringFilterInput
subject: TableStringFilterInput
}
type Student {
id: String!
year: String!
name: String
class: String
subject: String
}
Query:
query listStudentByYear {
listStudents (filter:{year:{eq:"2017"}}) {
items {
id
year
name
class
subject
}
}
}
The issue: The query return 001 and 002, but not 003.
When I tried to update "id" from 003 to 004, then the query returns correctly 001, 002, 004.
This weird issue happens quite frequently, after some times, the AppSync query returns an incomplete result (missing some).
Any suggestion is appreciated.
Check out this thread from amplify-js issues.
Essentially what is happening is a limit is applied before the filter. So if you have a limit of 20 and 003 is entry number 21 it will not be included in the filter operation.
A workaround here is to remove the limit from you resolver in the AWS AppSync Console
So change this:
#set( $ListRequest = {
"version": "2017-02-28",
"limit": $limit
})
to this:
#set( $ListRequest = {
"version": "2017-02-28",
} )
Now this isn't a graceful workaround as the DynamoDB Scan will only return 1MB of data meaning this solution will not work for large (practical) implementations.
I have a script, it work perfectly and everything is show on my power shell screen "Console". But I try to figure Out how to export in CSV
Script:
foreach($vds in Get-VDSwitch)
{
$vds.ExtensionData.Runtime.HostMemberRuntime | %{
$.HealthCheckResult | where{$ -is [VMware.Vim.VMwareDVSVlanHealthCheckResult]} |
Select #{N='vdSwitch';E={$vds.Name}},
UplinkPortKey,
#{N='TrunkedVLAN';E={
($.TrunkedVLAN | %{
if($.Start -eq $.End){
"{0}" -f $.Start
}
else{
"{0}-{1}" -f $.Start,$.End
}
}) -join ','
}}
}
}
The Output on screen look like this;
VsanEnabled : False
VsanDiskClaimMode : Manual
HATotalSlots : 3099
HAUsedSlots : 22
HAAvailableSlots : 1527
HASlotCpuMHz : 32
HASlotMemoryMb : 328
HASlotMemoryGB : 0.3203125
HASlotNumVCpus : 1
ParentId : Folder-group-h28
ParentFolder : host
HAEnabled : True
HAAdmissionControlEnabled : True
HAFailoverLevel : 1
HARestartPriority : Medium
HAIsolationResponse : DoNothing
VMSwapfilePolicy : WithVM
DrsEnabled : True
DrsMode : FullyAutomated
DrsAutomationLevel : FullyAutomated
EVCMode : intel-nehalem
Name : mac01dmzp01
CustomFields : {}
ExtensionData : VMware.Vim.ClusterComputeResource
Id : ClusterComputeResource-domain-c12033
Uid : /VIServer=cn\t175726#mac01vcp02.cn.ca:443/Cluster=ClusterComputeResource-domain-c12033/
vdSwitch : vds-toronto-mac01-2-ports-10Gbe
UplinkPortKey : 78
TrunkedVLAN : 11-17,396,500
vdSwitch : vds-toronto-mac01-2-ports-10Gbe
UplinkPortKey : 79
TrunkedVLAN : 11-17,396,500
vdSwitch : vds-toronto-mac01-2-ports-10Gbe
UplinkPortKey : 82
TrunkedVLAN : 11-17,396,500
vdSwitch : vds-toronto-mac01-2-ports-10Gbe
UplinkPortKey : 83
TrunkedVLAN : 11-17,396,500
vdSwitch : vds-toronto-mac01-2-ports-10Gbe
UplinkPortKey : 358
TrunkedVLAN : 11-17,396,500
vdSwitch : vds-toronto-mac01-2-ports-10Gbe
UplinkPortKey : 359
TrunkedVLAN : 11-17,396,500
a lot more ......
I found the way to do it, is with a function.
#####################################################
# vSphere 6.5
# Get ESX HealthCheck Network Config from VDS
#
# by Gerald Begin (Nov.20 2018)
#################################
##### Set Script Location
Set-Location T:\___Main-Script___\_VDS-vLANs_
##### Add VMWare Module.
Get-Module -Name VMware* -ListAvailable | Import-Module
##### Output Path
$Desti = 'T:\___Main-Script___\_VDS-vLANs_\Output'
Import-Module -Name "T:\__Script_Functions__\Connect2All.ps1" -Force:$true # Function to Connect to ALL vCenters
$Clster = "mac01dmzp01"
#### --------------------------------------
function GetInfo {
###################################################
foreach($vds in Get-VDSwitch)
{
$vds.ExtensionData.Runtime.HostMemberRuntime | %{
$_.HealthCheckResult | where{$_ -is [VMware.Vim.VMwareDVSVlanHealthCheckResult]} |
Select #{N='vdSwitch';E={$vds.Name}},
UplinkPortKey,
#{N='TrunkedVLAN';E={
($_.TrunkedVLAN | %{
if($_.Start -eq $_.End){
"{0}" -f $_.Start
}
else{
"{0}-{1}" -f $_.Start,$_.End
}
}) -join ','
}}
}
}
}
Get-Cluster -Name $Clster | GetInfo | Export-Csv -Path $Desti\Results.csv -NoTypeInformation
Disconnect-VIServer * -Confirm:$false
How to write right test, in order to test the below csv data stored in database table. In the input other than item, anything could be optional.
In this, item is key, rest all goes as part of json format typically it looks like this in database {"brand": "Brand6", "category": "Category6", "subcategory": "Sub-Category6"}
Input:
item,category,subcategory,brand,type,feature
TEST-ITEM6,Category6,Sub-Category6,Brand6
TEST-ITEM7,Category7,Sub-Category7,Brand7,TYPE7,FEATURE7
TEST-ITEM8,Category8,Sub-Category8,Brand8,TYPE8,FEATURE8
Test case tried:
def "Case 3a. Verify New 2 records with two more additional fields along with earlier fields to the same tenant"() {
expect:
sql().eachRow("SELECT * FROM item WHERE item IN ('"+item+"')") { row ->
def dbItem = row[0]
def dbAttributes = getJsonToObject(row[1])
def dbCategory = dbAttributes.getAt("category").toString()
def dbSubCategory = dbAttributes.getAt("subcategory").toString()
def dbBrand = dbAttributes.getAt("brand").toString()
def dbType = dbAttributes.getAt("type").toString()
def dbFeature = dbAttributes.getAt("feature").toString()
assert dbItem == item
assert category == dbCategory
assert subcategory == dbSubCategory
assert brand == dbBrand
assert type == dbType
assert feature == dbFeature
}
where:
item << ['TEST-ITEM6', 'TEST-ITEM7', 'TEST-ITEM8']
category << ['Category6','Category7', 'Category8']
subcategory << ['Sub-Category6','Sub-Category7', 'Sub-Category8']
brand << ['Brand6','Brand7', 'Brand8']
type << ['TYPE7', 'TYPE8']
feature << ['FEATURE7', 'FEATURE8']
}
Error:
Condition not satisfied:
type == dbType
| | |
TYPE8| TYPE7
false
1 difference (80% similarity)
TYPE(8)
TYPE(7)
Expected :TYPE7
Actual :TYPE8
In this case I would recommend to use Data Tables as it becomes more readable and resembles your input more closely.
And while type and feature are optional, you need to provide some value for it. It could be null or it could be an empty List or Map (if an Item can have more than one type/feature)
So you where block might look like this:
item | category | subcategory | brand | typeFeatureMap
'TEST-ITEM6' | 'Category6' | 'Sub-Category6' | 'Brand6' | [:] // empty
'TEST-ITEM7' | 'Category7' | 'Sub-Category7' | 'Brand7' | ['TYPE7':'FEATURE7']
'TEST-ITEM8' | 'Category8' | 'Sub-Category8' | 'Brand8' | ['TYPE8':'FEATURE8']
I would also recommend to collect the data and then compare it, so you get around ordering issues.
So bofore your eachRow do something like
def itemFeatures = [:]
In your eachRow do something like
itemFeatures.put(dbAttributes.getAt("type").toString(), dbAttributes.getAt("feature").toString())
And afterwards
itemFeatures == typeFeatureMap
While not answering your question, I would recommend to think about separating the tests from your database if possible.
If you create separate tests for an database abstraction layer and your business logic, you'll be more happy in the long run ;)
For the optional fields you can use the Elvis operator ?: like this (sorry, long code, I modeled your database and two new test cases, one with many optional fields and one failing test):
package de.scrum_master.stackoverflow
import spock.lang.Specification
import spock.lang.Unroll
class DataTableWithOptionalItemsTest extends Specification {
#Unroll
def "Case 3a. Verify record '#item' with possibly optional fields"() {
expect:
testData[item].each { row ->
def dbItem = row["item"]
def dbCategory = row["category"]
def dbSubCategory = row["subcategory"]
def dbBrand = row["brand"]
def dbType = row["type"]
def dbFeature = row["feature"]
assert dbItem == item
assert (category ?: dbCategory) == dbCategory
assert (subcategory ?: dbSubCategory) == dbSubCategory
assert (brand ?: dbBrand) == dbBrand
assert (type ?: dbType) == dbType
assert (feature ?: dbFeature) == dbFeature
}
where:
item | category | subcategory | brand | type | feature
'TEST-ITEM6' | 'Category6' | 'Sub-Category6' | 'Brand6' | null | null
'TEST-ITEM7' | 'Category7' | 'Sub-Category7' | 'Brand7' | 'TYPE7' | 'FEATURE7'
'TEST-ITEM8' | 'Category8' | 'Sub-Category8' | 'Brand8' | 'TYPE8' | 'FEATURE8'
'TEST-ITEM9' | null | null | null | null | null
'TEST-FAIL' | 'CategoryX' | 'Sub-CategoryX' | 'BrandX' | 'TYPEX' | 'FEATUREX'
}
static final testData = [
'TEST-ITEM6': [
[
item : 'TEST-ITEM6',
category : 'Category6',
subcategory: 'Sub-Category6',
brand : 'Brand6',
type : 'dummy',
feature : null
],
[
item : 'TEST-ITEM6',
category : 'Category6',
subcategory: 'Sub-Category6',
brand : 'Brand6',
type : null,
feature : "foo"
]
],
'TEST-ITEM7': [
[
item : 'TEST-ITEM7',
category : 'Category7',
subcategory: 'Sub-Category7',
brand : 'Brand7',
type : 'TYPE7',
feature : 'FEATURE7'
],
[
item : 'TEST-ITEM7',
category : 'Category7',
subcategory: 'Sub-Category7',
brand : 'Brand7',
type : 'TYPE7',
feature : 'FEATURE7'
]
],
'TEST-ITEM8': [
[
item : 'TEST-ITEM8',
category : 'Category8',
subcategory: 'Sub-Category8',
brand : 'Brand8',
type : 'TYPE8',
feature : 'FEATURE8'
],
[
item : 'TEST-ITEM8',
category : 'Category8',
subcategory: 'Sub-Category8',
brand : 'Brand8',
type : 'TYPE8',
feature : 'FEATURE8'
]
],
'TEST-ITEM9': [
[
item : 'TEST-ITEM9',
category : 'Category1',
subcategory: 'Sub-Category1',
brand : 'Brand1',
type : 'TYPE1',
feature : 'FEATURE1'
],
[
item : 'TEST-ITEM9',
category : null,
subcategory: null,
brand : null,
type : null,
feature : null
]
],
'TEST-FAIL' : [
[
item : 'TEST-FAIL',
category : 'CategoryX',
subcategory: 'Sub-CategoryX',
brand : 'BrandY',
type : 'TYPEX',
feature : 'FEATUREX'
]
]
]
}
I want to generate in google line graph the same in the picture but I cannot to do it. I have A,B and C each of these letters have some values ( A1, A2, B1, B2 ... e.g ) The date is the same it differs only time ( minutes or seconds but day is similar ) I could generate only one point for one letter in one date:
[cols] => Array
(
[0] => Array
(
[id] =>
[label] => Timestamp
[type] => string
)
[1] => Array
(
[id] =>
[label] => A
[type] => number
)
[2] => Array
(
[id] =>
[label] => B
[type] => number
)
[3] => Array
(
[id] =>
[label] => C
[type] => number
)
)
[rows] => Array
(
[0] => Array
(
[c] => Array
(
[0] => Array
(
[v] => 2014-01-30
)
[1] => Array
(
[v] => 120
)
[2] => Array
(
[v] => 100
)
[3] => Array
(
[v] => 35
)
)
)
[1] => Array
(
[c] => Array
(
[0] => Array
(
[v] => 2014-01-30
)
[1] => Array
(
[v] => 334
)
[2] => Array
(
[v] => 55
)
[3] => Array
(
[v] => 15
)
)
)
)
These code gives me 3 seperate lines with only 3 values in one date ( 2014-01-30 ) and next date is also the same ( 2014-01-30 ) But I want to collect all these data in one line as I mentioned in photo below. Thanks in advance for everybody!
Making this work is going to require a bit of trickery. You need to organize your data like this:
Date | Type | Value | Label
---------------------------------
30.01.2014 | A | 75 | A1
30.01.2014 | A | 100 | A2
30.01.2014 | A | 125 | A3
30.01.2014 | B | 150 | B1
30.01.2014 | B | 175 | B2
30.01.2014 | B | 200 | B3
30.01.2014 | C | 180 | C1
30.01.2014 | C | 210 | C2
30.01.2014 | C | 270 | C3
31.01.2014 | A | 75 | A1
31.01.2014 | A | 100 | A2
31.01.2014 | A | 125 | A3
31.01.2014 | B | 150 | B1
31.01.2014 | B | 175 | B2
31.01.2014 | B | 200 | B3
31.01.2014 | C | 180 | C1
31.01.2014 | C | 210 | C2
31.01.2014 | C | 270 | C3
The data needs to be ordered in the order you want the line drawn (so if you want the line to be in the order A1, A2, A3, B1, B2, B3 on 30.01.2014, then that is the order it must be in the table).
Next, you need to use a DataView to split this into multiple series of data to get the points color-coded like your legend:
var view = new google.visualization.DataView(data);
view.setColumns([0, {
type: 'number',
label: 'A',
calc: function (dt, row) {
return (dt.getValue(row, 1) == 'A') ? dt.getValue(row, 2) : null;
}
}, {
type: 'string',
role: 'annotation',
calc: function (dt, row) {
return (dt.getValue(row, 1) == 'A') ? dt.getValue(row, 3) : null;
}
}, {
type: 'number',
label: 'A',
calc: function (dt, row) {
return (dt.getValue(row, 1) == 'B') ? dt.getValue(row, 2) : null;
}
}, {
type: 'string',
role: 'annotation',
calc: function (dt, row) {
return (dt.getValue(row, 1) == 'B') ? dt.getValue(row, 3) : null;
}
}, {
type: 'number',
label: 'A',
calc: function (dt, row) {
return (dt.getValue(row, 1) == 'C') ? dt.getValue(row, 2) : null;
}
}, {
type: 'string',
role: 'annotation',
calc: function (dt, row) {
return (dt.getValue(row, 1) == 'C') ? dt.getValue(row, 3) : null;
}
}, 2]);
Then, when drawing the chart, set the series option to make the points and line appear correctly:
series: {
0: {
// series A options
pointSize: 3,
lineWidth: 0
// you can set the color here with the "color" option if you want
},
1: {
// series B options
pointSize: 3,
lineWidth: 0
// you can set the color here with the "color" option if you want
},
2: {
// series C options
pointSize: 3,
lineWidth: 0
// you can set the color here with the "color" option if you want
},
3: {
// this series draws the line
pointSize: 0,
lineWidth: 1,
visibleInLegend: false,
enableInteractivity: false
// you can set the color here with the "color" option if you want
}
}
See an example here: http://jsfiddle.net/asgallant/bn9tE/
I'm trying to generate a nested Json output using django-rest-framework.
But can't find the correct way to go...
Can someone please drive me to correct direction?
Thanks a lot!
--
Here are my models:
class Pacmr(models.Model):
package = models.ForeignKey(Package)
architecture = models.ForeignKey(Architecture)
component = models.ForeignKey(Component)
mirror = models.ForeignKey(Mirror)
release = models.ForeignKey(Release)
version = DebVersionField()
Package, Architecture, Component, Mirror, Release are all equivalent to:
class Package(models.Model):
name = models.CharField(max_length=50)
comment = models.CharField(max_length=200)
An example of the Pacmr table content:
SELECT id, package_id, architecture_id, component_id, mirror_id, release_id, version from pacmr_pacmr where package_id = 20671;
id | package_id | architecture_id | component_id | mirror_id | release_id | version
--------+------------+-----------------+--------------+-----------+------------+--------------
965201 | 20671 | 236 | 146 | 73 | 111 | 3.0-2.1b-1
965200 | 20671 | 236 | 146 | 73 | 109 | 3.0-2.1b-2.1
965199 | 20671 | 236 | 146 | 73 | 113 | 3.0-2.1a-5
691277 | 20671 | 236 | 146 | 73 | 105 | 3.0-2.1b-3
691276 | 20671 | 236 | 146 | 73 | 107 | 3.0-2.1b-3
691275 | 20671 | 236 | 146 | 73 | 104 | 3.0-2.1b-3
691274 | 20671 | 236 | 146 | 75 | 113 | 3.0-2.1a-5
691273 | 20671 | 236 | 146 | 72 | 111 | 3.0-2.1b-1
691272 | 20671 | 236 | 146 | 72 | 110 | 3.0-2.1b-1
691271 | 20671 | 236 | 146 | 72 | 109 | 3.0-2.1b-2.1
691270 | 20671 | 236 | 146 | 72 | 108 | 3.0-2.1b-2.1
691269 | 20671 | 236 | 146 | 72 | 113 | 3.0-2.1a-5
691268 | 20671 | 236 | 146 | 72 | 115 | 3.0-2.1a-5
691267 | 20671 | 236 | 146 | 72 | 107 | 3.0-2.1b-3
691266 | 20671 | 236 | 146 | 72 | 106 | 3.0-2.1b-3
691265 | 20671 | 236 | 146 | 72 | 104 | 3.0-2.1b-3
691264 | 20671 | 236 | 146 | 72 | 103 | 3.0-2.1b-3
691263 | 20671 | 236 | 144 | 71 | 97 | 3.0-2.1b-2.1
691262 | 20671 | 236 | 144 | 71 | 67 | 3.0-2.1b-3
691261 | 20671 | 236 | 144 | 71 | 114 | 3.0-2.1b-3
691260 | 20671 | 236 | 144 | 71 | 68 | 3.0-2.1b-3
691259 | 20671 | 236 | 144 | 71 | 101 | 3.0-2.1b-3
691258 | 20671 | 236 | 144 | 71 | 100 | 3.0-2.1b-3
691257 | 20671 | 236 | 144 | 70 | 97 | 3.0-2.1b-2.1
691256 | 20671 | 236 | 144 | 70 | 98 | 3.0-2.1b-2.1
691255 | 20671 | 236 | 144 | 70 | 67 | 3.0-2.1b-3
691254 | 20671 | 236 | 144 | 70 | 99 | 3.0-2.1b-3
691253 | 20671 | 236 | 144 | 70 | 68 | 3.0-2.1b-3
691252 | 20671 | 236 | 144 | 70 | 96 | 3.0-2.1b-3
With joins it look like this:
SELECT pacmr_pacmr.id as id, pacmr_package.name as package, pacmr_architecture.name as architecture, pacmr_component.name as component, pacmr_mirror.name as mirror, pacmr_release.name as release, version from pacmr_pacmr, pacmr_package, pacmr_architecture, pacmr_component, pacmr_mirror, pacmr_release where package_id=pacmr_package.id and architecture_id=pacmr_architecture.id and component_id=pacmr_component.id and mirror_id=pacmr_mirror.id and release_id=pacmr_release.id and package_id = 20671;
id | package | architecture | component | mirror | release | version
--------+---------+--------------+-----------+-------------------+-----------------+--------------
965201 | jvim | source | main | debian | etch | 3.0-2.1b-1
965200 | jvim | source | main | debian | lenny | 3.0-2.1b-2.1
965199 | jvim | source | main | debian | sarge | 3.0-2.1a-5
691277 | jvim | source | main | debian | sid | 3.0-2.1b-3
691276 | jvim | source | main | debian | squeeze | 3.0-2.1b-3
691275 | jvim | source | main | debian | wheezy | 3.0-2.1b-3
691274 | jvim | source | main | debian-amd64 | sarge | 3.0-2.1a-5
691273 | jvim | source | main | debian-production | etch | 3.0-2.1b-1
691272 | jvim | source | main | debian-production | etch-testing | 3.0-2.1b-1
691271 | jvim | source | main | debian-production | lenny | 3.0-2.1b-2.1
691270 | jvim | source | main | debian-production | lenny-testing | 3.0-2.1b-2.1
691269 | jvim | source | main | debian-production | sarge | 3.0-2.1a-5
691268 | jvim | source | main | debian-production | sarge-testing | 3.0-2.1a-5
691267 | jvim | source | main | debian-production | squeeze | 3.0-2.1b-3
691266 | jvim | source | main | debian-production | squeeze-testing | 3.0-2.1b-3
691265 | jvim | source | main | debian-production | wheezy | 3.0-2.1b-3
691264 | jvim | source | main | debian-production | wheezy-testing | 3.0-2.1b-3
691263 | jvim | source | universe | ubuntu | hardy | 3.0-2.1b-2.1
691262 | jvim | source | universe | ubuntu | lucid | 3.0-2.1b-3
691261 | jvim | source | universe | ubuntu | oneiric | 3.0-2.1b-3
691260 | jvim | source | universe | ubuntu | precise | 3.0-2.1b-3
691259 | jvim | source | universe | ubuntu | quantal | 3.0-2.1b-3
691258 | jvim | source | universe | ubuntu | raring | 3.0-2.1b-3
691257 | jvim | source | universe | ubuntu-production | hardy | 3.0-2.1b-2.1
691256 | jvim | source | universe | ubuntu-production | hardy-testing | 3.0-2.1b-2.1
691255 | jvim | source | universe | ubuntu-production | lucid | 3.0-2.1b-3
691254 | jvim | source | universe | ubuntu-production | lucid-testing | 3.0-2.1b-3
691253 | jvim | source | universe | ubuntu-production | precise | 3.0-2.1b-3
691252 | jvim | source | universe | ubuntu-production | precise-testing | 3.0-2.1b-3
(29 rows)
Here an example of what I would like to have with django-rest-framework (hand written):
{
"package_id": 20671,
"package": "jvim",
"mirrors": [
{
"name": "debian",
"components": [
{
"name": "main",
"releases": [
{
"name": "etch",
"version": "3.0-2.1b-1"
},
{
"name": "lenny",
"version": "3.0-2.1b-2.1"
},
{
"name": "sarge",
"version": "3.0-2.1a-5"
},
{
"name": "sid",
"version": "3.0-2.1b-3"
},
{
"name": "squeeze",
"version": "3.0-2.1b-3"
},
{
"name": "wheezy",
"version": "3.0-2.1b-3"
}
]
}
]
},
{
"name": "debian-amd64",
"components": [
{
"name": "main",
"releases": [
{
"name": "sarge",
"version": "3.0-2.1a-5"
}
]
}
]
},
{
"name": "debian-production",
"components": [
{
"name": "main",
"releases": [
{
"name": "etch",
"version": "3.0-2.1a-5"
},
{
"name": "etch-testing",
"version": "3.0-2.1a-5"
},
{
"name": "lenny",
"version": "3.0-2.1a-5"
},
{
"name": "lenny-testing",
"version": "3.0-2.1a-5"
},
{
"name": "sarge",
"version": "3.0-2.1a-5"
},
{
"name": "sarge-testing",
"version": "3.0-2.1a-5"
},
{
"name": "squeeze",
"version": "3.0-2.1a-5"
},
{
"name": "squeeze-testing",
"version": "3.0-2.1a-5"
},
{
"name": "wheezy",
"version": "3.0-2.1a-5"
},
{
"name": "wheezy-testing",
"version": "3.0-2.1a-5"
}
]
}
]
},
{
"name": "ubuntu",
"components": [
{
"name": "universe",
"releases": [
{
"name": "hardy",
"version": "3.0-2.1b-2.1"
},
{
"name": "lucid",
"version": "3.0-2.1b-3"
},
{
"name": "oneiric",
"version": "3.0-2.1b-3"
},
{
"name": "precise",
"version": "3.0-2.1b-3"
},
{
"name": "quantal",
"version": "3.0-2.1b-3"
},
{
"name": "raring",
"version": "3.0-2.1b-3"
}
]
}
]
},
{
"name": "ubuntu-production",
"components": [
{
"name": "universe",
"releases": [
{
"name": "hardy",
"version": "3.0-2.1b-2.1"
},
{
"name": "hardy-testing",
"version": "3.0-2.1b-2.1"
},
{
"name": "lucid",
"version": "3.0-2.1b-3"
},
{
"name": "lucid-testing",
"version": "3.0-2.1b-3"
},
{
"name": "precise",
"version": "3.0-2.1b-3"
},
{
"name": "precise-testing",
"version": "3.0-2.1b-3"
}
]
}
]
}
]
}
Answer myself...
Found a solution based on Django REST framework: non-model serializer
Maybe a better solution could be implemented?
class Pmcra(object):
package = None
package_id = None
def __init__(self, package, *args, **kw):
self.package = package
pass
def do_work(self):
my_dict = {}
if self.package:
pacmrs = Pacmr.objects.filter(package__name=self.package)
mirrors = pacmrs.all().distinct('mirror')
mirrors_array = []
for mirror in mirrors:
self.package_id = mirror.package.id
components = mirrors.filter(mirror=mirror.mirror).distinct('component')
components_array = []
for component in components:
releases = components.filter(component=component.component).distinct('release')
releases_array = []
for release in releases:
releases_array.append( {'name': release.release.__str__(), 'version': release.version.__str__() })
components_array.append( {'name': component.component.__str__(), 'releases': releases_array} )
mirrors_array.append( {'name': mirror.mirror.__str__(), 'components': components_array} )
return { 'package': self.package, 'package_id': self.package_id, 'mirrors': mirrors_array }
class MyView(APIView):
def get(self, request, *args, **kw):
# Process any get params that you may need
# If you don't need to process get params,
# you can skip this part
get_arg1 = request.GET.get('pkg', None)
# Any URL parameters get passed in **kw
myClass = Pmcra(get_arg1, *args, **kw)
result = myClass.do_work()
response = Response(result, status=status.HTTP_200_OK)
return response