예제 #1
0
 def create(self, validated_data):
     user_ip = self.context['request'].user_ip
     short_url = get_random_string(length=6)
     link = Link(
         short_url=short_url,
         created=datetime.now(),
         author_ip=user_ip,
         redirect_location=validated_data["redirect_location"],
     )
     link.save()
     return link
    def handle(self, *args, **options):
        filename = "inputs.csv"

        Activity.objects.all().delete()
        Category.objects.all().delete()
        Link.objects.all().delete()

        with open("inputs.csv", newline="") as csvfile:
            csvreader = csv.DictReader(csvfile)
            for row in csvreader:
                if row["is_published"].lower() == "false":
                    continue
                print(row["name"])
                activity = Activity(
                    name=row["name"],
                    description=row["description"],
                    date_created=datetime.datetime.strptime(
                        row["date_created"], "%m/%d/%Y").date(),
                    min_cost=row["min_cost"],
                    max_cost=row["max_cost"],
                    min_participants=row["min_participants"],
                    max_participants=row["max_participants"],
                    requirements=row["requirements"],
                )
                activity.save()

                links = [link for link in row["links"].split("\n")]
                for link in links:
                    if len(link):
                        name, url = link.split(",")
                    link = Link(name=name.strip(),
                                url=url.strip(),
                                activity=activity)
                    link.save()

                categories_names = [
                    category.strip().lower()
                    for category in row["categories"].split(",")
                    if len(category)
                ]
                for category_name in categories_names:
                    try:
                        category = Category.objects.get(name=category_name)
                    except Category.DoesNotExist:
                        category = Category.objects.create(name=category_name)

                    category.save()
                    activity.categories.add(category)
예제 #3
0
def create_link(request, from_id, to_id):
    from_cap = Capsule.objects.get(pk=from_id)
    to_cap = Capsule.objects.get(pk=to_id)
    if request.method == "POST":
        link = Link(capsule=to_cap, **{k:v for k,v in request.POST.iteritems()})
        if not Capsule.objects.filter(pk=from_cap.pk, links__capsule=to_cap):
            link.save()
            from_cap.links.add(link)
            return HttpResponse(json.dumps({'data': 'success'}),
                                content_type="application/json")
        return HttpResponse(json.dumps({'data': 'link already exists'}),
                            content_type='application/json')
    elif request.method == "DELETE":
        link = from_cap.links.get(capsule=to_cap)
        if link:
            from_cap.links.remove(link)
            link.delete()
            return HttpResponse(json.dumps({'data': 'success'}),
                                content_type="application/json")
        return HttpResponse(json.dumps({'data': 'link does not exist'}),
                            content_type='application/json')
예제 #4
0
import datetime

from api.models import Link
from api.views import random_string

for i in range(10):
    l = Link(
        short_url=random_string(),
        redirect_location=f"http://127.0.0.1:8000/{i}",
        expiration_date=datetime.datetime.now(),
        created=datetime.datetime.now(),
        author_ip=f"127.0.0.{int(i / 2)}",
        is_active=True,
    )
    l.save()