本文整理汇总了Python中accounts.models.Account.find_account_by_user方法的典型用法代码示例。如果您正苦于以下问题:Python Account.find_account_by_user方法的具体用法?Python Account.find_account_by_user怎么用?Python Account.find_account_by_user使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类accounts.models.Account
的用法示例。
在下文中一共展示了Account.find_account_by_user方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: post
# 需要导入模块: from accounts.models import Account [as 别名]
# 或者: from accounts.models.Account import find_account_by_user [as 别名]
def post(self, request, format=None):
'''
create a personal plan
{
"id": 18,
"dish": [{
"type": 0,
"ingredient": [{
"id": 1,
"amount": 100
}],
"recipe": [{
"id": 2,
"amount": 200
}]
},{
"type": 1,
"ingredient": [],
"recipe": [{
"id":3,
"amount": 300
}]
}],
"joined_date": "2015-01-01"
}
'''
try:
body = json.loads(request.body)
user = Account.find_account_by_user(request.user)
try:
joined_date = datetime.strptime(body['joined_date'],'%Y-%m-%d').date()
except:
joined_date = date.today()
planid = body.get('id', None)
if planid is None:
# create
p = Plan.objects.create(user=user)
else :
p = Plan.objects.get(user=user, pk=planid)
p.delete_routines()
r = Routine.objects.create(plan=p)
for dish in body.get('dish', []):
d = Dish.objects.create(type=dish['type'], routine=r)
for si in dish.get('ingredient', []):
ingredient = Ingredient.objects.get(pk=si['id'])
SingleIngredient.objects.create(ingredient=ingredient, amount=si['amount'], dish=d)
for sr in dish.get('recipe', []):
recipe = Recipe.objects.get(pk=sr['id'])
SingleRecipe.objects.create(recipe=recipe, amount=sr['amount'], dish=d)
# after created it join it!
try:
c = Calendar.objects.get(user=user, joined_date=joined_date)
if c.plan.id != p.id:
c.plan = p
c.save()
except Calendar.DoesNotExist:
Calendar.objects.create(user=user, plan=p, joined_date=joined_date)
return self.success_response(PlanSerializer(p).data)
except:
return self.fail_response(400, 'fail')
示例2: get
# 需要导入模块: from accounts.models import Account [as 别名]
# 或者: from accounts.models.Account import find_account_by_user [as 别名]
def get(self, request, format=None):
'''
get my calendar
'''
user = Account.find_account_by_user(request.user)
turn_to_date = lambda x: x and datetime.strptime(x, '%Y%m%d') or date.today()
start_date = turn_to_date(request.GET.get('start', None))
end_date = turn_to_date(request.GET.get('end', None))
plans = []
calendars = (Calendar.objects
.select_related('plan')
.select_related('user')
.filter(user=user, joined_date__gte=start_date, joined_date__lte=end_date))
try:
last_joined = Calendar.objects.filter(user=user, joined_date__lte=start_date).order_by('-joined_date')[0]
last = CalendarSerializer(last_joined, context={'simple': False}).data
plans.append(last['plan'])
except IndexError:
last = None
serializer = CalendarSerializer(calendars, many=True, context={'simple': False}).data
for c in serializer:
plans.append(c['plan'])
punchs = Punch.objects.filter(user=user, date__lte=end_date, date__gte=start_date, state__gte=10)
count = Punch.objects.filter(user=user, state__gte=10)
result = {
'lastJoined': last,
'calendar': serializer,
'punch': PunchSerializer(punchs, many=True).data,
'plans': plans,
'count': len(count)
}
return self.success_response(result)
示例3: post
# 需要导入模块: from accounts.models import Account [as 别名]
# 或者: from accounts.models.Account import find_account_by_user [as 别名]
def post(self, request, format=None):
user = Account.find_account_by_user(request.user)
body = json.loads(request.body)
type = body.get('type', None)
img = body.get('img', None)
if None in (type, img):
return self.fail_response(400, 'Wrong Data')
p = Punch.objects.create(user=user, type=type, img=img)
return self.success_response(PunchSerializer(p).data)
示例4: post
# 需要导入模块: from accounts.models import Account [as 别名]
# 或者: from accounts.models.Account import find_account_by_user [as 别名]
def post(self, request, format=None):
c = Comment()
try:
data = json.loads(request.body)
reply_id = data.get('reply', None)
c.reply= reply_id and Account.objects.get(pk=reply_id)
c.content = data['content']
c.author = Account.find_account_by_user(request.user)
c.recipe = Recipe.objects.get(pk=data['recipe'])
except:
return self.fail_response(400, 'BadArgument')
c.save()
return self.success_response(CommentSerializer(c).data)
示例5: get
# 需要导入模块: from accounts.models import Account [as 别名]
# 或者: from accounts.models.Account import find_account_by_user [as 别名]
def get(self, request, pk, format=None):
'''
return a specific recipe.
'''
recipe = self.get_object(Recipe, pk)
try:
user = Account.find_account_by_user(request.user)
has_collected = RecipeCollection.has_collected(recipe, user)
except Account.DoesNotExist:
has_collected = False
if recipe.status > 0:
serializer = RecipeSerializer(recipe, context={'simple': False})
result = serializer.data
result['has_collected'] = has_collected
result['comment_count'] = recipe.comment_set.count()
return self.success_response(result)
else:
return self.fail_response(400, 'DoesNotExist')
示例6: get
# 需要导入模块: from accounts.models import Account [as 别名]
# 或者: from accounts.models.Account import find_account_by_user [as 别名]
def get(self, request, collection_type, format=None):
lastid = request.GET.get('lastid', None)
if collection_type == 'theme':
model = ThemeCollection
serializer = ThemeCollectionSerializer
elif collection_type == 'series':
model = SeriesCollection
serializer = SeriesCollectionSerializer
elif collection_type == 'recipe':
model = RecipeCollection
serializer = RecipeCollectionSerializer
else:
return self.fail_response(400, 'BadArgument')
owner = Account.find_account_by_user(request.user)
if lastid:
result = model.objects.filter(owner=owner, id__lt=lastid, status__gt=0).order_by('-created_time')[0:20]
else:
result = model.objects.filter(owner=owner, status__gt=0).order_by('-created_time')[0:20]
return self.success_response(serializer(result, many=True).data)