Best Python code snippet using localstack_python
tasks.py
Source:tasks.py
1"""Async tasks."""2from django.utils import timezone3from modoboa.lib import cryptutils4from .lib import carddav5from . import models6def get_cdav_client(request, addressbook, write_support=False):7 """Instantiate a new CardDAV client."""8 return carddav.PyCardDAV(9 addressbook.url, user=request.user.username,10 passwd=cryptutils.decrypt(request.session["password"]),11 write_support=write_support12 )13def create_cdav_addressbook(addressbook, password):14 """Create CardDAV address book."""15 clt = carddav.PyCardDAV(16 addressbook.url, user=addressbook.user.username,17 passwd=password,18 write_support=True19 )20 clt.create_abook()21def push_addressbook_to_carddav(request, addressbook):22 """Push every addressbook item to carddav collection.23 Use only once.24 """25 clt = get_cdav_client(request, addressbook, True)26 for contact in addressbook.contact_set.all():27 href, etag = clt.upload_new_card(contact.uid, contact.to_vcard())28 contact.etag = etag29 contact.save(update_fields=["etag"])30 addressbook.last_sync = timezone.now()31 addressbook.sync_token = clt.get_sync_token()32 addressbook.save(update_fields=["last_sync", "sync_token"])33def sync_addressbook_from_cdav(request, addressbook):34 """Fetch changes from CardDAV server."""35 clt = get_cdav_client(request, addressbook)36 changes = clt.sync_vcards(addressbook.sync_token)37 if not len(changes["cards"]):38 return39 for card in changes["cards"]:40 # UID sometimes embded .vcf extension, sometimes not...41 long_uid = card["href"].split("/")[-1]42 short_uid = long_uid.split(".")[0]43 if "200" in card["status"]:44 content = clt.get_vcard(card["href"]).decode()45 contact = models.Contact.objects.filter(46 uid__in=[long_uid, short_uid]).first()47 if not contact:48 contact = models.Contact(addressbook=addressbook)49 if contact.etag != card["etag"]:50 contact.etag = card["etag"]51 contact.update_from_vcard(content)52 elif "404" in card["status"]:53 models.Contact.objects.filter(54 uid__in=[long_uid, short_uid]).delete()55 addressbook.last_sync = timezone.now()56 addressbook.sync_token = changes["token"]57 addressbook.save(update_fields=["last_sync", "sync_token"])58def push_contact_to_cdav(request, contact):59 """Upload new contact to cdav collection."""60 clt = get_cdav_client(request, contact.addressbook, True)61 path, etag = clt.upload_new_card(contact.uid, contact.to_vcard())62 contact.etag = etag63 contact.save(update_fields=["etag"])64def update_contact_cdav(request, contact):65 """Update existing contact."""66 clt = get_cdav_client(request, contact.addressbook, True)67 uid = contact.uid68 if not uid.endswith(".vcf"):69 uid += ".vcf"70 result = clt.update_vcard(contact.to_vcard(), uid, contact.etag)71 contact.etag = result["cards"][0]["etag"]72 contact.save(update_fields=["etag"])73def delete_contact_cdav(request, contact):74 """Delete a contact."""75 clt = get_cdav_client(request, contact.addressbook, True)76 uid = contact.uid77 if not uid.endswith(".vcf"):78 uid += ".vcf"...
lambda_destinations.py
Source:lambda_destinations.py
...7 payload = {8 "version": "1.0",9 "timestamp": timestamp_millis(),10 "requestContext": {11 "requestId": long_uid(),12 "functionArn": func_details.arn(),13 "condition": "RetriesExhausted",14 "approximateInvokeCount": 1,15 },16 "requestPayload": event,17 "responseContext": {"statusCode": 200, "executedVersion": "$LATEST"},18 "responsePayload": {},19 }20 if result and result.result:21 try:22 payload["requestContext"]["condition"] = "Success"23 payload["responsePayload"] = json.loads(result.result)24 except Exception:25 payload["responsePayload"] = result.result...
models.py
Source:models.py
1from django.contrib.auth.models import AbstractUser2from django.db import models3from utils.utils import long_uid4class User(AbstractUser):5 pass6class Token(models.Model):7 """8 The default authorization token model.9 """10 key = models.CharField(max_length=40, default=long_uid, db_index=True, )11 user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='tokens')...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!